【Vim】How to use Vim (Learn Faster in the minimum required)
1. Most important things
Vim has two mode, Insert(for edit, monitor show 'INSERT MODE') and Normal(for operation, monitor show nothing).
You can edit the file with insert mode, and save or quit with normal mode. You are in the normal mode after opening a file by vim filename
.
- To Insert mode:
i
(you can edit files as usual) - Back Normal mode:
esc
- Save the file:
:w
in normal mode - Quit Vim:
:q
in normal mode - Save and quit
:wq
in normal mode - Quit without saving
:q!
in normal mode
These are important operations. Please see the rests when you need.
2. Basic of vim
2.1 Starting Vim
- Open a file:
vim filename
- Open Vim without a file:
vim
2.2 Basic Modes
Vim operates in several modes, but the two primary ones are:
- Normal Mode: For navigating and manipulating text. You can see and delete and paste, etc. without add or insert.
- Insert Mode: For inserting text. You can write and delete the words as usual.
2.3 Switching Modes
- Normal Mode to Insert Mode:
Press one of them
i
(insert before the cursor)
I
(insert at the beginning of the line)
a
(append after the cursor)
A
(append at the end of the line)
o
(open a new line below the current line)
O
(open a new line above the current line). - Insert Mode to Normal Mode:
PressEsc
.
2.4 Basic Move Navigation (in Normal Mode)
You can check the file contents:
- h, j, k, l: Move left, down, up, right.
- 0: Move to the beginning of the line.
- $: Move to the end of the line.
- w: Move to the beginning of the next word.
- b: Move to the beginning of the previous word.
- G: Move to the end of the file.
- gg: Move to the beginning of the file.
2.5 Basic Editing (in Normal Mode)
You can delete, copy and paste:
- x: Delete the character under the cursor.
- dd: Delete the current line.
- dw: Delete the word from the cursor.
- u: Undo the last change.
- Ctrl + r: Redo the undone change.
- yy: Yank (copy) the current line.
- p: Paste after the cursor.
- P: Paste before the cursor.
2.6 Saving and Exiting (in Normal Mode)
- :w : Save the file.
- :q : Quit Vim.
- :wq : Save and quit.
- :q! : Quit without saving.
2.7 Visual Mode (for selecting text)
Also you can move in visual mode, and can select, mode, delete, copy and others operations against to rectangle regardress line.
Only introduce how in the visual mode, please search about more information if you interested in.
- v: Start visual mode (select by character).
- V: Start line-wise visual mode (select by line).
- Ctrl + v: Start block-wise visual mode (select a block of text).
- Esc: Exit visual mode.
2.8 Searching (in normal mode)
- /text: Search for 'text' in the file.
- n: Repeat the search in the same direction.
- N: Repeat the search in the opposite direction.
2.9 Command Mode (in normal mode)
Commands are entered by pressing :
followed by the command. Some common commands are:
- :w filename : Save as a different filename.
- :set nu : Show line numbers.
- :set nonu : Hide line numbers.
- :help command : Open help for a specific command.
With these basics, you should be able to start using Vim effectively. As you become more comfortable, you can explore more advanced features and plugins to enhance your works.
Discussion