The world’s leading publication for data science, AI, and ML professionals.

5 Killer Vim Features for Your Data Science Daily Routine

Boost your productivity with those simple tips

Photo by Andrew Neel on Unsplash
Photo by Andrew Neel on Unsplash

Despite being named "the sexiest job of the 21st century," the daily Data Science routine also includes boring and repetitive tasks.

Writing, again and again, the same imports at the top of your files, manually formatting a list of values that a colleague sent by email, or plotting a suspicious column to understand what is wrong with the data, those steps are part of our everyday jobs, and the truth is, they suck a substantial amount of your time.

In this article, I will introduce a few vim features that really help me to remove frictions in my data science workflows. Some of the features are vanilla vim, others require additional plugins. This post just aims to bring food for thought. What works best for me is maybe not the most relevant for you. If you want to get the most out of it, just take a few minutes to identify the frictions in your workflow, you’ll hopefully be able to write down your very own shortcuts by adapting the tricks described below.

1. Use and abuse of registers

You can think about registers as a way to have multiple clipboards, each associated with an identifier so that it can easily be accessed later. Registers’ commands take the form "<register-name><vim-command>.

For instance, "apallows you to paste the content of the register a. When working on a data science project, I often yank common imports in the register i so that I don’t have to manually copy-paste it across scripts. This process is showcased in the gif below.

Showcasing the use of a register. The vim commands are highlighted in yellow. Gif by Author.
Showcasing the use of a register. The vim commands are highlighted in yellow. Gif by Author.

I also have a register that contains basic plotting instructions in "p. I use it almost on a daily basis. So much more convenient than having to constantly google the correct syntax.

Registers are also quite handy for storing basic matplotlib commands. Gif by Author.
Registers are also quite handy for storing basic matplotlib commands. Gif by Author.

Another great thing with registers: they are persisted across vim sessions!

2. Vim’s best-kept secret: line auto-completion

This is a vanilla vim command that I am using all the time but which is surprisingly not very well known by vim users, ctrl-x ctrl-lallows you to auto-complete a full line. Super convenient in data science projects where it is quite common to apply similar transformations to various columns for instance.

Note that the auto-completion takes into account the content of any open buffers. Pretty convenient when you want to quickly insert a path already defined in a different script.

Line auto-completion. Gif by Author.
Line auto-completion. Gif by Author.

3. The all-mighty macros

Vim macros can be a bit daunting at first but once you start using this feature, you wonder how you were actually able to work before that. Macros are used to store a sequence of actions so that the sequence can be applied multiple times. Macros can be started with the command q<macro-identifier>. After defining your actions, the macro is closed with the command q. You can then call your macro with @<macro-identifier>.

Let’s see how this works on a concrete example. I often used macros to quickly format data that I received by email or scrape from the web. In the example below, the goal is to turn a set of unformatted strings into a python list.

Using vim's macro to turn unformatted values into a python list. Gif by Author.
Using vim’s macro to turn unformatted values into a python list. Gif by Author.

4. Fuzzy file finder

ctrlp.vim is a vim plugin that allows you to quickly retrieve files, buffers, etc… thanks to fuzzy matching. It is most useful when working on large projects where you can have dozens of files.

By default, you can call the plugin using ctrl-p. Typing just a few letters of the file name will allow you to retrieve your script.

Finding files with fuzzy matching. Gif by Author.
Finding files with fuzzy matching. Gif by Author.

5. The absolute git plugin

Fugitive is an amazing Git plugin for vim. Quoting his author:

"fugitive is so awesome that it shall be illegal. That’s why it is called fugitive".

Fugitive allows you to call any git commands from the Vim command bar. For the sake of tracking code even faster, I have created mappings for some of the often-used git commands in my .vimrc

And here is a demo of how I use it in practice:

The ultimate git plugin. Gif by Author.
The ultimate git plugin. Gif by Author.

Do you want more?

My development setup actually relies on the joint use of vim and a screen multiplexer called tmux. The key feature of the vim + tmux combo is the possibility to send lines of code directly from vim to your terminal. Associated with ipython, this gives a highly interactive environment, which fits very well with the heavy exploration tasks inherent to data science. If you want to learn more about it, please check out this post.

Thanks for reading! If you enjoyed this type of content, please follow me on Medium. You can also support my writing by joining Medium using my affiliated link.


Related Articles