Vim register Usage Details

Source: Internet
Author: User
Tags processing text uppercase letter
Registers are used to store specific content in VIM operations. Most normal commands and some ex commands can specify the registers associated with operations. Registers are also special variables in Vim, so they can be accessed in command lines and scripts to implement some very useful functions. Vim has many different types of registers with different roles and functions. If it is used flexibly, it makes editing work easy and efficient. This article describes the functions and usage of various registers based on the common problems in Vim.

1. Common functions in editing operations

: H V_p
: H g @
: H redo-register

The most common problems in Vim are digit registers. When no register is specified, the content of the copy operation is saved to "0. The content of the delete operation is" pressed "to" 1, and the content of the original "1 is transferred to" 2, so far, the original "8 to" 9, the original "9 content is lost. If the operation registers such as "Ayy and" BDD are specified, the preceding numeric registers are unaffected (for some exceptions, see the vim Manual ). The Untitled register "" saves the last copy or delete operation, whether or not the register is specified. There is also a special "black hole" register "_. When it is specified to be deleted, any registers including" "will not be affected. Of course, you can't take the material p that falls into the black hole.

[Example 1] Copy, delete, and paste
This is a problem that often bothers new users of VIM: When copying a word (YW) and preparing to replace another word, the natural idea is to delete (DW) and paste (P ), however, DW has updated "" to the deleted word, and the content of P will not be copied. There are several ways to choose from:
A. P first and then DW. The problem is to locate the part to be deleted. You can try it with GP/GP, which has the same function as P/P, but the cursor stays behind the pasted text to facilitate subsequent deletion;
B. Transfer the deleted content to the black hole ("_ DW), and then P;
C. Specify the copy content ("0 P );
D. Use the switch feature (VWP) of the p command in visual mode to paste the content of the specified register and then delete the selected text. The keyboard input in this method is easier than that in B and C.
E. Use the S command of the following map (the original function of S is the same as that of CC, so I changed it ).
"Replace text with unnamed register (in all modes)
Function! Replacewithunamed (type)
Let paste_save = & Paste
Let & Paste = 1
If a: TYPE = 'line'
Silent EXE "normal! '[V']"
Elseif A: TYPE = 'block'
Silent EXE "normal! '[\ <C-V>']"
Elseif A: TYPE = 'Char'
Silent EXE "normal! '[V']"
Else
Silent EXE "normal! '<". A: type."'>"
Endif
Silent EXE "normal! \ "_ C \ <C-R>" \ <ESC>"
Let & Paste = paste_save
Endfunction
NMAP <silent> S: Set opfunc = replacewithunamed <CR> G @
Vmap <silent> S: <C-U> call replacewithunamed (visualmode () <CR>
This method conforms to the command-motion operation process, and the content in "" does not change. It is very convenient to replace multiple texts with the same content.

[Example 2] fast Reverse Order
Sometimes we need to swap a series of content, for example, to change "0x12, 0x34, 0x56" to "0x56, 0x34, 0x12 ". Assume that the cursor is initially at 0x12, and the digital register's pressure stack function is used, DW .... delete the five parts to "5 to" 1, and then use "1p .... in turn. The clever thing is that if the p command specifies a digital recorder, the subsequent. the command will automatically add the number of The number register, that is, the first one. the execution is "2 P, and so on.

[Example 3] copy and paste multiple parts
The above technique can also be used to copy the texts of multiple segments to non-consecutive destinations, for example, to declare a variable in a function and reference multiple segments (as Part A, B, and C) copy to another number (marked as X, Y, and Z ). We can delete and undo in part A, B, and C in sequence, and then execute "1p and 2 redo in Z, y, and X, in this way, you do not have to run between two functions.
This usage of the number register can also be used to select historical deleted content. For example, if you want to paste the previously deleted content, you do not know which layer has been pressed, it can be "1pu (or, for example," 3 P, if you are sure that the content is not deleted for the last two times) and then executed several times. U until the desired content is displayed. Of course, it may be faster to use di to check the content of each register.
In this example, the three parts can be respectively y to "a," B and "C, and then to their respective destinations" AP, "bp," CP.

The 26 "A to" Z "Naming registers provide abundant resources for editing operations, for example,ProgramStaff canCodeThe template is preset to some registers (using ftplugin) and P is required. Another major function of the Life name register is to correspond to "A to" Z. When these uppercase letter registers are used, the Operation content is appended to the original content of the lowercase letter register, this is especially convenient to collect multiple contents, and it is more useful to use it with other commands. See the following example.
[Example 4] extract matching row content
: G/RegEx/norm "Ayy
The above command saves all rows matching the RegEx to "A". Of course, you need to clear "a" before running, for example, "Ayy" on a blank line.

Register ". Save the last inserted text. It is very useful when the same text needs to be inserted in multiple places, but it cannot be redo because of other modifications in the middle.

Registers "% and" # Save the current file and replace the file name, which is often used for programming to write the comment header. Note that if the file is in the current directory or its sub-directory, the file name is a relative path name; otherwise, it is a full path name.

2. Other reference methods of registers
: H I _CTRL-R
: H autocmd
: H redir

In addition to specifying as "X" in the normal command, enter <CTRL-R>-X in insert mode and command line to insert the content of "X. Because the register is a special variable pre-defined in Vim, you can also directly read or modify it in the form of a variable (add @ before the variable name) in the command line and script.

Vim saves the last search text in "/". The corresponding variable @/determines the n/n command and the search for highlighted objects. @/Is shared by all the buffer, that is, a new query is performed in a buffer. The matching highlighting of other buffer and the n/n command are also updated. If you want each buffer to maintain its own search content, refer to the following example:
[Example 5] search for content Localization
We use the autocmd function to save the "/value when leaving the buffer, and restore it when entering the buffer:
: Au bufleave * Let B: search_save = @/
: Au bufenter * If exists ("B: search_save") | let @/= B: search_save | endif
You can also change the buffer to a window, that is, maintain your own "/For each window, so that when you edit the same file in multiple windows, the search text will not interfere with each other.

[Example 6] Information redirection
Sometimes we want to save the information for executing the ex command. Vim provides the redir command to redirect the information output to a file or register. For example:
: Redir @
Some commands)
: Redir end
You can define a command to automatically complete the preceding operations:
: Command! -Nargs = * Mc redir @ "> | try | EXE" <ARGs> "| finally | redir end | endtry
For example, [Example 4] can also be implemented without the need to clear registers in advance.
: Mc g/RegEx/P

3. Registers and macros
: H Q
: H @
: H :@

Macros are an important function in vim and are used to repeatedly execute multiple continuous operations. Macros are especially convenient when these operations include commands of different types, such as moving, searching, inserting, and modifying. In many cases, functions that are hard to implement are used: S and: G, macros can be easily handled. Recording macros with Q is actually the process of recording keyboard input to registers, while running macros with @ is the process of using the specified register content as the normal command. The Q command provides "what you do is what you get", but sometimes it is more convenient to directly modify the register. For example, when you finish recording a very complex macro, but find a small problem (for example, it should be de rather than DW), you do not have to record it again, you only need to P out the register content before modifying it.
[Example 5] Some macro skills
A. Allowable Error: if there is a mistake during recording, you don't have to give up again. You can undo it or <BS>. As long as you ensure that these operations are irrelevant to the processing text, what are some messy items in the register.
B. Divide and conquer: When recording a very complex macro, you can consider dividing it into several segments. For example, QA step 1, QB step 2, and then calling A and B in QC, each of which is easy to crack.
C. Run the @ command again to repeat the previous macro call.
D. Use the Q command to input the command into the register. You can also leave nothing to record! What is the purpose? [Example 4] Clear "A's fastest method: Qaq.

In vim, the ": register records the last command line run, so @: Repeat the previous command line operation. It is worth noting that @ x macro runs the normal command, while @: runs the ex command. If a register "X stores the ex command, you can run it with @ X. For example, when testing a command in vimrc, YY is performed first and then @ "is executed.

4. Register Evaluation
Register "= stands out. It does not store text, but provides a way to evaluate the value with an expression and obtain the result in a register. To put it simply, when "=" is specified, VIM will prompt you to enter an expression and return the result of the evaluation. As for how to use this string of text, we can see where it is used.

[Example 6] convert hexadecimal to decimal
In insert mode, enter <C-R> =, VIM will prompt a =, and then enter 0x1234 <CR>, 4660 will be automatically inserted to the current position.

[Example 7] convert decimal to hexadecimal
The reverse operation in the previous example is not that convenient. You must use the printf function. The following MAP replaces the current number with the hexadecimal format. This map may not be easy to understand. The Command actually mentions V_p in [Example 1], but the content used for replacement is the result of "=.
: NMAP \ h viw "= printf (" 0x % x ", <C-R> <C-W>) <CR> P

[Example 8] set the font
A common problem is how to write the guifont settings to _ vimrc. The font size and the font name are usually very long. For example, I use "bitstream_vera_sans_mono: H9: cansi ", it is too troublesome to repeat. Of course, you can use redir in [Example 6] to capture the output of: Set guifont, but this is not the best method. The option in VIM is also a variable, and the variable name is prefixed with &, so we can conveniently use <c-r >=& guifont <CR> to get it. Other variables, such as the Environment Variable $ home, are also required.

"= It is not only used to insert text, but can be referenced wherever registers can be used. For example, if you want to run the DW command after using QA to record a macro, a quick method is to execute
@ A [:-3]
@ = Returns the result of running the following expression. [X: Y] is the substring of the character variable (the first part of the string is numbered from 0, and the last part is numbered from-1). Therefore, @ [: -3] The content except the last DW is returned. Remember, to run it again, just press @@.

[Example 9] example of register evaluation in command line
: Noremap, E: E <C-R >=expand ("%: P: H"). "/" <CR> <C-D>
: Noremap, S: Split <C-R> = expand ("%: P: H"). "/" <CR> <C-D>
: Noremap, S: vsplit <C-R> = expand ("%: P: H"). "/" <CR> <C-D>
: Noremap, T: tabnew <C-R> = expand ("%: P: H"). "/" <CR> <C-D>
The above map prompts to display the list of files in the directory of the current file for edit/split/tabnew, the directory name is obtained by <C-R> =. (If you want the current directory to automatically follow the current file, set autochdir .)

5. Summary
Most registers are described above, but "*," +, and "~ are not mentioned "~, These registers are related to Gui selection and drag-and-drop.ArticleThis article will not be detailed. If you want to copy and paste it with other programs, you can set the clipboard register "* as an untitled register:
: Set clipboard = unnamed
In addition, Q: and Q/can be used to open the history window that stores command lines and find content (the command line history window can also be opened by hitting the CTRL-F under the command line ), you can search for, edit, modify, and execute historical commands.
Register operations seem to be fragmented, but they are often used in editing. We not only need to master the complex and powerful commands such as S and G, but also need to be proficient in small items such as registers, because this is the key to determining the editing efficiency-bit-by-bit acceleration, efficient source.

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.