Linux - vim vi Learning Notes

Often on Mac or Linux, because we frequently need to use it, but its operations differ greatly from other editors, so I’ll specifically learn it. When using it, won’t be too confused. In learning, need to practice more and use more to become proficient.

Three Modes of vim/vi

  • Command mode
  • Insert mode
  • Last line mode

Command Mode

When we use vim to open a file, we enter command mode. In command mode, we cannot input/edit text. In command mode, we can input the following to switch to other modes

Switch to Insert Mode

i

To return to command mode, press the esc key.

Switch to Last Line Mode

:

To return to command mode, press the esc key.

Specific Operations

Cursor Movement

For cursor movement, we can use regular arrow keys, or vi’s unique way

KeyAction
h or Left Arrow (←)Move cursor left one character
j or Down Arrow (↓)Move cursor down one character
k or Up Arrow (↑)Move cursor up one character
l or Right Arrow (→)Move cursor right one character

Various Insert Mode Switching

KeyAction
i, IEnter insert mode: i is ‘input from current cursor position’, I is ‘start input at first non-space character of current line’
a Aa is ‘start input from next character after current cursor’, A is ‘start input from last character of current line’. (Common)
o, OEnter insert mode. This is uppercase/lowercase of letter o. o is ‘input new line below current cursor line’; O is input new line above current cursor line! (Common)
r, RReplace mode: r only replaces the character at cursor once; R continuously replaces text at cursor until ESC is pressed; (Common)
[Esc]Exit edit mode, return to normal mode (Common)

Copy and Delete

KeyAction
x, XIn a line, x deletes one character backward (equivalent to [del] key), X deletes one character forward (equivalent to [backspace] key) (Common)
nxn is a number, continuously delete n characters backward. For example, to continuously delete 10 characters, ‘10x’.
ddDelete the entire line where cursor is (Common)
nddn is a number. Delete n lines downward from cursor, e.g. 20dd deletes 20 lines (Common)
d1GDelete all data from cursor to first line
dGDelete all data from cursor to last line
d$Delete from cursor position to last character of the line
d0That’s number 0, delete from cursor position to first character of the line
yyCopy the line where cursor is (Common)
nyyn is a number. Copy n lines downward from cursor, e.g. 20yy copies 20 lines (Common)
y1GCopy all data from cursor line to first line
yGCopy all data from cursor line to last line
y0Copy all data from character at cursor to beginning of line
y$Copy all data from character at cursor to end of line
p, Pp pastes copied data on next line after cursor, P pastes on line before cursor! For example, if cursor is on line 20, and 10 lines are copied. After pressing p, those 10 lines will be pasted after original line 20, i.e. starting from line 21. But if P is pressed? Then original line 20 will be pushed to become line 30. (Common)
JCombine cursor line and next line data into same line
cRepeat delete multiple data, e.g. delete 10 lines downward, [ 10cj ]
uUndo previous action. (Common)
[Ctrl]+rRedo previous action. (Common)
.Don’t doubt! This is a period! Means repeat previous action. If you want to repeat delete, repeat paste, etc., just press period ‘.’! (Common)

u and [Ctrl]+r are very commonly used commands! One is undo, the other is redo~ Using these two function keys, your editing, hehe! Very happy!

Save Exit

KeyAction
:wWrite edited data to hard disk file (Common)
:w!If file attribute is ‘read-only’, force write to file. However, whether it can be written still depends on your file permissions for that file!
:qLeave vi (Common)
:q!If file was modified but don’t want to save, use ! to force exit without saving file.
:wqSave then exit, if :wq! then force save then exit (Common)
ZZThis is uppercase Z! If file hasn’t changed, exit without saving, if file has been changed, save then exit!
:w [filename]Save edited data as another file (similar to save as)
:r [filename]In edited data, read in another file’s data. That is, add ‘filename’ file content after cursor line
:n1,n2 w [filename]Save content from n1 to n2 as filename file.
:! commandTemporarily leave vi to command line mode to execute command’s display result! For example
‘:! ls /home’Can view /home directory ls output file information in vi!

Note, that exclamation mark (!) in vi often has the meaning of ‘force’~

Line Number Display/Hide

KeyAction
:set nuDisplay line numbers, after setting, will display line number prefix on each line
:set nonuOpposite of set nu, cancel line numbers!

Article Link:

https://alili.tech/en/archive/linux-vim-vi-learning-notes/

# Latest Articles