[IPython-dev] easiest way to insert a literal tab character in a code cell?

Paul Ivanov pi at berkeley.edu
Thu Apr 3 22:46:03 EDT 2014


Greg Wilson, on 2014-04-03 18:37,  wrote:
> Hi,
> I'd like to put literal tab characters in cells, but of course tab means 
> "indent" to the editor.  What's the easiest way to do this?
> Thanks,
> Greg
> p.s. because I'm going to write Makefiles in the notebook...

Here you go, Greg:

http://nbviewer.ipython.org/url/pirsquared.org/blog/notebooks/indenting-tabs.ipynb

The TL;DR version is - you can toggle tab-literal insertion via:

    %%javascript

    IPython.tab_as_tab_everywhere = function(use_tabs) {
        if (use_tabs === undefined) {
            use_tabs = true; 
        }
        
        // apply setting to all current CodeMirror instances
        IPython.notebook.get_cells().map(
            function(c) {  return c.code_mirror.options.indentWithTabs=use_tabs;  }
        );
        // make sure new CodeMirror instances created in the future also use this setting
        CodeMirror.defaults.indentWithTabs=use_tabs;
        
    };

And then just call

    %%javascript

    IPython.tab_as_tab_everywhere();

I believe you already have code to add buttons to the toolbar
that Matthias and Min supplied in the past, so hooking those
things up would be a breeze.

best,
-- 
                   _
                  / \
                A*   \^   -
             ,./   _.`\\ / \
            / ,--.S    \/   \
           /  `"~,_     \    \
     __o           ?
   _ \<,_         /:\
--(_)/-(_)----.../ | \
--------------.......J
Paul Ivanov
http://pirsquared.org



More information about the IPython-dev mailing list