Shenyifengtk.github. IO

Unconsciously using Vim has been for some time, also can be considered to really master the use of skills. I can’t remember why I learned the editor at the beginning, nor how I learned to edit files. I only remember that I didn’t quit Vim after pressing all the keys. How much wind and rain does it take for a seed to grow into a towering tree? How many mountains does it take for a stream to merge into the sea? We usually learn the same thing, how many difficulties, how many setbacks, only oneself know best.

Vim model

Normal editors can open files directly for editing, saving, using shortcuts, and so on. Vim also has these functions, but some functions need to be used in a specific mode. It is also because of these patterns that the VIM learning route becomes particularly steep. Vim has a variety of modes, each mode has a specific function, sometimes you need certain functions, deliberately switch to the specified mode operation. The keyboard can be used to arbitrarily switch modes, so that VIM does not rely on the mouse operation, using minimal key combination operation, editing efficiency is greatly improved.

Normal mode

yy
p
dw
a
Insert mode
:
Command mode
Ctrl
v
Select the schema

Insert mode

-- insert --
Esc

Visual model

Select the schema

Command mode

:
:
:q
:wq

The Ex model

Q

Usually in the use of VIM is mainly in the ordinary mode, insert mode, visual mode, command mode for operation, master the basic can be familiar with the use of VIM.

Vim Command shortcut key

Global mobile
  • hMove left ←
  • jMove down ↓
  • kMove up ↑
  • lMove to the right →

Master these shortcut keys, when moving the cursor, the phone will basically not leave the core keyboard area, there is no feeling like W, A, S, D moving the role 🙂.

  • ggMove quickly to the beginning
  • GMove the end of the text quickly
  • CtrlfA PageDown is a PageDown
  • CtrlbPageUp is a PageUp
  • Command mode:nMoves the specified number of rows n
Inline mobile
  • worWMove on to the next word
  • borBMove to the previous word
  • eMove the end of the next word
  • ^Moves to the first non-null character on the current line
  • 0Move to the top of the line
  • $Move to the end of the line
  • f{char} looks for char characters in the line forward, and the cursor moves to the target char
  • F{char} looks for char characters in the reverse direction of the line, and the cursor moves to the target char
  • ;Repeat the forward search for char characters in the line, as above
  • .Search for the last char character in the line, in reverse, same as above
Go into insert mode

To write text, you must also operate in insert mode, and the following command moves from normal mode to insert mode

  • iEnter insert mode and type under the current cursor
  • oCreates a new blank line of insert characters below the current cursor
  • aInsert character behind cursor (append)
  • AInsert at the end of the line, a$=> a
  • IInsert I ^ => I at the beginning of the line
  • OCreate a new blank line insert above the cursor
  • sDeletes the current cursor character and enters insert mode
delete

The d delete character has no meaning on its own, but it can be combined with other commands to do some quick things.

  • xDelete current character
  • ddDelete current row
  • dwDelete entire words from the cursor (Delete word)
  • dbDelete from cursor forward to a character, move backward to delete if
  • dbxReverse delete the entire word
  • d$Deletes from current position to end of line
  • d^Reverse delete to the beginning of the line
  • nddN indicates the number of rows to be deleted. 2dd deletes two rows
  • ddnSame as above
Understand viM operations

Much of Vim’s power comes from the combination of operators and action commands. In this section, we’ll see how it works and consider its implications – ViM Practical Tips 2nd edition

Operator + action command = operation

The d{motion} command operates on a character DL, a whole word daw, or a whole paragraph dap, and its scope is determined by the action command. Similarly, c{motion}, y{motion}, and other commands are collectively called operators.

The command use
c Modify (change)
d Delete (delete)
y Copy to register
g~ Reverse case
gu Reverse lowercase
gU Inversion of the capital
> Increase indent
< Decrease the indentation
= Automatic indentation

For example, we already know that daw deletes the entire word and gU reverses capitalization, so we can use gUaw to uppercase the entire word and gUap to uppercase the entire paragraph

Learn to draw inferences
  • eaInsert after the word
  • cwModify the whole word
  • c$Modify content from the cursor to the end of the line
  • c^Modify content from the cursor to the beginning of the line
  • ywCopies the characters from the cursor to the end of the word
  • y^Copy the content from the cursor to the beginning of the line
  • y$Copy content from the cursor to the end of the line
  • nyyCopy n lines

Vim copy and cut operation

Since the shortcut key of copy in VIM has been occupied by change, y can only be used to represent copy (yank), and P can be used to paste (put). D is actually to cut the content into the register, and cutting realizes deletion. DDP appears to swap the positions of the two lines. In fact, the current line is cut into the register and p is pasted below the cursor. Mainly remember to use the command Y + action command or D + action command, both of which are to temporarily store the text in the register and paste it with P or P. For a small demo exercise, replace the function argument foo below with the variable field.

let field = method();
fun(foo)
Copy the code

First use yw to copy the field, move it down into foo, dw to delete foo, and P to paste. But you’ll notice that the paste structure is still foo. This is because DW puts the deleted text into a register, overwriting the copy variable above. We usually copy, cut text will be temporarily stored in the nameless register.

Knowing the nameless register

Instead of using a single register for copy, cut, and paste, Vim provides multiple sets of registers for these operations. When using delete, copy, and paste commands, you can explicitly specify one of them to operate on.

Delete, copy, and paste all use registers in VIm. Registers can be named with “{registerName} “. Usually we use dd, YY command, do not name the register name default use anonymous register. If you do not specify a register to use, Vim defaults to using an anonymous register, which can be expressed in double quotes (see :h quote_quote). The nameless register can be represented by “”, such as “” YY equals YY, “”p equals P

Digital register

“[0-9] indicates the numeric register, and the smaller the number, the earliest contents in the register. For example,” 0 indicates the first entry of register text,”1 indicates the second entry of register content, similar to the value stack structure, advanced after out arrangement. You can use :reg to view the contents of a register.

"0P

System register

In Windows, you can copy text anywhere with Ctrl/C. Can VIm paste content that is not copied in VIM? Of course it can.” + represents the system register, which can read the contents of the system stickboard outside the system VIM, paste the contents, and copy them to the system stickboard. You need to install vim-Gnome on Ubuntu to use system registers properly.

paste

  • pPastes the text in the register after the cursor
  • PPastes the text in the register before the cursor

Use the paste command before the + register name, you can directly use the special register content, such as “+p paste system clipboard content.

Use

+ register name to paste character text in insert mode. For example, you can use

” to paste the contents of the nameless register.

Part of this article refers to “ViM practical skills 2nd edition”, interested students can buy a look, really very good oh!