Linux development tools (1) – vim tool

1. what is vim
1.1 What is vim
Vim adopts the concept of mode editing, that is, it provides multiple modes, and keys have different functions in different modes. You can browse files in normal mode, insert text in insert mode, select lines in visual mode, execute commands in command mode, and more. This may sound complicated at first, but it has a big advantage: Instead of holding down multiple keys at the same time to complete an action, most of the time you just need to press the keys one after the other. The more commonly used operations, the fewer keys are required.

The concepts closely related to schema editing are operators and actions. An operator refers to starting a certain behavior, such as modifying, deleting or selecting text, and then you need to use an action to specify the text area that needs to be operated. For example, to change the text inside parentheses, you need to execute ci( (pronounced change inner parentheses); to delete the content of the entire paragraph, you need to execute dap (pronounced: delete around paragraph).

If you can see old Vim drivers operating, you will find that they use the Vim scripting language like a pianist plays the piano. Complicated operations can be completed with just a few keys. They don’t even have to think about it because it’s muscle memory. This reduces cognitive load and helps people focus on the actual task.

1.2 The difference between vim and vi:
The difference between vi/vim To put it simply, they are both multi-mode editors. The difference is that vim is an upgraded version of vi. It is not only compatible with all commands of vi, but also has some new features in it. For example, syntax highlighting and visualization operations can be run not only on the terminal, but also on x window, mac os, and windows. In our classroom, we will explain according to vim.

2. vim basics
2.1 Whether vim is installed
vim is a pure editor, not an integrated development environment

Generally, cloud servers are installed with vim, so enter vim to check whether it is installed

Installed:

Not Installed:
If not installed, you can install it like this

2.2 vim mode
There are many modes of vim. This blog introduces the three most commonly used modes.

Command mode (Normal mode)
Control the movement of the screen cursor, delete characters, words or lines, move and copy a section and enter Insert mode, or go to last line mode

Insert mode
Only in Insert mode, text input can be done, press “ESC” key to return to the command line mode. This mode is the most frequent editing mode we will use later.

Bottom line mode (last line mode)
Save or exit the file, you can also perform file replacement, find character strings, list line numbers and other operations. In command mode, shift+: to enter this mode. To view all your modes: open vim, enter the bottom line mode directly

vim has 12 modes in total.

2.3 Move up, down, left, and right
hjkl move up and down left and right

h is left, j is down, k is up, l is right

2.4 Buffers, windows, labels
Vim is a text editor. Each time the text is displayed as part of the buffer. Each file is opened in its own unique buffer, and the content displayed by the plug-in is also in their own buffer. The buffer has many attributes, such as whether the content of the buffer can be modified, or whether the buffer is associated with a file, and whether it needs to be saved to disk synchronously.

window is the window on the layer above the buffer. If you want to view several files at the same time or view different locations of the same file, then you will need windows.

Please don’t call them split screen. You can split a window into two, but that doesn’t make the two windows completely separate.

Windows can be split horizontally or vertically and the height and width of existing windows can be adjusted, so if you need multiple window layouts, consider using tabs.

Tabs (tabs) are collections of windows. So use tabs when you want to use multiple window layouts.

In short, if you start Vim without any arguments, you get a tab containing a window that presents a buffer.

By the way, the list of buffers is globally visible, you can access any buffer from any tab.

3.vim basic operation
3.1 How to enter vim
vim filename

If there is this file, it will go directly, if there is no such file, it will create a file and then enter this file

3.2 Mode switching
After entering vim, the default is the command mode, and you cannot write in the command mode

Switch to insert mode: press i in command mode

Switch to bottom line mode: press shift+ in command mode:

Switch to command mode: press ESC in any mode

Switch to replace mode: 1. shift r

2. Press a (the cursor will go back one space) / press o

After writing something in insert mode, how to save and exit if you want to save and exit?

Switch to the bottom line mode and press wq (w is to save, q is to exit)

3.3 View the content of vim (cat)
cat filename

3.4 Call out and remove the line number (set nu/set nonu)
In the bottom line mode, call out and remove the line number, set nu/set nonu

3.5 copy cut paste
copy:
Which line to copy, let the cursor stop on which line press yy

You can also nyy, copy n lines

cut:
dd /ndd truncate one or n lines

Paste:
If you paste, press p on a certain line, it will be pasted on the line below this line

You can also press np to paste n lines

3.6 Revocation
press u to undo

revoke the revocation order

Ctrl+r

3.7 The cursor jumps to the end/jumps to the nth line/jumps forward/backward in units of words
Press gg to go to the beginning of the document

Press n shift g to the nth line

nw, n b: according to the word as a unit, move forward and backward, w backward, b forward

3.8 The cursor jumps to the end/beginning of the current line
Press shift+4 ($) to go to the end of the current line

Press shift+6 (^) to go to the beginning of the current line

3.9 Switch case/replace characters/delete characters
Press and hold shift+~, click to switch a case, keep pressing and switching

Press r + character to replace the position where the limit cursor is with the character

Replace all: %s/replaced character/replaced character/g

Press x to delete one, keep pressing and deleting

Press nx to delete the last n characters

Press n shift x to delete the first n characters

3.10 Create associated headers (vs)/delete windows
Create associated header files: vs filename

Under vim, the line where the cursor is can be written in which line

When there are two windows, how to switch windows and write
Press Ctrl+ww

If there are too many windows, switch to the bottom line mode if you want to exit

:q! (do not save the cursor current page)

Precautions:

1. If you don’t know what mode you are in, no brain esc
2. When exiting, generally save first, and then exit
3. Do not use the mouse and mouse wheel when operating in vim

By hmimcu