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

Paul Ivanov pi at berkeley.edu
Fri Apr 4 03:49:28 EDT 2014


Aaron Meurer, on 2014-04-03 23:20,  wrote:
> Since you asked for the easiest way, I should point out that you can just
> paste a tab into a code cell. If you need one to paste, just type
> print('\t') and copy the whitespace below the cell.

Well, I started off my post with exactly that, but figured that
Greg already figured that out and wanted something more elegant.

    The easiest way to do this is to just get a tab character
    somewhere that you can copy, and then paste it in.

    In [1]: print("\t")
        

    In [2]:		# I copy pasted the output of the cell above here

Then I also gave the alternative of using

	get_ipython().set_next_input("\t Your code here")

wich will create a new cell with a tab and your code.


> 
> Aaron Meurer
> 
> 
> On Thu, Apr 3, 2014 at 9:46 PM, Paul Ivanov <pi at berkeley.edu> wrote:
> 
> > 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
> > _______________________________________________
> > IPython-dev mailing list
> > IPython-dev at scipy.org
> > http://mail.scipy.org/mailman/listinfo/ipython-dev
> >

> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev


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



More information about the IPython-dev mailing list