Vim in Practice and use

Written politely 2020-11-24.

Introduction

These thoughts are my own, for the most part. I have been using Vim for 3+ years at the time of this writing. I wouldn’t say I am the greatest at Vim and I don’t know all there is to know, for sure, but I enjoy the mindset it puts me in when I am coding, which is a big benefit, I believe.

play in life

My first boss as a professional dev used Vim and I was always hearing quick key successions (or chords) with a guttural expression signifying the end of a thought, almost like the breathing that a musician has when playing a piece of music. If you don’t know what I am talking about, listen to some classical guitarists (Segovia, Williams, Bream, etc.) and you can actually hear them breathing and syncing their bodies to their output and phrasing. That is what Vim can do when you get in the zone, or are in a state of flow.

It honestly takes a long time to get to that point where you aren’t think about what keys to push, but you are thinking in the language. “Delete line, down 5 lines, put line above the current line, find ‘t’, next, next, change inner word to ‘role’, escape, :w return”.

I tried three different times after initially seeing my first boss use Vim, and it wasn’t until the third time that it really stuck. It wasn’t easy, but I wanted it. You have to want to do it, too, because the learning curve is a bit steep. This presentation will show the power of what Vim can do, so hopefully you will ‘want it’, too. And if not, no harm no foul. Put it in your back pocket. It isn’t for everyone. But if you spend the time and really commit to using it, it will pay you back exponentially.

If you want to jump right into a game that will teach you what effect the keys in Vim will have on text composition, Vim Adventures is a viable method to do just that.

Lesson 1

Lesson #1

In essence, when you enter vim you have two modes, insert and command modes. Insert mode is entered by typing i in command mode, and exiting insert mode is done by pressing <esc>.

When you are insert mode, you can type just as you normally do. So you are already a master at that half of vim. If you just stayed in insert mode all the time, it wouldn’t be any different than your regular typing other than when you exit insert mode by pressing <esc>.

Normal mode is where the real power of Vim exists. Other programs have the user using keys such as <ctrl> <option> and <command> and the arrow keys to manipulate the position of the cursor and select and edit text. All of these require a user to reach for keys away from the home row to perform these actions. Command mode in Vim keeps the user for the most part on the home row or close to it.

h j k l are the replacements for moving the cursor Left, Down, Up, and Right, respectively, in command mode. This can be very strange to get used to, but to become efficient it needs to get to the point that it is second nature.

Also, this post summarizes what really made it sink in with me, personally.

Find a game to play online where you are moving around the game usually using the arrow keys, and remap them to hjkl. Pacman would be an obvious choice here, because you don’t need any buttons, but directions.

pac man

Play it for a while until you don’t have to think about it. That will take the monotony out of having to get used to it in your editor which will most likely lead to frustration because it will seem so slow to do anything. When using hjkl becomes decently second nature, you are ready to start coding using Vim. Or, screw it and just start using it in an editor. There isn’t a best way to practice this. When asked what the best exercise routine is for everyone, it is one that you will do consistently. Find what works for you and stick with it.

Lesson 2

Lesson # 2

Delete is the same in Vim. Use d to delete a character under the cursor. dd to delete a whole line.

If you have a sentence, This is a sentence that is just an example., and your cursor is on the beginning of the sentence. If you wanted to delete the first two words, you could type dta, which translates as delete till "a". The t means till.

In this example you would remain in command mode but delete everything until a. If you wanted to start typing there, you could instead do cta, or change till "a", which would delete the same characters but put you in insert mode, saving a keystroke.

Use destinations to make deleting and changing more powerful.

Use f to find text forward of the cursor, and F to find text before the cursor.

Typing v will put you in visual mode where the cursor movement will highlight text one and await a command, such as delete or change. Typing V will put you in visual-lines-mode where the cursor will operate on whole lines at a time that the cursor is occupying or has moved to, as compared to just parts of a line.

Lesson 3

Lesson # 3

Copy is yank in Vim, so in command mode, use y to yank a character under the cursor. yy to copy the whole line.

Paste is also the same in vim. Use p to paste a character/line that has been copied into the vim registry after the cursor. Press P to paste before the cursor.

Open a new line below the current line (and change to insert mode) by pressing o, and above the current line by pressing O.

Lesson 4

Lesson # 4

Some of the real power of Vim is revealed in this lesson. In normal mode, if you press *, Vim will take you to the next instance of that word. If you press #, Vim will take you to the previous instance of that word.

Using / to enter command mode and enter a string of text to search for. Pressing n will take you to the next instance of the search query and N will take you to the previous instance.

Lesson 5

Lesson # 7

Here are some special case keys that prove themselves very useful when composing text.

~ will change the case of a character, or a word if it is highlighted in visual mode. No more deleting then reentering the character just to fix the case.

D will delete from the current position to the end of the line, and C will do the same except put the user in insert mode after deleting.

If you want to append the next line to end of the current line, J will do just that. This is a very handy and much quicker way to join multiple lines together.

To indent a line to the next tabstop, use >. To unindent a line, use <.

Replace is something different. Use r to replace the character under the cursor to something else. Doing r4 will replace the character under the cursor to a 4.

So, you know the two modes of vim (input and command) and you can use command to delete, copy, paste, and change text. You can move the cursor around to where you want it. If you master these commands and wonder, “What is next?”, stay tuned for the next post to supercharge your Vim usage. For now, play with these command. They can be arranged endless combinations limited only by your thoughts. Play with it, just as you would learning a new language, because in essence, that is exactly what you are doing. Learning a language to manipulate text in your editor.

Summary

Here is an overview of using Vim while coding and some of the great things that can be accomplished quickly and efficiently.


Lucas McDaniel

Husband, father, teacher, musician, avid gamer, nature enthusiast, and passionate about the human condition.