How do I use vim registers?

后端 未结 17 2118
一向
一向 2020-11-27 08:58

I only know of one instance using registers is via CtrlR* whereby I paste text from a clipboard.

What are other uses of registers?

相关标签:
17条回答
  • 2020-11-27 09:07

    The black hole register _ is the /dev/null of registers.

    I use it in my vimrc to allow deleting single characters without updating the default register:

    noremap x "_x
    

    and to paste in visual mode without updating the default register:

    vnoremap p "_dP
    
    0 讨论(0)
  • 2020-11-27 09:08

    I think the secret guru register is the expression = register. It can be used for creative mappings.

    :inoremap  \d The current date <c-r>=system("date")<cr>
    

    You can use it in conjunction with your system as above or get responses from custom VimL functions etc.

    or just ad hoc stuff like

    <c-r>=35+7<cr>
    
    0 讨论(0)
  • 2020-11-27 09:09

    Other useful registers:

    "* or "+ - the contents of the system clipboard

    "/ - last search command

    ": - last command-line command.

    Note with vim macros, you can edit them, since they are just a list of the keystrokes used when recording the macro. So you can write to a text file the macro (using "ap to write macro a) and edit them, and load them into a register with "ay$. Nice way of storing useful macros.

    0 讨论(0)
  • 2020-11-27 09:10

    A cool trick is to use "1p to paste the last delete/change (, and then use . to repeatedly to paste the subsequent deletes. In other words, "1p... is basically equivalent to "1p"2p"3p"4p.

    You can use this to reverse-order a handful of lines: ffffdffffdffffdd"1p....

    0 讨论(0)
  • 2020-11-27 09:12

    One of my favorite parts about registers is using them as macros!

    Let's say you are dealing with a tab-delimited value file as such:

    ID  Df  %Dev    Lambda
    1   0   0.000000    0.313682
    2   1   0.023113    0.304332
    3   1   0.044869    0.295261
    4   1   0.065347    0.286460
    5   1   0.084623    0.277922
    6   1   0.102767    0.269638
    7   1   0.119845    0.261601
    

    Now you decide that you need to add a percentage sign at the end of the %Dev field (starting from 2nd line). We'll make a simple macro in the (arbitrarily selected) m register as follows:

    1. Press: qm: To start recording macro under m register.

    2. EE: Go to the end of the 3rd column.

    3. a: Insert mode to append to the end of this column.

    4. %: Type the percent sign we want to add.

    5. <ESC>: Get back into command mode.

    6. j0: Go to beginning of next line.

    7. q: Stop recording macro

    We can now just type @m to run this macro on the current line. Furthermore, we can type @@ to repeat, or 100@m to do this 100 times! Life's looking pretty good.

    At this point you should be saying, "But what does this have to do with registers?"

    Excellent point. Let's investigate what is in the contents of the m register by typing "mp. We then get the following:

    EEa%<ESC>j0
    

    At first this looks like you accidentally opened a binary file in notepad, but upon second glance, it's the exact sequence of characters in our macro!

    You are a curious person, so let's do something interesting and edit this line of text to insert a ! instead of boring old %.

    EEa!<ESC>j0
    

    Then let's yank this into the n register by typing B"nyE. Then, just for kicks, let's run the n macro on a line of our data using @n....

    It added a !.

    Essentially, running a macro is like pressing the exact sequence of keys in that macro's register. If that isn't a cool register trick, I'll eat my hat.

    0 讨论(0)
  • 2020-11-27 09:14

    I use the default register to grep for text in my vim window without having to reach for the mouse.

    1. yank text
    2. :!grep "<CTRL-R>0"<CR>
    0 讨论(0)
提交回复
热议问题