How do I use vim registers?

后端 未结 17 2120
一向
一向 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:14

    One overlooked register is the '.' dot register which contains the last inserted text no matter how it was inserted eg ct] (change till ]). Then you realise you need to insert it elsewhere but can't use the dot repeat method.

     :reg .
     :%s/fred/<C-R>./
    
    0 讨论(0)
  • 2020-11-27 09:14

    My favorite register is the ':' register. Running @: in Normal mode allows me to repeat the previously ran ex command.

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

    My favorite feature is the ability to append into registers by using capital letters. For example, say you want to move a subset of imports from buffer X to buffer Y.

    1. Go to line x1 in buffer X.
    2. Type "ayy to replace register a with the content of line x1.
    3. Go to line x5.
    4. Type "Ayy (capital A) to append line x5 at the end of register a.
    5. Go to buffer Y and type "ap to paste
    <content of line x1>
    <content of line x5>
    
    0 讨论(0)
  • 2020-11-27 09:22
    • q5 records edits into register 5 (next q stops recording)
    • :reg show all registers and any contents in them
    • @5 execute register 5 macro (recorded edits)
    0 讨论(0)
  • 2020-11-27 09:24

    Use registers in commands with @. E.g.:

    echo @a
    echo @0
    echo @+
    

    Set them in command:

    let @a = 'abc'
    

    Now "ap will paste abc.

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

    If you ever want to paste the contents of the register in an ex-mode command, hit <C-r><registerletter>.

    Why would you use this? I wanted to do a search and replace for a longish string, so I selected it in visual mode, started typing out the search/replace expression :%s/[PASTE YANKED PHRASE]//g and went on my day.

    If you only want to paste a single word in ex mode, can make sure the cursor is on it before entering ex mode, and then hit <C-r><C-w> when in ex mode to paste the word.

    0 讨论(0)
提交回复
热议问题