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:
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
x
deletes the currently highlighted character. - Pressing
A
takes you to the end of the current line. - Press
0
to go to the start of the line. - To delete a word, go to its first character and press
d
followed byw
. If you replacew
withe
, the whitespace before the next word is preserved. - Use a number before
d
andw
to delete multiple words, such asd3w
to delete three words forward. - Press
d
followed byd
to delete an entire line. To delete from the cursor position to the end of the line, used
followed 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.