Vim: The Text Editor That Can Make Us More Productive

Vim: The Text Editor That Can Make Us More Productive

To some, Vim is a beautiful relic from the past. To others, it’s that annoying thing you have to escape whenever you need to write a message for a merge commit.

Today I’ll introduce you to this picturesque text editor and its wonders, and show you why we’re still using it 26 years after its first release.

So fire up your terminal on your working directory of choice, and type

 vim sample.txt

so that we can get started! 

First steps: navigating, editing and exiting.

Whenever you open Vim on a file, there are three basic things you may want to do:

  • Reading the file’s contents
  • Writing on the file
  • Quitting the program

Navigation

To navigate a file on Vim, use the letters h,j,k, and l. These commands are called motions, as they move the cursor.

The keys h and l will move your cursor horizontally (one character at a time), while j and k move vertically (one line at a time). If you put your hand on them, the layout sorta makes sense. 

Some people have trouble remembering which key goes up and which goes down. Pro tip: j sorta looks like a downwards pointing arrow.

As a general note, it is considered bad practice -even though it’s possible- to use the arrow keys for moving in Vim. Get used to using hjkl, and I promise you’ll see a significant boost in speed.

Repetition

Once you’re confident moving through a file one character or line at a time, try pressing a number (any number, it could have many digits) before moving. You’ll jump as many times as the number you entered.

This is a very powerful concept in Vim: Repetition. Have you ever found yourself editing a text file and doing the same thing over and over? Especially something very mundane, like deleting quotes and replacing them with commas? Vim’s got you covered: Just do the thing once, and press . to repeat it. Enter a number and press . again if you want to repeat it as many times.

Edition

Moving around in a text file and reading what’s in it is good and all, but what if we need to change some of its contents? Do not despair, editing a file is as easy as pressing the i key. That will move you from normal mode into editing mode

Most Vim commands will only be available on normal mode, and entering editing mode is usually frowned upon, and should be avoided as much as possible. However, when entering editing mode, Vim will be like any other Text Editor (with Syntax Highlighting on), making its functionalities a superset of those available in your typical notepad.

To exit editing mode, press the ESC key to return to normal mode.

Quitting

To quit Vim, enter normal mode if you’re not on it (just press ESC), and press :wq if you want to save your changes (write and quit), or q! if you want to leave without saving.

More commands: Actually useful features.

Editing files from the terminal might make you look like a cool hacker or something, but why should we use this text-based program instead of good old Sublime Text? The answer is commands.

A thousand ways of deleting text

Want to delete part of your file? You could enter editing mode and press backspace once for every character. It doesn’t really beat using Sublime and pressing ctrl+shift+left to select a whole word before deleting it. 

If you really want to harness the power of Vim, you should try pressing the d key. Pressed once, it won’t do anything. But it’s not sitting idle, it is actually expectant, waiting for an order. Pass it a motion like the ones we learned today (l, 5j, whichever you feel like really) and it will gobble those characters up. For instance, dNl for any number N, will delete the following N letters from the cursor. 

Introducing new motions

  • e : Moves the cursor to the end of the current word (defined as a concatenation of letters, numbers and underscores).
  • w : Moves it to the beginning of the next word.

So if I have this text:

Hello there, general.

And my cursor is standing on the H. When I press de in normal mode, the line will end up looking like this:

 there, general.

While using dw will leave it like this:

there, general

Notice how in the second example, there’s no space before ‘there’. 

We could then press i to insert some replacement word after deleting ‘Hello’. Luckily, there’s an even more fluid way of doing that: the c command (for change). Pressing c and a motion is exactly equivalent to pressing d+motion+i.

This is starting to look nicer, but it still doesn’t beat pressing shift+home/end and deleting a whole line in a few keystrokes, right? Well I see that, and raise you to the $ and 0 motions.

  • 0: moves the cursor to the first character in the current line.
  • $: moves the cursor to the last character in the line.

There’s an even faster way of deleting the whole current line though: dd.
And if you want to delete many lines? dxd deletes the x following lines.

Generally useful Vim commands

By now, the usefulness of vim when editing code (and just code — I wouldn’t encourage you to use vim for other text editing) should start to become apparent.

A few other commands you may want to check out are:

  • o and O: create a new line below or above the current one, respectively, and enter editing mode.
  • v : enter visual mode to select text to which you may then apply more commands. V will select the whole current line.
  • r: (followed by a character) replace the current character without exiting normal mode.
  • R: Enter replace mode, similar to editing mode except you’ll be overwriting the text instead of adding characters (like pressing the insert key in most OSes).
  • y or Y: yank (copy) the selected text, or the current line, respectively.
    Notice that yanking will move text to a special Vim reserved buffer, and not to your usual clipboard. This way, you can effectively manage two different clipboards!
  • p : put the yanked content. You can paste from your regular clipboard with ctrl+shift+v as usual (in editing mode), or insert the last string you yanked using p (in normal mode).
  • * : find the next occurrence of the current word.

When writing software, I find myself duplicating lines to change a few words quite often, so I think Yp is an amazing command.

Conclusion

I’ve barely scratched the surface with this introduction, but I hope I’ll at least have persuaded you into trying Vim out for yourself. It may not replace an IDE if you’re coding in Java or C++, especially if you’re using Frameworks and auto-complete is helping you. But when coding in C or Python, I usually pick it as my editor of choice. And sometimes when I need to transform a string quickly, editing it from Vim is faster than coding a script in Bash or Python.

If you want to learn more, let me know, as I’ll probably keep coming back to this as a series. But I also encourage you to try the software on your own, and run the vimtutor program from your shell (it usually comes preinstalled with Linux and on Macs). If you want to really learn how to optimize your Vim use after going through vimtutor, this very geeky, very awesome site may be of interest to you as well.

For part two introducing more Vim features (multiple windows and buffers to do even more complex copying and pasting), click here.

I hope you found this article useful or interesting, and as usual any feedback will be welcome, whether anything I said was plain wrong, or you actually liked some part of this tutorial. 

Follow me for more Programming tutorials, tips and tricks, and consider also following me on Twitter to keep up to date with my articles.

11 thoughts on “Vim: The Text Editor That Can Make Us More Productive

  1. OMG you gave me the fright of my life! I read a headline about an evil millenial illuminati conspiracy trying to kill my favorite (and really the only useable) text editor. My heart starts racing, I go into a full fledged panic mode, …, but wait, it says we can do something about it! So I clicked on the link to see where do I go to get drafted, and all I get is a one page brief intro tutorial on using vim! Don’t do this to me!

  2. First, I’m a millennial and I use vim. Second, can you please not bait people like this? I’m tired of millennial’s being the group that kill all the things boomers loved. This is just another example of scapegoating and click baiting for a worthless article.

    1. Hi! First of all, I’m a millenial too. Secondly, the title was supposed to be a joke, a bit of satire mocking how American writers blame everything on millenials on click-baity titles.

      I’ve received a lot of backlash and the joke seems to have been lost on translation, so I’m just changing it to a generic title. It’s clear we don’t live in the post clickbait utopia in which I thought we did.

Leave a Reply

Your email address will not be published. Required fields are marked *