This notebook originally appeared in Paul Ivanov's Journal
Greg Wilson asked on the IPython mailing list:
Subject: easiest way to insert a literal tab character in a code cell? 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...
The easiest way to do this is to just get a tab character somewhere that you can copy, and then paste it in.
# I copy pasted the output of the cell above here
An alternative solution is to make a string with tabs and insert it into another cell, using IPython machinery.
ip = get_ipython()
ip.set_next_input("\tMakefiles are awesome")
Makefiles are awesome
If you have a file on disk or on the web, you can also just use the
%load magic to do this.
default: cat /etc/issue whoami
Such files can be written with the
%%writefile cell magic... but of course
you need to have inserted tabs there in some manner.
%%writefile ~/file_with_tabs default: cat /etc/issue whoami
!make -f /home/pi/file_with_tabs
cat /etc/issue Debian GNU/Linux jessie/sid \n \l whoami pi
We can set up CodeMirror to insert tabs instead of spaces.
The reason we attach
IPython is because when we use the
any variables we define there must be called in the same cell that defined it - they get their own closure.
We could have attached it to
I covered how to add functions like this to the
custom.js file in your profile in my post about disabling blinking in the notebook. That way these little functions are available in every notebook,
without you having to insert a cell defining them.
Now we've got code that allows us to apply the change to all current and future cells. We leave it as an exercise for the interested reader to modify that code and make a little button in the toolbar, to toggle it on a per-cell basis.
You can get to the code mirror instance via