Introduction to Registers in VIM

Source: Internet
Author: User

Originally in vim paste from other places copied from the text, always use SHIFT + inert, then think, can not directly like P paste, do not have to enter the insertion mode and then paste. Later read the "Vim practical skills" on the introduction of VIM register, found this inside great article, so today specially to share with you.

First of all, the registers in vim are roughly browsed, which are mainly divided into the following parts:

1. Nameless Registers (")

2. Copy private registers (0)

3. System Clipboard (main clipboard under X11 Windows System) (*)

4. Black Hole Register (_)

5. Name register (A-Z)

6. Expression Register (=)

7. Some other read-only registers ("/register Special)

Before we learn more about these registers, we also need to know a few of the register-related commands:

0. Various copy and paste command instructions

The d command mentioned in this article, they command , and theC command refer to the set of commands associated with this operator, such as the following commands associated with the Y operator:

1 yy        // Copy the current line 2 yw     // the word under the current cursor 3 yit        // Copy the contents of an HTML tag 4 yft        // Copy all content between the cursor on the current line and the first T 56 ...        

1. Using registers in normal mode

When you execute the paste (p) command, or copy (y) and cut (X,s,d,c and their caps) commands, you can precede the "{Register}" (where that {register} represents the name of the register) so that we can use the appropriate registers, if not added, The default is to use the nameless register.

For example,% represents the file name register, which is currently being edited, and I now have a file file2 open and in normal mode, as shown in:

  

At this point, if I enter the following:

1 " %p 2 3 //Indicates the contents of the file name register are pasted into the current line

The contents of the file become like this:

  

2. Using registers in insert mode

In insert mode, when we press the <CTRL> + R key, plus the corresponding register name, you can insert the contents of the register.

For example, = represents an expression register, such as I enter the following command in VIM:

1 i                     #进入插入模式 2 <c-r>=           #按下 <ctrl>+r Key, and then press the equal sign, you can enter an expression now            

At this point, the Vim page looks like this:

  

Then, at this point the cursor jumps to the bottom of the VIM task bar (specifically this place I do not know what is called, that is the ex mode input command of the place), here you can enter the Vim script command, for example, I input a 2+3. Then the result of the operation 5 will automatically be added to the main window, the cursor will return to the main window, as shown in:

  

3. View the contents of the current register

By entering the reg or DIS command in ex mode, you can view the contents of all current registers. The results of the run are as follows:

  

Ok, after understanding these common commands about registers, let's take a look at the various registers in vim.

  

The first is the nameless register, which is identified by a double quotation mark ("), which is the largest register we have contacted, and if not specifically specified, copy (y), cut (x,s,d,c and their caps), paste (P) command will store the contents in this register, Or read the contents from this register.

  

The second is to copy the private register, that is, the use of the y command to copy the content will be stored in this register, such as a text:

1 printf (""); 2 printf ("world!\n");

I first put the cursor on the first line, press the YY command, copy the first line, and then move to the second row, press the DD command, delete the second row. At this point, if we want to paste the contents of the first line, it is not possible to press p directly, because the p will refer to the contents of the nameless register, and its content has been overwritten by the contents of the second line, so this is the turn to copy the dedicated register, at this time if you press this command:

1 " 0p

In this case, the contents of the copy private register will be pasted. Here directly paste the result diagram may not see what effect, then I will post the second line with DD Delete all the contents of the register to show you:

As can be seen from here, the Nameless register holds the content that the DD command deletes, while the copy private register (0) holds the contents of the y command copy.

  

The third is the system Clipboard Register (+) and the main clipboard register (*) of the X11 Windows system. Personally think this is more commonly used, so the first to say. As we all know, any operating system has a clipboard, and Vim is to use the + register to represent the clip, I often encounter the situation is to copy a piece of text in the browser, and then need to paste in vim, I feel that this time the + register to work. For example, when you visit a blog to see someone else's code, as follows:

1 def Main (): 2     Print " Python is fun " 3 4 Main ()

The direct copy is copied to the system Clipboard, when you open vim to paste into vim, then only need this command is good

"+p

This will put the contents of the system Clipboard directly into the vim buffer, as shown (this is the Windows Gvim, so there is no main clipboard this register, + and * represents a register):

  

The point to be declared is that the Clipboard needs vim to compile the time to add clipboard this option, want to see their vim has this option, just open vim, enter the version command in the EX mode, you can see your VIM branch does not support this feature, as shown in:

  

There is also a register of the main clipboard, which is something that exists in the X11 Windows system under Linux, which stores the last highlighted content. This is because I did not add the Clipboard option when I built the VIM on Linux, so I did not show it.

The fourth thing to illustrate is the black hole register, as the name implies, the function of this register and the/dev/null device is very similar, that is, all the input is swallowed up ^ o ^. Usually we often use the x command to delete a character, with the DD command to delete a row, in fact, these are not deleted, is the clip, the actual deletion should be these commands:

1 " _dd         //delete a line 2"_x          //Delete the character under the cursor

  

The fifth to illustrate is the name register, this is a total of a-z26 registers, respectively, in English letters to express. The main thing to say is the difference between uppercase and lowercase letters, when writing to the register (that is, when copying or cutting), the capital letter is to append the content that is currently being copied to the register, while the lowercase letter indicates that the content that is currently being copied is overwritten with the original contents of the register. This can be analogous to the ">" and ">>" commands in data flow redirection.

For example, there is now a file file2, and the content is this

1 printf (""); 2 printf ("world!\n");

The contents of the a register are just the beginning:

  

After I executed the "ayy" command on the first line, the contents of the a register became so that the contents of the first line were appended to the Register:

  

Then I execute the AYY command on the second line, and the contents of the a register become so that the original contents of the register are overwritten:

  

The sixth thing to say is the expression register, which has been shown in the previous example, you can press "= or insert mode in normal mode to press <c-r>,= to enter the mode of editing vim expression, the results of the expression will be inserted into the buffer of vim." This is something I usually use to do operations like this:

  

The seventh thing to say is some registers that hold specific information, mainly the following:

7.1 Current file name register (%)

7.2 Rotate file name Register (#)

7.3 Last executed ex command (:)

7.4 Last found keyword (/)

This example is the following, I opened two files File1 and file2, and then switched to File2, and made a search, found the Hello keyword, executed two reg command, the second reg command execution results such as:

  

The last four lines correspond to the four registers we said above, it is important to note that the keyword register (/) is more special, it can be changed by the Let command, the specific execution command is as follows:

Let @/="the"

The above command is to turn the search keyword into the one.

 

OK, this is all, if you want to know more about the contents of the registers in Vim, you can refer to the 10th chapter of Vim's practical tips, or see the help documentation for the registers in vim.

: Help Registers

Introduction to registers in Vim

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.