When you think of Vim, you don’t typically think machine learning. But, when used strategically, it can save a lot of time on data science tasks. Vim is one of the most popular tools for editing code. It’s fast, efficient and lean. It’s also very customizable so data scientists can configure it to their own workflow. Whether you’re a data scientist or data engineer, learning to use Vim and optimizing its capabilities could speed up code editing and speed up data science tasks.
And, if you are comfortable enough with the Vim key binding and shell commands, you can use Vim to do almost everything you need to do as a data scientist. For instance, data scientists can easily use Vim to edit CSV files, or to run multiple shell commands on a server for data analysis at one time.
But, there’s a common narrative about Vim. Many say it’s outdated, and confusing. The most common complaint - many people have trouble exiting it.
The year is 2019 and Vim remains one of the most used textual editors. Especially if you're working with servers and debugging through the CLI, Vim is the go-to.
So in order to make things easier, here are five tips to help you use Vim more efficiently, and hopefully save you time so that you can build high impact machine learning models.
1. :w !sudo tee %
If there's one thing that can save you trouble and time on Vim - it’s this command. We’ve all been there. Entering a file and editing it, only to get that dreadful red alert:
This file is read only!
At this point, most people will just exit and re-enter with sudo. But, what if you edit your Python script and have to add a lot of lines? With the use of this command, you can save your script from within Vim without exiting it.
Here’s a breakdown of the command:
:w is write
! - Vim to run a command line
sudo- use sudo
tee % - write the current file into the filename presented.
And just like that, your problem is solved.
2. cursor movements
Even though Vim is being used in the command line and with no GUI, I still see many people using the mouse to select words or sentences from Vim. Sometimes, I’ll even see people using Vim character…. by…... character. It’s painfully slow, especially if you have a large file with over 200K lines. Vim was built for speed, and one of the best things that drastically improved my Vim speed was the usage of cursor movements.
If you’ve ever played Dangerous Dave online, then you know how to use arrows to go down, up or side to side. But, there is a much better way to navigate the cursor.
Cursor movements include some interesting commands such as:
$- go to the end of the line
W- go to the end of the current words until the space
B - go the the beginning of the word until space
Once you master the cursor movement you’ll notice how Vim is actually like a command language.
dw- delete word
yB - yank the beginning of the word
3. The power of change
Let's say that you run a machine learning experiment, and the parameter of git commit id was wrong. Now you’d need to edit a config file with a 40 character variable. Believe it or not, most people press “i” to get into insert mode and delete the characters one by one. If you are someone that does this, this tip could save you a lot of time. I present to you, the change mode “c”. By pressing “c” and using the right arrow cursor movement, it will delete all the way up to the cursor. Not only that but it will bring you directly to insert mode. If you want to change the variable, you will just go to the first character of the word and type “cw”. Just like that it will change the word. If it's in between two quotation marks, you can even delete whatever text is inside. For example, if I want to change "cnvrgrocks" I can type ci" - and it will delete the words between the quotation marks and put you in insert mode.
There's also the C, which will change from your current position to the end of the line (same as c$) which is one of the more powerful commands you could use.
4. A “.” can go a long way
This is one of the strongest commands Vim has to offer. The use of a simple “.” will redo the last command you did. For instance, if you change a change a CSV header from user to , type ct,customer
Now you can go to another header, stand on the word user, original_user, type “.” and it will change it to "customer" with one click! Therefore, if you deleted, yanked, pasted, moved as your last command, it comes back without effort.
A “.” can go a long way, but sometimes it doesn’t go quite far enough. Now let's say you want to change the name of a variable in your experiment output data. You’d like to change anything that says "Artificial" to say "Organic". Instead of manually changing each word in each column individually, you can just type this command
Like that, “Artificial” will be changed to “Organic” everywhere in the CSV. The command is much like search and replace in your known GUI text editor.
Implementing these five tips can drastically improve your Vim speed. Once you’ve learned to speed up the process you’ll learn to love Vim. It’s a powerful tool, especially if you are a data scientist working and debugging your code on servers. If you want to see some more tips and tricks, you can use this resource - https://vim.fandom.com