Top 5 Vim Commands To Increase Efficiency

There are many reasons to become more proficient with bash scripting including becoming a more efficient cloud developer, typing faster than you can click on a GUI, and debugging more easily - just to name just a few. Part of navigating bash scripting is editing text files which I’ve found to be a skill I rely on daily.

The way that I edit most text files through the command line is with Vim. Maybe you’re not familiar with Vim but you know Vi. They serve the same purpose, Vim just has a little more flavor added. It has been stated that Vim stands for “Vi improved”. Since I got on the Vim train early, I can’t speak to many of the differences between the two but have loved using Vim as my default text editor when working in Linux boxes and need to make some quick changes to any text file.

To be a better developer, that often means being able to control your tools well. With that in mind, I’ve come up with a small list of Vim shortcuts that have dramatically decreased my time spent editing text files which means more time deploying!

To enter into Vim, simply type vim /path/to/file in your terminal where /path/to/file is the file that you’d like to edit. If you don’t have Vim downloaded, use brew install vim on a Mac or sudo apt install vim for Linux. If neither suits your flavor of OS, a quick google search with "How do install Vim on [insert-your-OS-here] will do.

dd

This one was a real game changer for me when updating known host files or cleaning up configuration files. The dd shortcut removes the entire line your cursor is currently on. So if there’s dead space you want to remove or just a full line of code you don’t want anymore, simply type vim /path/to/file to open the text file you’d like to edit. Then navigate to the line you’d like to delete with your up and down arrows and type dd. The line will then be removed!

/

The forward slash is your search in Vim. This one is crucial for for searching through text files exponentially faster. Combined with the dd command, you’ll be editing text files so fast! To use the search function just type vim /path/to/file to open the text file you’d like to edit. Then type / to open the search function. Type any string you’d like and it will find the next instance from your cursor where that string appears. For example, you could open a file that looks like this:

There once was a man who lived in the ocean. 
The man was small but could swim forever. 
Although he seemed like a normal man, he was not.

Then if your cursor was at the top of the file and you wanted to search for man, it would find the first one - "a man who". If you typed in man to the search again, it would point out the one in the second sentence - "The man was". So on and so forth.

o and O

Lowercase o and uppercase O. These are other quick editing tools in Vim that insert lines either above or below the line your cursor is currently positioned. Little o will add a line underneath the current line and also enter insert mode. Big O will add a line above the current line and also enter insert mode. To use either of these just type vim /path/to/file to open the text file you’d like to edit. Move your cursor with the up and down arrows on your keyboard to the line you want to either insert above or below and then type either o to insert below or O to insert above. How do you get big O? Just hold shift while you hit o.

H and L

These are Vim navigation shortcuts to help you move about the screen. H will move you to the top of the screen. Now, it’s important to know that it won’t move you to the top of the file but rather to the top of the visible screen. So this will change depending on how big you have your terminal window stretched and how much text is displayed. L does the exact opposite, it will navigate you to the bottom of the screen. To use either, just type vim /path/to/file to open the text file you’d like to edit and either hit L or H to move about. Similar to big o, you'd press and hold shift on your keyboard and then either l or h to get them to be uppercase.

nG

This is a critical command for navigating very large files. n represents whatever number you’d like. It could be 2, it could be 200. G is what allows you to jump to that particular n line. For example, if you knew you needed to make a change on the 50th line of a file, you could type 50G (nothing will show on the screen, but know that it’s working) and hit enter. Your cursor will jump to the 50th line and you can make whatever edits you need.

Final Thoughts

While there aren’t many shortcuts listed here, I’m confident they’ll help you dramatically improve your text editing proficiency. These commands are some that I use daily and have helped me increase my debugging speed quite a bit as well as my overall text editing skills. I’ve also included some more reading on further shortcuts as well as the Vim documentation below.

Further Reading

welcome home : vim online

Basic Vim Commands Every Linux User Must Know [With PDF Cheat Sheet]