Easy Tutorial
❮ Linux Comm Sudo Linux Comm Finger ❯

Linux vi/vim

All Unix-like systems come with the built-in vi text editor, while other text editors may not necessarily be present.

However, the vim editor is currently used more frequently.

Vim has the capability for program editing, actively distinguishing the correctness of syntax through font colors, which is convenient for programming.

Related article: The Most Comprehensive Vim Shortcut Cheat Sheet — From Beginner to Advanced


What is Vim?

Vim is a text editor derived from vi, with extensive features for code completion, compilation, and error jumping, making it widely used among programmers.

In simple terms, vi is an old-fashioned word processor with comprehensive features, yet there is room for improvement. Vim, on the other hand, is a very useful tool for program developers.

Even the official Vim website (https://www.vim.org/) states that Vim is a tool for program development rather than a word processing software.

Vim keyboard diagram:


Usage of vi/vim

Basically, vi/vim has three modes: Command mode, Insert mode, and Last line mode. The functions of these three modes are:

Command mode:

When you start vi/vim, you enter Command mode.

In this state, keystrokes are recognized by Vim as commands rather than character input. For example, pressing 'i' will not input a character but will be treated as a command.

Here are some commonly used commands:

To edit text: Start Vim, enter Command mode, press 'i' to switch to Insert mode.

Command mode only has some basic commands, so more commands need to be entered using Last line mode.

Insert mode

Press 'i' in Command mode to enter Insert mode.

In Insert mode, you can use the following keys:

Last line mode

Press ':' (English colon) in Command mode to enter Last line mode.

Last line mode allows input of single or multi-character commands, with many available commands.

In Last line mode, basic commands include (omitting the colon):

Pressing the ESC key can exit Last line mode at any time.

In short, we can think of these three modes as represented by the following icons:


Example of using vi/vim

Entering Normal mode using vi/vim

If you want to use vi to create a file named tutorialpro.txt, you can do this:

$ vim tutorialpro.txt

Simply type vi filename to enter vi's Normal mode. Remember, always include the filename after vi, whether the file exists or not!

Press 'i' to enter Insert mode (also known as Edit mode) and start editing text

In Normal mode, pressing 'i', 'o', 'a', etc., can enter Insert mode!

In Edit mode, you will notice the –INSERT– prompt in the bottom left status bar, indicating that you can input any characters.

At this point, except for the ESC key, all other keys on the keyboard can be considered as regular input buttons, allowing you to edit freely.

Press the ESC button to return to Normal mode

Now, suppose I have finished editing as shown above, how do I exit? Yes! That's right! Just press the ESC button! You will immediately notice that the –INSERT– in the bottom left corner disappears!

In Normal mode, press :wq to save and exit vi

OK, we need to save the file, and the command to save and exit is simple, just type :wq to save and leave!

OK! This way, we have successfully created a tutorialpro.txt file.


vi/vim Key Descriptions

Besides the simple examples of 'i', 'Esc', ':wq', there are actually many more keys available for use in vim.

Part One: Cursor movement, copy and paste, search and replace available in Normal mode

Cursor movement methods
h or left arrow key (←) Move cursor left by one character
j or down arrow key (↓) Move cursor down by one character
k or up arrow key (↑) Move cursor up by one character
l or right arrow key (→) Move cursor right one character
If you place your right hand on the keyboard, you'll find that hjkl are arranged together, so you can use these four keys to move the cursor.

If you want to move multiple times, for example, down 30 lines, you can use the combination "30j" or "30↓", just add the number of times you want to move (digit) and press the action key! | | [Ctrl] + [f] | Move screen 'down' one page, equivalent to [Page Down] key (commonly used) | | [Ctrl] + [b] | Move screen 'up' one page, equivalent to [Page Up] key (commonly used) | | [Ctrl] + [d] | Move screen 'down' half a page | | [Ctrl] + [u] | Move screen 'up' half a page | | + | Move cursor to the next line with non-space characters | | - | Move cursor to the previous line with non-space characters | | n<space> | n represents a 'number', for example, 20. Press the number followed by the space key, and the cursor will move right n characters in the line. For example, 20<space> will move the cursor 20 characters to the right. | | 0 or [Home] key | This is the digit '0': Move to the first character of the line (commonly used) | | $ or [End] key | Move to the last character of the line (commonly used) | | H | Move cursor to the first character of the top line on the screen | | M | Move cursor to the first character of the middle line on the screen | | L | Move cursor to the first character of the bottom line on the screen | | G | Move to the last line of the file (commonly used) | | nG | n is a number. Move to the nth line of the file. For example, 20G will move to the 20th line of the file (can be used with :set nu) | | gg | Move to the first line of the file, equivalent to 1G! (commonly used) | | n<Enter> | n is a number. Move the cursor down n lines (commonly used) |

Search and Replace
/word Search for a string named word below the cursor. For example, to search for the string vbird within the file, type /vbird.

(commonly used) | | ?word | Search for a string named word above the cursor. | | n | This n is an English key. It represents repeating the previous search action. For example, if we just performed /vbird to search for the string vbird downwards, pressing n will continue to search for the next instance of vbird downwards. If we performed ?vbird, pressing n will continue to search for vbird upwards! | | N | This N is an English key. It is the opposite of n, performing the 'reverse' of the previous search action. For example, after /vbird, pressing N will search for vbird 'upwards'. | | Using /word with n and N is very helpful! It allows you to repeatedly find some keywords you are searching for! | | :n1,n2s/word1/word2/g | n1 and n2 are numbers. Search for the string word1 between the n1th and n2th lines and replace it with word2! For example, to search for vbird and replace with VBIRD between lines 100 and 200: <br> ':100,200s/vbird/VBIRD/g'. (commonly used) | | :1,$s/word1/word2/g or :%s/word1/word2/g | Search for the string word1 from the first line to the last line and replace it with word2! (commonly used) | | :1,$s/word1/word2/gc or :%s/word1/word2/gc | Search for the string word1 from the first line to the last line and replace it with word2! And display a prompt for user confirmation before replacing! (commonly used) |

Delete, Copy, and Paste
x, X In a line, x deletes a character to the right (equivalent to [del] key),

X deletes a character to the left (equivalent to [backspace] key, which is the backspace key) (commonly used) | | nx | n is a number, delete n characters continuously to the right. For example, to delete 10 characters continuously, '10x'. | | dd | Cut the entire line where the cursor is located (commonly used), can be pasted with p/P. | | ndd | n is a number. Cut n lines down from the cursor, for example, 20dd cuts 20 lines (commonly used), can be pasted with p/P. | | d1G | Delete all data from the cursor to the first line | | dG | Delete all data from the cursor to the last line | | d$ | Delete from the cursor to the last character of the line | | d0 | This is the digit 0, delete from the cursor to the first character of the line | | yy | Copy the line where the cursor is located (commonly used) | | nyy | n is a number. Copy n lines down from the cursor, e.g., 20yy copies 20 lines (commonly used). | | y1G | Copy all data from the cursor's current line to the first line. | | yG | Copy all data from the cursor's current line to the last line. | | y0 | Copy data from the cursor's current character to the beginning of the line. | | y$ | Copy data from the cursor's current character to the end of the line. | | p, P | p pastes the copied data below the cursor, while P pastes above the cursor. For example, if the cursor is on line 20 and 10 lines have been copied, pressing p will paste them starting from line 21, whereas pressing P will push the original line 20 to become line 30. (commonly used) | | J | Combine the data of the current line with the next line into one line. | | c | Repeat deletion of multiple data, e.g., [10cj] deletes 10 lines down. | | u | Undo the previous action. (commonly used) | | [Ctrl]+r | Redo the previous action. (commonly used) | | This u and [Ctrl]+r are very useful commands! One is for undo, and the other is for redo. Using these function keys, your editing will be quite enjoyable! | | . | No doubt, this is the decimal point! It means to repeat the previous action. If you want to repeat actions like deletion or pasting, just press the dot '.' (commonly used). |

Part II: Buttons for Switching from Normal Mode to Insert Mode

Entering Insert or Replace Mode
i, I Enter Insert mode: <br>i starts input at the current cursor position, I starts at the first non-space character of the current line. (commonly used)
a, A Enter Insert mode: <br>a starts input at the next character from the current cursor position, A starts at the last character of the current line. (commonly used)
o, O Enter Insert mode: <br>o creates a new line below the current cursor line, O creates a new line above the current cursor line. (commonly used)
r, R Enter Replace mode: <br>r replaces only the character at the cursor once; R continues to replace text at the cursor until ESC is pressed. (commonly used)
In vi, you will see '--INSERT--' or '--REPLACE--' at the bottom left corner of the screen, indicating the mode.
[Esc] Exit Insert mode and return to Normal mode. (commonly used)

Part III: Buttons for Switching from Normal Mode to Command Mode

Commands for Saving and Exiting
:w Write the edited data to the hard drive file. (commonly used)
:w! Force write to the file if it is read-only. Whether it can be written depends on the file permissions.
:q Exit vi. (commonly used)
:q! Force exit without saving if the file has been modified.
Note that the exclamation mark (!) often implies 'force' in vi.
:wq Save and exit, :wq! forces save and exit. (commonly used)
ZZ This is the uppercase Z. It saves the current file if modified and exits. Equivalent to save and exit.
ZQ Force exit without saving. Equivalent to :q!.
:w [filename] Save the edited data as another file (similar to 'Save As').
:r [filename] Insert the content of 'filename' into the current file after the cursor's line.
:n1,n2 w [filename] Save the content from line n1 to line n2 into 'filename'.
:! command Temporarily exit vi to execute 'command' in command mode, e.g., ':! ls /home' to view the file information in /home.
vim Environment Changes
---
:set nu Display line numbers, showing the line number at the beginning of each line.
:set nonu Opposite of set nu, disables line numbering.

Note: Numbers are significant in vi/vim. They often represent repetitions or specific line references. For example, to delete 50 lines, you would use '50dd', right? The number precedes the action; if I want to move down 20 lines, that would be '20j' or '20↓'.

❮ Linux Comm Sudo Linux Comm Finger ❯