Development under Linux environment - Vim editor

Knowledge reserve:

  • Vim is a text editor developed from vi.

  • Its code completion, compilation, error jumping and other convenient programming functions are particularly rich, and are widely used among programmers.

  • Tied with Emacs as the favorite editor for Unix-like system users.

  • Vim common mode:

  • interactive mode, insert mode, command mode, visual mode

Introduction to common modes of Vim

  • Interactive mode , Vim's default mode (normal mode), will enter this mode every time you run a Vim program.

Mode features:
1. In this mode, you cannot enter text;
2. It allows us to move between texts, delete a line of text, copy and paste text, jump to a specified line, undo operations, and so on.
在 Vim 的交互模式下,我们可以在文本中移动光标。
• h 向左移动一个字符
• j 向下移动一个字符
• k 向上移动一个字符
• i 向右移动一个字符
当然也可以使用四个方向键进行移动,效果是一样的。

跳至行首和行末
行首:在交互模式下,为了将光标定位到一行的开始位置,只需要按下数字键 0 即可,键盘上的 Home 键也有相同效果。
行末:在交互模式下,为了将光标定位到一行的末尾,只需要按下美元符号键 $ 即可,键盘上的 End 键也有相同效果。

在交互模式下,按字母键 w 可以一个单词一个单词的移动
  • Insert mode, the mode of the text editor.

Mode features:
1. Press the letter key i (i, I, a, A, o, O can all enter the insert mode, but the positions are different), to exit this mode, just press the Esc key.
2. i, I enter the input mode Insert mode: i means "input from the current cursor position", I means "start input at the first non-space character in the current line"; 3. a, A enter the input mode
Insert mode: a means "start input from the next character where the cursor is currently located", A means "start input from the last character of the line where the cursor is located"; 4, o, O
enter the input mode Insert mode: o means "at the current Enter a new line at the next line where the cursor is located"; O means input a new line at the previous line where the cursor is currently located.
  • Command mode, also known as bottom-line command mode, in this mode you can run some commands such as "exit", "save", and other actions.

You can also use this mode to activate some Vim configurations, such as syntax highlighting, displaying line numbers, etc. You can even send some commands to the terminal command line, such as ls, cp. To enter command mode, first enter interactive mode, then press the colon key.


Getting started with Vim

open a file

After entering vim in the terminal command line and pressing Enter, Vim will be started.

You can also use Vim to open a file, just add the file name after vim. Like vim file.name, if the file does not exist, it will be created.

exit file

  • In interactive mode, press the colon key: to enter the command mode, and then press the q key to exit.

  • If you modify the file before exiting, and you want to exit Vim directly with :q, then Vim will display a red letter indicating the error message. At this point we have two options:

  • save and exit with :wq or :x;

  • Quit without saving :q! .

delete single/multiple characters

  • In interactive mode, position the cursor on a character you want to delete, press the letter key x to delete the character.

  • To delete multiple characters at once, just enter the number before pressing the x key.

copy word/line

  • Copy line: Press y twice to copy the line where the cursor is to memory, similar to dd, dd is used to "cut" the line where the cursor is.

  • Copy word: yw will copy a word.

  • Copy to the end of the line: y$ is to copy all characters from the cursor position to the end of the line.

  • Copy to the beginning of the line: y0 is to copy all characters from the cursor position to the beginning of the line.

cut or delete word/line

  • Cut a line: Double-tap d to cut the line where the cursor is.

  • Cut multiple lines: For example, enter the number 2 first, and then press dd, it will cut two lines starting from the line where the cursor is.

  • Delete a word: place the cursor on the first letter of a word, then press dw.

  • Delete multiple words: For example, press the number key 2 and then press dw to delete two words.

  • Delete from the cursor position to the beginning of the line: d0.

  • Delete from the cursor position to the end of the line: d$.

paste

  • If you used dd or yy to cut and copy it before, you can use p to paste it. You can also use the number + p to represent multiple copies.

replace a character

  • In interactive mode, place the cursor on the character you want to replace. Press the r key, and then enter the character you want to replace.

undo operation

  • To undo the most recent modification, just press the u key.

  • If you want to undo the last four changes, you can press 4, then press u.

