Vim is one of the many tools I've played around with before deciding it was a pain to use compared to the nano editor, but this weekend I finally got the hang of it. And it's definitely worth learning, being superior to almost every other editor out there for several reasons.
For anyone developing skills with the Linux operating system, perhaps studying computer engineering, there'll be situations where text files must be modified in the command line. Applying those skills to the real world, it's likely you'd SSH into a server to modify scripts, configurations and code. So far I've gotten by using nano, but it's often not available on the systems I've worked with - some variation of vi or ed is more common.
Being developed for terminals where only the keyboard is used, there are numerous keybindings and commands, and fluency with these enables the experienced use to perform actions faster than a GUI would allow. This is maybe one of the reasons we find this feature in Visual Studio Code also.
Basic Actions
Press the 'I' or 'A' key to start inserting text. There'll be '
-- INSERT --' at the foot of the editor. In order to save the file or quit, press the '
Esc' key and ':', then enter one of the following commands:
- :w - Write buffer to file
- :wq - Write buffer to file and quit
- :q! - Quit without writing buffer
These are the commands that are quickest to pick up, and they're pretty much all that's needed for basic usage. Also, there is an online help feature, accessible by pressing the tab key after entering the first character of a command.
Navigating Through Text
In addition to the 'I' or 'A' key for entering INSERT mode, there are several other key bindings for navigating through text: 'H', 'J', 'K' and 'L' are used for moving the cursor up, down, left and right one place.
Cutting and Pasting Text, and Reversing Changes
Sections of text can also be copied and pasted. Press 'V' and whichever direction keys to highlight a section of text. Pressing 'C' will cut that section and 'P' will insert it elsewhere. If the user wants to reverse that change (or any other change), this can be done with the 'U' key. Lastly vim enables the user to delete a character or section/paragraph with the 'X' and 'D' keys.
Searching Text
Often you'd want to find a string within the current file. Press the 'Esc' key, and use the following command:
:/[keyword]
And press 'N' to scroll through the matches.
Configuration
The editor's configuration is stored in /etc/vim/vimrc. If the GUI version is installed there'll also be a gvimrc in the same directory, although I found an easier way to configure that is to save and load a session file.
The first thing you might want is a 'soft wrap' which wraps text within the editor without inserting line breaks in the file itself:
set columns=90
But we also don't want to wrap in the middle of words, so set a line break:
set linebreak
Also, I'm used to seeing line numbers when modifying code:
set number
In another directory, /usr/share/vim/vim73/colors, there'll also be a number of colour scheme files. Any of these can be set, e.g.
colorscheme evening
References