Vim is really good! Here is why, approximately...

·

8 min read

You almost certainly have a favorite code editor. Perhaps you have a few, and you'll use them all for various projects/languages. Regardless, it is likely that those editors all have one thing in common: they are slow!

I know, big talk from someone who doesn't know what editor(s) you use.

Nearly all code editors, such as VSCode, Intellij, Atom, etc. share the fact that they rely on arrow keys and your mouse, and you have to do everything manually. This just feels tedious and slow. If you want to go up a few lines, you have to move your hand over to the arrow keys, hit the up arrow a few times, use the left and right arrows to navigate to your desired spot in the line, then go back to the keyboard and type away. Worse yet, you could completely remove your hand from the keyboard and use your mouse to actually click the spot you want to type. This may be alright for scrolling around a file to read through it, but can absolutely break your momentum when trying to actively edit and manipulate the code as well.

For me, this is the primary problem vim solves

Vim, for those of you unfamiliar, is a terminal based text editor. You open up your favorite terminal emulator, enter vim and start typing away. Except, as you type, nothing seems to happen at first, not until you happen to hit one of a few keys that let you enter insert mode. This is the key that makes Vim so fast, it has different "modes" that you use the editor in.

The default mode you find yourself in when you enter Vim is normal mode. This is where you spend most of your time when using vim, only leaving it to perform certain actions or to actively insert text into the file. This brings us to insert mode, which is, as I mentioned, the mode where you can begin typing into the file. The other two major modes are visual mode and command mode. Visual mode allows you to select text similarly to highlighting in most editors, and command mode allows you to perform specific commands which require more complex input than a simple keyboard shortcut.

Don't feel pressured to remember all the details I share here. This is purely to showcase all the different, highly efficient ways that Vim improves on a standard text editor. If you want to actually learn Vim, I would suggest a true tutorial

Normal mode

Normal mode is the mode where you will find yourself most often. Any time you want to move your text cursor around the file, this is where you do it. Vim is very different from most editors, in that you don't move around with your arrow keys! (You can choose to do so, but it is slower, and I personally recommend disabling them to help you break the habit) Instead, you use h, j, k, l to move around, respectively moving you left, down, up, and right, when in normal mode. This does mean, however, that when in insert mode, you can't easily navigate the file, as h, j, k, l will instead be typed into the actual file. This is the core of Vim's modal editor model. You use normal mode to navigate around, and insert mode only to actually type text at the location of the cursor, switching back to normal mode with esc as soon as you're done.

Navigate gets much fancier than just h, j, k, l though! When in normal mode, you can navigate by bigger chunks at a time. For example, hitting w will move you forward one full word, and b will move you backwards a full word. $ will take you to the end of the line, while 0 takes you to the beginning.

Keep in mind, 0 takes you to the begging of the line, but in code there's often white-space indenting before your actual code. w takes you to the next word, so most people will hit 0 w to quickly go to the beginning of the actual code on the line.

e will take you to the end of the next word, while g e will take you to the ending of the previous word. } will take you to the next block of code, a { to the previous block. Vim has a truly incredible number of ways to simply navigate your code, and as you gain proficiency with using them, it becomes much faster than using arrow keys or your mouse.

Insert mode

Insert mode is the mode where you actually do your typing. When you are in insert mode, its not very exciting. Anything you type will go into the file, just like any editor you're used to. h, j, k, l, along with any other movement keys, no longer work, as they simply type into the file, so you wont be doing much moving around. There isn't much to say about what you can do while in insert mode. The exciting part is entering insert mode.

As you navigate your file, and wish to input some actual text, you need to actually enter insert mode to do so. The most basic way to do so is pressing i, this will put you into insert mode in front of your cursor, and you may begin typing. Alternatively, you can press a which will put you after your cursor.

Much like movement keys, the ways of enter insert mode also get much fancier. Pressing o will create a new line below the current line you are on, and place you there in insert mode with proper indentation. O(capital o, or shift+o) does the same but above your current line. s will delete the character your cursor is on, and place you into insert mode at that location. S will delete the entire line and place you into insert mode. There is a ton of different ways to enter insert mode that, much like the fancier move keys, will become part of your workflow and overall become much faster than any "normal" editor can compete with. After only a couple days of usage, I found my efficiency with both writing new code, and editing existing code, to be at least on par with normal editors, if not faster in some situations.

Other modes

The other modes, while just as important, are both more complex than I'd like to cover in this article, and less commonly used. These I will gloss over here, but I would recommend finding a proper Vim tutorial for more in depth learning.

In brief, command mode is entered by pressing : while in normal mode, and allows you to do things like open a new file, save your current file, quit out of Vim, etc. (:w saves your file, :q quits vim, and they can be combined as :wq, while ! added to any command will override any warnings, like quitting without saving)

Visual mode, as I mentioned before, is used to select/highlight text. You enter it by pressing v, and navigate with any normal movement keys. This allows you to copy the selected text (y) or cut it (d, which deletes and copies the selection), as well as some fancier things. Again, I recommend finding an in depth tutorial if you are interested.

Extending Vim

Vim is also highly extensible! It has a config file which can be used to not only configure various settings and create your own shortcuts, but to add plugins as well. Much like the popular alternatives, there is a very extensive list of plugins available for Vim to add support for various languages, as well as change the behavior of the editor entirely. You can add things like a pop-up terminal emulator, a full file explorer, etc.

There is also a version of Vim called NeoVim that allows you to write plugins using Lua, a much more powerful language than the VimScript that Vim natively supports. This is the version I use and the version I suggest to most people. The options available with Lua extensions are just unbeatable.

Other interesting features

Vim also has shortcuts in normal mode to do other various useful things when trying to edit your document. For example, x deletes the current character under your cursor. cw deletes up to the end of the word your cursor is in and puts you into insert mode to type a replacement. dd deletes your current line, and dw deletes the current word without entering insert mode. u will undo your last command (including entering insert mode, typing something, and exiting insert mode) and ctrl r will redo.

r followed by another character will replace your current character, while s will replace the current character, leaving you in insert mode.

There is a ton of ways to edit a file, and while you don't need to know them all, knowing the ones you use most will increase the speed of your work considerably.

TL;DR

All of that is meant to outline that Vim allows you to edit and navigate files much faster than being restricted to the mouse and arrow keys! With the right settings and extensions, I find Vim to be just as feature-rich as something like VSCode, including highly intelligent code completion, while maintaining the incredible speed advantages. You don't even have to stick to Vim to keep the efficiency gains you earn. Many popular editors, like VSCode and the Jetbrains IDE's, have plugins that allow to you not only use the Vim shortcuts and modes, but even include your Vim configuration files to maintain the extensibility you come to love.

I cannot recommend Vim enough, once you take the couple of days to get up to speed, you will not only find yourself editing your code much more efficiently, but you will likely find yourself wishing the shortcuts worked in more place. Even sending discord messages will feel sluggish by comparison.