How do I use vim registers?

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

    I was pleased when I discovered the 0 register. If you yank text without assigning it to a particular register, then it will be assigned to the 0 register, as well as being saved in the default " register. The difference between the 0 and " registers is that 0 is only populated with yanked text, whereas the default register is also populated with text deleted using d/D/x/X/c/C/s/S commands.

    I find this useful when I want to copy some text, delete something and replace it with the copied text. The following steps illustrate an example:

    • Yank the text you want to copy with y[motion] - this text is saved in " and 0 registers
    • Delete the text you want to replace with d[motion] - this text is saved in " register
    • Paste the yanked text with "0p

    where " is the command to use a register for the next command.

    On the final step, if you were to paste from the default register (with p), it would use the text that you had just deleted (probably not what you intended).

    Note that p or P pastes from the default register. The longhand equivalent would be ""p (or ""P) and "0 holds the last yank, "1holds the last delete or change.

    For more info see :help registers.

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

    From vim's help page:

    CTRL-R {0-9a-z"%#:-=.}                  *c_CTRL-R* *c_<C-R>*
            Insert the contents of a numbered or named register.  Between
            typing CTRL-R and the second character '"' will be displayed
            <...snip...>
            Special registers:
                '"' the unnamed register, containing the text of
                    the last delete or yank
                '%' the current file name
                '#' the alternate file name
                '*' the clipboard contents (X11: primary selection)
                '+' the clipboard contents
                '/' the last search pattern
                ':' the last command-line
                '-' the last small (less than a line) delete
                '.' the last inserted text
                                *c_CTRL-R_=*
                '=' the expression register: you are prompted to
                    enter an expression (see |expression|)
                    (doesn't work at the expression prompt; some
                    things such as changing the buffer or current
                    window are not allowed to avoid side effects)
                    When the result is a |List| the items are used
                    as lines.  They can have line breaks inside
                    too.
                    When the result is a Float it's automatically
                    converted to a String.
            See |registers| about registers.  {not in Vi}
            <...snip...>
    
    0 讨论(0)
  • 2020-11-27 09:30

    My friend Brian wrote a comprehensive article on this. I think it is a great intro to how to use topics. https://www.brianstorti.com/vim-registers/

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

    A big source of confusion is the default register ". It is important to know the way it works. It is much better if the default register is avoided most of the times. The explanation from the Vim documentation:

    Vim fills this register with text deleted with the "d", "c", "s", "x" commands
    or copied with the yank "y" command, regardless of whether or not a specific
    register was used (e.g.  "xdd).  This is like the unnamed register is pointing
    to the last used register.
    

    So the default register is actually a pointer to the last used register. When you delete, or yank something this register is going to point to other registers. You can test that by checking the registers. There is always another register that is exactly the same as the default register: the yank register ("0) , the first delete register("1) , small delete register("-) or any other register that was used to delete or yank.

    The only exception is the black hole register. Vim doc says:

    An exception is the '_' register: "_dd does not store the deleted text in any
    register.
    

    Usually you are much better off by using directly: "0, "- and "1-"9 default registers or named registers.

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

    Registers in Vim let you run actions or commands on text stored within them. To access a register, you type "a before a command, where a is the name of a register. If you want to copy the current line into register k, you can type

    "kyy
    

    Or you can append to a register by using a capital letter

    "Kyy
    

    You can then move through the document and paste it elsewhere using

    "kp
    

    To paste from system clipboard on Linux

    "+p
    

    To paste from system clipboard on Windows (or from "mouse highlight" clipboard on Linux)

    "*p
    

    To access all currently defined registers type

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