VIM Replace word with contents of paste buffer?

后端 未结 9 1409
灰色年华
灰色年华 2020-12-22 15:41

I need to do a bunch of word replacements in a file and want to do it with a vi command, not an EX command such as :%s///g. I know that this is the typical way

相关标签:
9条回答
  • 2020-12-22 15:56

    You can use yw to yank the word, then you can change the word with yanked word by vipw to yank word and paste previously yanked word.

    0 讨论(0)
  • 2020-12-22 15:57

    Do you mean the system paste buffer or the vi register?

    If you want to use the system paste buffer then you are fine and could do dw"+P - " chooses a register, and "+ is the system paste buffer.

    Otherwise copy into the non-default register with say "ay to copy into register a and then to replace something do dw"aP

    0 讨论(0)
  • 2020-12-22 16:05

    Using the information in this post, I have formed this useful mapping. I chose 'cp' because it signifies "change paste"

    nmap <silent> cp "_cw<C-R>"<Esc>

    EDIT:

    Also I took this a step further and supported any motion.

    To get the equivalent of command above it would be cpw for "change paste word"

    "This allows for change paste motion cp{motion}
    nmap <silent> cp :set opfunc=ChangePaste<CR>g@
    function! ChangePaste(type, ...)
        silent exe "normal! `[v`]\"_c"
        silent exe "normal! p"
    endfunction
    
    0 讨论(0)
  • 2020-12-22 16:05

    Or you could do Shift+v-p (select the whole line and paste in its' place)

    0 讨论(0)
  • 2020-12-22 16:11

    You can use the visual mode of vim for this. e.g. copy a word: ye and then overwrite another one with the copied word: vep

    0 讨论(0)
  • 2020-12-22 16:13

    I'm thinking by "paste" you mean the unnamed (yank/put/change/delete/substitute) register, right? (Since that's the one that'd get overwritten by the change command.)

    Registers are generally specified by typing " then the name (single character) of the register, like "ay then "ap to yank into register a, then put the contents of register a. Same goes for a change command. In this case, if you don't want the text you remove with the change command to go anywhere, you can use the black hole register "_: "_cw. Then once in insert mode, you can hit ctrl-R followed by the register you want (probably ") to put in the contents of that register.

    • "* - selection register (middle-button paste)
    • "+ - clipboard register (probably also accessible with ctrl-shift-v via the terminal)
    • "" - vim's default (unnamed) yank/put/change/delete/substitute register.

    Short answer: "_cw^R"

    Edit: as others are suggesting, you can of course use a different register for the yank (or whatever) that got your text into the default register. You don't always think of that first, though, so it's nice to do a single change command without blowing it away. Though it's not totally blown away. There are the numbered registers "0 through "9:

    Vim fills these registers with text from yank and delete commands.

    Numbered register 0 contains the text from the most recent yank command, unless the command specified another register with ["x].

    Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (the small delete register is used then). An exception is made for the delete operator with these movement commands: %, (, ), `, /, ?, n, N, { and }. Register "1 is always used then (this is Vi compatible). The "- register is used as well if the delete is within a line.

    With each successive deletion or change, Vim shifts the previous contents of register 1 into register 2, 2 into 3, and so forth, losing the previous contents of register 9.

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