redo

  • To cancel undo, that is, to redo the previous modification, use ctrl + r.

jump to the specified line

In the file edited by Vim, each line has a line number, and the line number starts from 1 and increases one by one.

The line number is not displayed by default. If you need to display it, you can enter the command mode and enter set nu. If you want to hide the line number, use set nonu.

  • Jump to the specified line: number + gg, such as 7gg, means to jump to the 7th line.

  • To jump to the last line, press G .

  • To jump to the first line, press gg


Vim advanced operation

search mode

  1. In interactive mode, press the / key to enter search mode.

  1. Enter the string you are looking for and press Enter. The cursor will jump to the next found match in the file.

  1. If the string does not exist, "pattern not found" will be displayed.

  • n jumps to the next match;

  • N jumps to the previous match.

⚠️The search with a slash is to search from the current cursor to the end of the file. If you want to start from the current cursor and search to the beginning of the file, use ? , of course, you can also press gg to jump to the first line in the Do a full-text search.

find and replace

  • Replace the first matching string on the line where the cursor is located (syntax)

#语法
:s/旧字符串/新字符串

# 实例
:s/one/two
  • Replace all old strings on the line where the cursor is located with new strings:

# 语法
:s/旧字符串/新字符串/g
  • Replace all strings in the first few lines to the first few lines:

# 语法
:n,m s/旧字符串/新字符串/g

# 实例
:2,4 s/one/two/g
  • Full text replacement:

# 语法
:%s/旧字符串/新字符串/g

Merge files

  • You can use colon+r ( :r ) to insert the content of a file at the cursor.

  • :r filename # You can use the Tab key to automatically complete the path of another file.

Split Screen

With split screen, you can open several files at the same time. After split screen, each piece of the screen is called a viewport, which means "viewport".

  • Horizontal split screen: sp file name

  • Vertical split screen: vsp filename

Shortcut keys in split screen mode

  • Ctrl + w plus Ctrl + w means to move the cursor from one viewport to another viewport;

  • Ctrl + w plus "arrow key", you can move to the next viewport in this direction;

  • Ctrl + w plus a + sign means to expand the current viewport;

  • Ctrl + w plus - sign means to reduce the current viewport;

  • Ctrl + w plus = sign means average current viewport;

  • Ctrl + w plus r key will reverse the position of the viewport;

  • Ctrl + w plus q will close the current viewport;

  • Ctrl + w plus o key will close all viewports except the current viewport;

Run an external command: !

You can run some terminal commands in Vim, as long as you enter :! first, followed by the command name.

For example:

:!ls # 在Vim中打开的文件所在的目录运行ls命令

visual mode

We only talked about the three modes of Vim before, but there is actually another mode called visual mode.

There are three ways to get into it (all start in interactive mode):

  • v Character visual mode, after entering, select the character with the direction key, and then press the d key to delete the selected character.

  • V line visual mode, after entering, the line where the cursor is located is selected by default, and then press the d key to delete the line.

  • Ctrl + v blocks the visual mode, which is the most useful function of the visual mode. With the d and I keys, you can delete the selected content and insert the content.

Select multiple rows at the same time and insert content at the head of the selected row:

1. ctrl + v to enter block visual mode

2. Use the arrow keys to select (up, down, left, and right) assuming that 5 lines are selected

3. Enter the I key to insert multiple lines at the same time

4. After the insertion is complete, press the esc key twice continuously to insert the same character in multiple lines at the same time

Operation keys after entering visual mode:

  • The d key means to delete the selection;

  • I key, means insert before selection;

  • u key, which means that the selection is changed to lowercase;

  • U key, which means that the selection becomes uppercase;


Vim configuration

option parameter

Create a Vim configuration file .vimrc in your home directory ( cd ~ ).

.vimrc

set number      显示行号
syntax on       激活语法高亮
set showcmd     实时看到输入的命令
set ignorecase  搜索时不区分大小写
set mouse=a     激活鼠标,用鼠标选中时相当于进入可视模式

Vim can be made into its own IDE through personalized configuration and so on.

You can also search for some powerful Vim configuration files on github.

Guess you like

Origin blog.csdn.net/weixin_41606115/article/details/129019279