Code editors have been around since the 1980s in order to make developers’ lives easier. This might include marking particular pieces of code in different colors, like strings or numbers, or adding automatic indentation. Many code editors, including Vim, also offer more keyboard shortcuts that make coding quicker because you don’t have to grab a mouse all the time.
Aside from code, you can use code editors for any document that’s text-based and doesn’t rely too much on visual effects. The advantage of storing documents in formats like .txt, .sh, .py, etc., which are supported by standard code editors, is that many of these formats are executable as code. Those that are not executable, like .txt, nevertheless take up a lot less disk space than documents ending in .docx or .pptx.
Vim is a dinosaur among code editors, but it’s nowhere close to extinction. Sure, its popularity has been on somewhat of a decline since its inception in the early 1990s. But the fact that it has survived until today, despite the emergence of more integrated and collaborative editors, speaks for its outstanding quality and utility.
What Is Vim?
Vim is a code editor that has been around for several decades. It marks particular pieces of code in different colors, like strings or numbers, and adds automatic indentation. It also offers keyboard shortcuts that make coding quicker because you don’t have to grab a mouse all the time. Vim is an especially good choice if you’re working with limited computing power and therefore prefer a lightweight editor.
Vim is an especially good choice if you’re working with limited computing power and therefore prefer a lightweight editor (me!), and if you’re a command-line aficionado who hates computer mice and the point-and-click procedure that comes with them (also me!).
You’ll have to remember a handful of commands when you start using Vim, though. If you don’t, you might find yourself sifting through an unending feed of memes that mock you for not being able to quit a Vim session.
So, without further ado, let’s get started in Vim!
Getting Started with Vim
Vim works entirely in your terminal, so you don’t need to open any fancy app. Many computers, even today, come with Vim preinstalled, so it’s quite likely that you’re already ready to rumble. You can check by opening your terminal and typing vim
. This should work without any error messages and display a piece of text that lists the Vim version and perhaps a few tips for working with it.
If you do encounter an error message, don’t panic! You can download and install Vim from the official site.
Now, let’s create a simple text file with Vim to keep some notes. You might be surprised to hear that I used to write many of my lecture notes with Vim during my studies. It’s versatile!
We create the file by writing in the terminal:
vim notes.txt
After pressing enter, this command opens the file notes.txt. Note that if notes.txt exists already in your current working directory, it will open that file. Otherwise, it will create a new file.
Basic Vim Skills
When you first open your file in Vim, you will be in normal mode. You can use a variety of commands to navigate the file, search for text, and perform other tasks in this mode. I’ll explain more about this below.
First, we want to populate our note-taking document with some notes, however. To do this, just press i
. This will switch you into insert mode. Now you can take as many notes as you like. Once you’re done, press the esc
key to get back to normal mode.
If you don’t want to do any further edits, you can now type :x
to save the file and quit Vim. Remember this command and you’ll never have memes mocking you!
Essential Vim Commands
Vim would be a lousy code editor if opening a file, inserting text, and saving were all it could do. What makes it so special is all the neat keyboard commands that save you time and energy. Here are the most important ones:
Vim Navigation Commands
h, j, k, and l
: Move the cursor left, down, up, and right, respectively. You can also use the arrow keys, but, as any gamer out there knows, using the standard keyboard will save you tons of time if you need to navigate around a lot.w and b
: Move the cursor forward and backward one word at a time.gg and G
: Move the cursor to the beginning and end of the file, respectively.:n
: Move the cursor to line n.
Vim Editing Commands
i
: Switch to insert mode.a
: Switch to insert mode and move the cursor one character to the right.A
: Switch to insert mode and move the cursor to the end of the line.o
: Create a new line below the current line and switch to insert mode.O
: Create a new line above the current line and switch to insert mode.x
: Delete the character under the cursor.dw
: delete the word under the cursor.dd
: Delete the current line.yy
: Copy the current line (Vim insiders call this “yanking”).p
: Paste the last yanked or deleted text.u
: undo recent changescontrol+r
: redo recent changes- If you want to perform an action a certain number of times, just precede your command with that number of times. For example, if you want to delete the next six lines, just type
6dd
.
Saving and Quitting in Vim
:w
: Save the current file without exiting Vim.:q
: Quit Vim. This only works if you have saved all recent changes.:wq
: Save the file and quit.:q!
: Quit without saving any changes made since the last save.
Advanced Vim Functionalities
If you still don’t see the advantage that Vim has over other code editors, fret not. What really puts the icing on the cake are the advanced functionalities. Feel free to open a file with Vim and try these out for yourself.
Visual Mode
In many contemporary editors, you can’t really edit code one block at a time. For example, I might have a block of code, and I want to insert a zero after the sixth character on each line. This might sound ultra-niche, but I’ve been using this quite regularly when editing input data or repetitive blocks of code that I can’t refactor quickly.
Instead of proceeding line-by-line for hundreds of lines, just use visual mode. This is a mode in which you can visually highlight a block of text and manipulate it. Navigate to the character in front of which you want to place your zero (or whatever sequence of characters you want to insert). Then press control+v
, and use j
or the arrow down to select all lines you want to edit. Next, type a capital I
, and then type a single zero or the sequence of characters you want to insert. Press esc
and watch it appear on all selected lines.
Generally speaking, even if you don’t want to insert any fancy characters on many lines, you can enter visual mode by pressing v
. You can select any text by navigating around the file in visual mode. Then you can yank that text using yy
, or replace it with whatever is on your clipboard by typing p
or get creative with other commands.
Macros
Say you have a huge file, and you need to perform a series of actions over and over. With macros, which are automated input sequences that imitate keystrokes or mouse actions, Vim lets you automate those tasks and save time.
To start recording a macro, type q
followed by a letter (e.g., qa
). Then, press enter and perform the actions you want to record, separated by enter. To stop recording, type q
again. To execute the macro, type @
followed by the letter you used to start recording (e.g., @a
).
You can use this macro for any document you’re editing with Vim, and it will stay active even if you quit the document or quit the terminal altogether. Just make sure to remake your macros if you’re using a different computer.
Use registers to copy and paste between files
Don’t you hate that you can only ever copy-paste one thing at a time? Well, Vim has several registers that you can use to copy and paste text. A register is essentially a virtual place to store and retrieve some text commands, which you can access by its name. This saves you time because you don’t have to type the entire command every time you need it. Instead, you just tell Vim that you want the command from a specific register. For example, you can yank text to the "a
register by typing "ay
, and then paste it by typing "ap
.
Like macros, these registers will stay active even if you quit the document or the terminal. So, if you have a piece of code that you need often, you can yank it to a register. I have a standard note-taking template on one of my registers, but you can use it for whatever content you like.
Use plugins to extend VIM’s functionality
Finally, Vim has a vast ecosystem of plugins that can extend its functionality. Plugins are software add-ons that can be installed on Vim to enhance its operations. They are particularly useful when you are performing tasks that go beyond standard workflows or when you want to save even more time by using additional functionalities.
Some popular plugins include NERDTree (for file navigation), Vim-airline (for a better status bar), and YouCompleteMe (for autocompletion). To install plugins, you can use a plugin manager like Pathogen or Vundle.
Learn to Use Vim
Vim commands might not be super intuitive, but they’re incredibly powerful. If you like working on the command line, don’t like frilly graphic gimmicks and lengthy point-and-click procedures, and want to finish your workday as early as possible, Vim just might become your favorite code editor.
It might be more difficult to learn than some other popular code editors out there, but its lightweight and powerful methods for automation more than make up for that on many occasions.
Happy coding!