Not everyone likes to write code using impressive IDEs. Many experienced programmers prefer to use the old-school Vim text editor, which runs natively in the Linux terminal. There are also versions available for Windows and MacOS.
Although Vim has a reputation for being intimidating to new users, it offers an incredible degree of control and customization, making it a highly efficient tool for all kinds of text editing.
In this post, we will run through many of Vim‘s basic commands — as well as some more advanced features that will help you become a power user of Vim.
Creating a New File
At the Linux command line, you can always use the
touch command to create an empty file:
However, you can also simply pass a new filename to the vim text editor, and it will create a new file that is ready for editing:
This will start the Vim text editor with an empty buffer that can be saved under the given filename.
A buffer in vim is just an in-memory representation of text.
Getting Started With Vim
If you launch the Vim command without any arguments, then the text editor will launch with an empty buffer that can later be saved wherever you choose.
You can start typing immediately, but it is recommended to switch to Insert mode first. To do so, press the
i key. This will allow you to insert text into the current buffer.
Entering Normal Mode
Once you have finished writing, you can switch back to Normal mode by pressing the
When the editor is in Normal mode, Vim offers a wide range of commands that allow you to work with your text in various ways.
And this is where Vim‘s real power comes into play.
Let’s start by looking at some commands that move the position of the current cursor.
Moving Around by Line
To move the cursor to the beginning of the current line, press
To move to the end of the line, press
To move up or down a line, press
To move to the first line in the current buffer, press
gg, or to move to the last line in the current buffer, press
Moving Around by Character
To move left or right one character at a time, use the
l keys respectively.
Likewise, to move up or down one character at a time, use the
j keys respectively.
You can also use the keyboard’s arrow keys in order to move up, down, left or right by one character at a time.
Moving Around by Word
To move to the beginning of the next word, press
To move to the end of the next word, press
To move to the beginning of the previous word, press
To move to the end of the previous word, press
To copy a character at the cursor’s current position, press
To copy a word, press
To copy a line, press
To paste copied text after the cursor’s current position, press
Alternatively, to paste copied text before the cursor’s current position, press
To delete a character at the cursor’s current position, press
To delete a word, press
To delete a line, press
To delete everything from the cursor’s current position to the end of the buffer, press
To display line numbers, type
To activate the text editor’s spell-checking feature, type
To convert tabs into a sequence of spaces, type
To automatically indent new lines based on the indentation of the previous line, type
Vim Has Three Modes
Vim can operate in one of three modes:
We have already looked at the Normal and Insert modes.
Introducing Visual Mode
Visual mode allows you to select text in a more intuitive way.
In order to enter Visual mode, press
This will allow you to select text using the arrow keys.
Once you have selected your text, you can perform operations on it, such as the following:
- to copy the selected text, press
- to delete the selected text, press
- to copy and delete the selected text, press
- to toggle the case of the selected text, press
- to join multiple lines of selected text into a single line, press
Exiting Vim and Saving Files
To exit the text editor without saving any changes, press
To save the current buffer to disk, press
:w. If a filename has not already been provided or the file has not already been save, then you will be prompted to enter a filename.
To save the current buffer to disk and then exit the text editor, press
:x. However, this will only work if changes have been made to the current buffer, so if that isn’t the case, you should press
Advanced Features of Vim
Now that you have a basic understanding of Vim and its many commands, it’s time to undertake a quick run-through of some of the text editor’s more advanced features.
In the final sections of this blog post, we will look at the following features of Vim:
- how to search and replace text;
- how to store text in registers;
- how to record macros.
Performing Search and Replace
To search for text in Vim, enter Normal mode and press
/, followed by the text you want to search for. Then press the
n will move the cursor to the next occurrence of the search text.
To replace text, enter Normal mode and type
old is the text that you want to replace, and
new is the replacement text.
g at the end of the command stands for “global”, which will replace all occurrences of
new, rather than just the first occurrence.
Vim allows you to store and retrieve text using registers.
A register is just a named storage area where text can be saved.
There are various types of registers in Vim, but the most commonly used are the named registers. There are twenty-six named registers, and each one of them has a single-character name from a to z.
To store text in a register, enter Normal mode and type
a is the name of the register.
Then select the text you want to store using Visual mode, and type
y to save it. This operation is sometimes known as yanking.
To retrieve the text from the register, enter Normal mode and type
a is the name of the register.
Macros allow you to automate repetitive tasks in Vim.
To record a macro, enter Normal mode and type
q, followed by a register name — for example,
Then perform the actions that you want to record.
Once you’re done, type
q again to stop the recording.
To play back the macro, enter Normal mode and type
a is the name of the register where you stored the macro.