A Short Guide to Vim: Mastering the Popular File Editor
Vim, the highly popular file editor, is a favorite among programmers. With its active development and frequent updates, it has built a thriving community, even hosting the Vim conference. In modern systems, the vi command is simply an alias to vim, standing for “vi improved”. Let’s dive into the world of Vim with this short guide.
To launch Vim, simply run vi on the command line. You can also specify a filename to edit a specific file:
1 | vi test.txt |
Vim operates in two main modes: command (or normal) mode and insert mode. When you start the editor, you are in command mode, where you can’t enter text as you would in a GUI-based editor. To enter insert mode and begin typing, press the i key. You’ll see the -- INSERT -- message at the bottom of the editor. Now you can seamlessly fill the screen with your file contents.
Navigating within the file can be done using the arrow keys or the h, j, k, and l keys. h-l move left and right, while j-k move up and down.
Once you’re done editing, press the esc key to exit insert mode and return to command mode. In this mode, you can navigate the file but cannot add content. Be mindful of the keys you press, as some may trigger commands.
Saving the file can be done by pressing : to enter command mode, followed by w. To save and quit, use :wq. If you wish to quit without saving, use :q!.
To undo an edit, return to command mode and press u. Redoing an action (canceling an undo) can be achieved by pressing ctrl-r.
The basics covered here are just the beginning of Vim’s capabilities. There is a vast array of commands available for more advanced editing tasks. Here are a few additional commands to get you started:
- Pressing
xdeletes the currently highlighted character. - Pressing
Atakes you to the end of the current line. - Press
0to go to the start of the line. - To delete a word, go to its first character and press
dfollowed byw. If you replacewwithe, the whitespace before the next word is preserved. - Use a number before
dandwto delete multiple words, such asd3wto delete three words forward. - Press
dfollowed bydto delete an entire line. To delete from the cursor position to the end of the line, usedfollowed by$.
To delve deeper into Vim, I recommend checking out the Vim FAQ and running the vimtutor command, which is likely already installed on your system and will greatly aid your Vim explorations.
tags: [“vim”, “file editor”, “command mode”, “insert mode”, “saving files”, “editing commands”]