How I use Vim for data-science

R
Bash
Tutorial
Author

Gninahophin Jean-Pierre Axel

Vim is a fantastic text-editor and it’s one a few piece of software that changed my life. Since most of my work involve some kind of data analysis I was concerned about its practicality. And then I just tried it. This post is a testament on how it improved my workflow.

Using R and Python with Vim

If you’ve ever run Python or R directly through the command line, you probably know they both have a console where you can write your code directly and execute it by pressing Enter. So essentialy, to run your code, you just need a text-editor to write it (VIM), a console/repl and a way to send your code to the console.

Doing it manually

In practice, you don’t need to write any complicated code to “send your code to the console”. You can just copy paste your code from Vim to the console.

The thing is, it would get annoying pretty quickly, and programmers like to automate annoying things.

Using a Terminal Multiplexer like Tmux or Screen

Acknowledgements

First of all, let’s give credit to this fantastic 2006 blog post

It kind of led to the creation of the vim-slime plugin I will cover later on.

I will cover Tmux only but you can find the equivalent for Screen here by the author of vim-slime himself.

Tmux

So if you’re not familiar with Tmux, it’s a terminal multiplexer and that’s just a fancy way of saying that you can use it to create/use/view multiple terminals from the same screen.

Now a quick tour into Tmux manual will show us this:

send-keys [-FHlMRX] [-N repeat-count] [-t target-pane] key … (alias: send) Send a key or keys to a window. Each argument key is the name of the key (such as ‘C-a’ or ‘NPage’) to send; if the string is not recognised as a key, it is sent as a series of characters. All arguments are sent sequentially from first to last. If no keys are given and the command is bound to a key, then that key is used.

So you can essentially send keys toward a tmux pane and this pane can have a python/R console running inside of it. Do you see where I’m going with this ?

But it’s even better than what you would think based on this example alone because you can also send keys from a tmux pane to Vim.

Here’s a line that sends a message from the terminal to Vim, exactly where my cursor was. And I chained it with notify-send to simulate a push notification. This could be a fun mechanic for a vim game!

echo "I will get added to Vim." |xargs tmux send-keys -t 0 & notify-send "New message from Beatrix"

Ok , this is cool but not practical if you’re going to use it fairly often. Plus if you’re going to use this with Python, you’ ll need to format the text properly. This is where Vim-slime comes in.

Vim-Slime , the plugin you didn’t know you needed

Vim-slime takes this idea, and run with it by making it extremely convenient.

Check out the github repo for more details.

Honorable mention: Nvim-R if you’re only writing R code

Improving the experience ?

The vim essentials

Auto-completion

If you’re coming from RStudio or VS Code you might look for some kind of auto-completion. There’s many ways to achieve this in Vim.

To get the closest to what they provide, using a solution leveraging the Language server protocol is your best bet. (Search for vim lsp :) )

I don’t use them because I feel like they bloat my system, and Vim native
omni-completion is already good-enough for me.

Some good references are the super-tab repo and if you want even less bloat you can do it yourself by reading these posts on this Vim’s fandom page

Snippets

If you’re typing the same thing quite often, you might benefit from defining a snippet and reusing it.

If you’re writing Latex equations this one is a must have : UltiSnips