Vim is a power tool to edit files, and in this series, I want to share tips on using it. This is especially important if you are going to connect to a linux system to do troubleshooting or just general editing. Furthermore, you can use this editor on any operating system (including Windows!)
In this post however, I will go over basic commands. In upcoming posts I will focus on some plugins that I have found to be quite useful for my daily coding.
If you have never used Vim, you should know that it has two modes - COMMAND and INSERT.
COMMAND mode is the default mode you will usually be in when you open vim. To enter this mode, you press the escape key. Some commands in this mode require typing the whole command and pressing enter - this is where the colon comes first (you can see some of these commands below). INSERT mode is the mode that allows you edit the file and can be entered by pressing the letter 'i' or pressing the insert key.
Here are some basic commands (commands that need to be typed with ESC key first will be in bold - you will notice they are also the ones that start with a colon):
- Save file - :w
- Quit - :q
- Quit and discard changes - :q!
- Delete current character - x
- Scroll to bottom - GG
- Scroll to top - gg
- Move cursor (in Command mode) - h (left), j (down), k (up), l (right)
- Move (in Command or Edit mode): Arrow keys
- Delete whole line - dd
- Search - /text to search
- Next match down - n
- Next match up - N
- Replace text in the whole file - :%s/word to find/word to replace/g (g is global)
- Delete a word from current cursor position to the end of the word - dw
- Delete current word from beginning to end of the word - diw
- Go to beginning of a line - ^
- Go to the end of a line - $
Switching out of the editor temporarily
Apart from commands above, one of my favorite is to switch to the terminal temporarily to do something else and then get back to the vim editor (with everything preserved the way it was left):
- In Vim, press the Control + Z key - this should take you back to terminal
- Once you are ready to get back to your editor, type the fg command on the terminal.
Check file diff
Another thing that you can do with vim is to check the diff between files. Just type the following:
vi -d file1 file2
This will open files side by side. To navigate between the files, you can just use Control + ww. Make sure you are in COMMAND mode.
Opening files in tabs
To open multiple files in tabs, type in the following on the terminal:
vi -p file1 file2 file3
To switch tabs, use tab+gt. If you want to switch to the second tab, press 2gt in COMMAND mode.
Opening files as windows
To open files as windows so you can see their contents (instead of tabs), use the following:
vi -o file1 file2
This will open files with file1 at the top, and file2 at the bottom. If you use a capital letter O, it will open file1 on the left, and file2 on the right.
Navigation is similar to tabs. Use the combination of Control + ww to go through all windows one at a time, or, Control + w, [h or j or k or l]. Recall cursor navigation uses similar keys (h = left, j = down, k = up, l = right).