Timesavers: Bash kernel for Jupyter notebooks & ctag Vim navigation

I found these two things save me some time while developing,

bash kernel for notebooks

To install,

git clone git@github.com:takluyver/bash_kernel.git
cd bash_kernel
source activate fastai
python -m bash_kernel.install --sys-prefix

This is handy because I often find bash easiest for certain tasks, and building a script in a notebook is easier than running commands in the shell and copying them into an editor

The benefit over python notebook + shell is the syntax becomes simpler when saving or passing variables, and the syntax highlighting is better. Of course, if someone else has a better way, Iā€™m all ears.

ctag vim navigation

Also, unrelated, I found this guide useful for explaining how to do the navigation Jeremy did in Vim with Ctrl+] to move around source code via ā€œtagsā€

Basically, something like this,

sudo apt-get install exuberant-ctags
ctags -R -o ~/ctags ~/fastai-pytorch/fastai/ ~/anaconda3/envs/fastai/lib/python3.6/
echo --python-kinds=-i > ~/.ctags

Add to crontab (might need to change ~ to full path),

1 * * * * ~/ctags ~/fastai-pytorch/fastai/ ~/anaconda3/envs/fastai/lib/python3.6/ 2>/dev/null

notes

  • -i in the ~/.ctags file means ā€œignore importsā€ as discussed here. You might also add -v

  • this doesnā€™t seem to work for python core libraries, for example, if I put the cursor on join in os.path.join it takes me to the definition of join in asyncio/queues.py instead of ntpath.py (which has the code for os.path), but at least it works for jumping around fastai code. Perhaps better still is to use an IDE like Pycharm

Again, if there is an easier or more correct way to set this up, Iā€™m happy to hear it.

9 Likes

Thanks! Both look super useful.

Thanks for sharing this, @rob

Very helpful - might be worth adding the vim tags notes to @timleeā€™s notes and/or the lesson wikiā€¦

Alright, done

1 Like

One last step in the guide that you didnā€™t includeā€¦ You need to add the following to a ~/.vimrc file you create:

:set tags=~/ctags

Thanks for this great info!

3 Likes

Thanks for the info. I want to go through the pytorch source code as well. I am interested in accessing code for torch.optim, but when I run vim -t torch.optim, it leads me to source code where torch.optim is imported. Specifically, to the line:

import torch.optim as optim

I run Ctrl-] on torch, but all it does it lead me to the beginning of the same line. How can I access source code for an imported class/module?

Thank you.

1 Like

I was able to build tags across fastai library and pytorch using this command:
sudo ctags -R -o ~/tags /opt/anaconda3/pkgs/fastai-1.0.34-py_1/site-packages/fastai /opt/anaconda3/pkgs/pytorch-nightly-1.0.0.dev20181024-py3.6_cuda9.2.148_cudnn7.1.4_0

You may need to change directory paths. But this works for me.

Ctags is giving me a different issue though - Iā€™m unable to use ctrl+] command to bring up a definition in vim. The :tag works, as do the other vim ctags shortcuts. Anyone know what could be the issue?

add to ~/.vimrc: :set tags=~/mytags