Vim
Being able to use the vi text editor (and vim, which is an "improved" version of vi) is an invaluable skill because of its ubiquity and efficiency. When more advanced text editors are not available (e.g., on remote servers), vi/vim is almost always present. Additionally, vim's keyboard-centric design allows for very efficient text editing once the user becomes proficient.
Installation
The instructions depend on your operating system:
- On Mac
- Install the homebrew package manager from brew.sh, if not already installed
- Using brew, install macvim by running:
brew install macvim
- On Windows
- Install Windows Terminal, if not already installed
- Install the Chocolatey package manager
- Use Chocolatey to install vim:
choco install vim
- On Linux
- Use your package manager to install vim with the command:
sudo apt install -y vim
- Use your package manager to install vim with the command:
The basics of editing with vim
Modes
Vim has multiple modes, the two most important of which are "normal mode" and "insert mode". Normal mode is for navigating and manipulating text, while insert mode is for inserting text. When you first open a file with vim, you are in normal mode. To enter insert mode, press the i key. To return to normal mode from insert mode, press the Esc key.
If you ever get lost in vim, press
Esca few times to ensure you are in normal mode.
Opening a file
To open a file with vim, run the command vim <filename> in your terminal, replacing <filename> with the name of the file you want to open. If the file does not exist, vim will create a new file with that name.
Saving and quitting
To save your changes to a file, ensure you are in normal mode (press Esc), then type :w and press Enter. To quit vim, ensure you are in normal mode, then type :q and press Enter. To save and quit at the same time, type :wq and press Enter. Alternatively, :x will also save and quit, while saving one keystroke. If you want to quit without saving changes, type :q! and press Enter.
Moving around
When using vim from the terminal, the mouse typically can not used for navigation. Therefore, it is important to learn how to move the cursor using the keyboard.
In normal mode, you can move the cursor using:
h: move leftj: move downk: move upl: move right
You can also use the arrow keys to move the cursor, but using h, j, k, and l is often faster once you get used to it.
Once you begin to explore the capabilities of vim, you will find that there are many more advanced ways to navigate through text, such as jumping between words, paragraphs, and specific lines. The most efficient way to navigate will depend on the specific context and your personal preferences. However, a good starting point is search.
Searching
To search for a specific string of text in vim, ensure you are in normal mode, then type / followed by the string you want to search for, and press Enter. Vim will highlight the first occurrence of the string after the cursor. To move to the next occurrence, press n. To move to the previous occurrence, press N.
Highlighting
Once you identify a variable or function of interest, you may want to highlight it to keep track of it while you navigate the file.
To highlight text in vim, ensure you are in normal mode, then move the cursor to the beginning of the text you want to highlight. Press v to enter visual mode, then use the movement keys (h, j, k, l) to select the text you want to highlight. Once you have selected the text, you can perform various operations on it, such as copying or deleting.