In this chapter we learn about registers/jump lists and their applications: copy/paste/undo/redo/jump, as well as a multi-line edit that is supported by all GUI editors.

👁️ Pre-configuration tasks

In this chapter we shall make use of the abilities we have learned. First, let’s review how to use native help documents

["x]y{motion} # y{Visual}["x]y {motion} # y{Visual}["x]y {motion} # y{Visual}["x]y {motion} # y <C-]> # document jump <C- O > # jump back to Y/y # The lower case and upper case of a command are often related, such as o down on another line, o up on another line. {count}{motion} # execute {count}{motion}Copy the code

Using these skills can help us read documents well.

🧳 register

A register is a unit of a program used to store specific data. The current register can be viewed using the :reg command. Vim uses [” x] to represent the register, and x represents the register name.

There are ten registers:

The text processed by the D/C/S /x/ Y command is stored in the unnamed register in addition to being processed by other registers, as if the unnamed register points to the last register in use. 2. The 10 numeric register [" 0]-[" 9] and the text related to the yank/delete command are stored in the numeric register. The 0/1 registers store the latest text generated by yank and delete respectively, and the text is passed in sequence 2/3/4... Nine, up to nine. Delete register [" -] store delete text less than one line of 4.26 letters register, case insensitive [" a "-[" z] Store custom text, lowercase letters overwrite, uppercase letters add 5. Three read-only registers [",] [".] [" %] store files, current lines, current commands, etc. Can only be used in conjunction with p/ p command 6. Optional cache register [" #] stores the name of the optional file in the current window 7. Expression register [" =] stores the command string 8. Select the area register [" *] [" +] stores the selected text. *y can copy the selection to the system clipboard 9. The last search pattern register [" /] stores the pattern matching string from the last searchCopy the code

The vim register you can think of as an in-memory caching mechanism. It is similar to buffers in some programming languages, but it is very similar to registers in assembly.

:reg command output format

Type   Name   Content
c       "0 123 l "" aaaCopy the code

Macro recording and playback

This is the way to achieve great automation capabilities. We’re going to use these two commands

q                 Q ["x]{motion}q can define your motion into the X register
@                 Call the register and execute the commands in the register
Copy the code

Simplest macro recording: add 100 lines.

qao<Esc>q         # q["x]{motion}q Store the o
      
        command to the register
      
100@a             # {count}@a calls the command in register A count times
Copy the code

From there, you can get a little more complicated and add a hundred lines and number them

o1.<Esc>          Write 1 #.
qayypj_<C-a>q     # Copy the current line to the next line, move to the head of the new line, and increment the number. The operation is recorded in register A
100@a             # get results
Copy the code

Register call

In addition to the registers we will use in VimScript in the future, the normal editing behavior also supports shortcut keys.

<C-r>0                  Read the register information and paste it to the cursor
Copy the code

This means that you can invoke the contents of the specified register with the command in Insert mode. The above text indicates that we took the [” 0] register and got the contents of the last yank(vim copy).

😼 Copy and paste

Now let’s look at the copy and paste command. Copy in vim, yank, y copy command actually saves the text to a register, copy command supports specific register copy.

["x]y{motion} # If no text can be copied, the motion is copied at the [x] register. ["x]Y # copy cursor to end of line without newline {Visual}["x]Y # copy selection text {Visual}["x]Y # Copy selection line Multiple lines ["x]p # paste the contents of the register after the cursor {count} multiple times ["x]p # paste the contents of the register before the cursor {count} multiple timesCopy the code

When you don’t know which register is storing data, you can use **:reg to check the register status and paste the corresponding contents through [” x]p**. According to the register concept we discussed in the first section, p is actually an abbreviation of “p” ~, which means that P will get the last yank or delete.

The concept here is that copy and paste in VIM is relative to registers. As you can see, commands are defined register-based, so you can customize commands to implement clipboard copy and paste in the operating system.

{Visual}"*y # copies the selection to the [*] register. This register is also copied to the operating system {Normal}"*y{motion} # Normal mode execution, copy the result of motion into the register, also copy to the operating system "*p # take out the contents of the [*] register and pasteCopy the code

Use “{Normal}”*y{motion}” to copy two lines to the clipboard:

The [*] [+] register is what you use to interact with the operating system’s clipboard. So you can map it

Vmap Y "* Y "in Visual mode Y command to copy the selection to the clipboard nmap PP "*p" PP command to paste the clipboard contents in Normal modeCopy the code

👨🍳 Undo/redo

In the editor, the generic undo/redo English corresponds to undo/redo. Vim just uses the first letter of both words. You can also use {count} to indicate the number of undo/redo requests.

U # undo previous action < c-r > # undo previous actionCopy the code

In addition to using u/, your text editing status is recorded by VIm, so you can move through any state.

:undol[ist] # view the list of undos available g- # to enter the previous edit state :earlier g+ # To enter the next edit state :after :earlier {count} # {count {N}s # N seconds before the state, the same can be used as s/m/h/d seconds/minute/hour/day :after{count} # {count} s # N seconds after the state, the same can be used as s/m/h/d seconds/minute/hour/dayCopy the code

But in everyday code, I use u/ combination the most

👯♂️ multi-line editing

Earlier we mentioned multi-line editing. Use the command to enter the block selection mode, requiring multi-line editing can be

  1. Enter block selection mode
  2. {motion} selects the region
  3. I /I entry insert
  4. [> or < C – exit

Note here that the exit does not check for abbreviations and does not execute an automatic trigger event for InsterLeave, so you must exit with or <C-[>

✨ jump

Jump list – Jumps

In the VIM world, cursor positions are also recorded, so you can view and call historical positions. The jump list is like a function call stack where you can read the contents of a function in JumpIn/JumpOut mode. The logic behind the jump list is motion. Motion triggers cursor transfer, and the position before and after the transfer is recorded by the jump list, generating a call chain.

:ju[MPS] # To view the current jump list :cle[arjumps] # Clear the current jump listCopy the code

The jump list is displayed as follows

jump    line    col    file/text
 3       1       0      some text
 2       70      0      another text
 1      1154     23     end.
Copy the code

The columns represent the jump list number/cursor line number/cursor column number/corresponding file or text.

You can use the following two combinations to jump through the jump table

< c-O > # jump, jump backCopy the code

The following common commands can be used to jump (of type text motion), and you can use a combination of /

[[# section redirect]] # section redirect {# paragraph redirect} # paragraph redirect (# # jump to the next sentence jumpCopy the code

In fact, you can think of it as a jump list that records large jumps to the cursor. Now all you need to do is keep that in mind and use the two commands in practice.

change list jumps

At the same time, all changes are recorded in the list, forming the change record

:changes # View the current list of changesCopy the code

A change list is similar to a jump list in that the line number indicates the cursor position where the change occurred.

change   line  col   text
 3         16   0     abcdef
 2         35   3     abc
 1         7    3     aadf6
Copy the code

Insert (I), paste(p), change(c), and delete (x/ x)

You can use the following combination to jump

g; # {count} previous change position g, # {count} later change positionCopy the code

🗡 ️ summary

Today we learned a few important concepts and some advanced editor manipulations, so go ahead and apply them

  1. Register. Basic concept of register, storage and call
  2. Jump. Jump list, change list, jump
  3. Multi-line editing.
  4. Undo/redo
  5. Copy and paste

With these concepts, you can do richer operations, especially registers, which allow you to store/fetch text at will.

Next time we’ll talk about search/replace (search and replace takes a little bit more depth)