30

Frequently, in configuration files I might copy and paste a block of lines, then I want to make small changes to that block. After pasting, I use x to delete characters that I want to remove, then move on to paste the next block. Except x puts the deleted characters on the clipboard, so I have to recopy the block of lines again.

Is there a way to do this without going into insert mode?

35

Your options are:

  1. Use the black hole register _. For example, to delete a line you would type "_dd. This deletes without affecting the clipboard.
  2. Explicitly name a register for the original yank. For example, to yank a line into a named register you would type "ayy then to paste you would type "ap
  3. Use the yank registers to retrieve the original yank instead. You can yank, then delete as necessary, then when you go to paste type "0p
  4. Use a plugin such as vim-easyclip to provide a cut operator that is distinct from the delete operator (full disclosure: I wrote that)

Run :help registers for more detail on the first 3

  • 4
    So for x I'd use "_x then? – durron597 Feb 3 '15 at 19:43
  • 1
    That's correct. – Steve Vermeulen Feb 3 '15 at 19:44
  • 2
    @durron597 if you're deleting one character at a time with x, you should probably learn to motion deletes like dt) which deletes until the next close parenthesis of d2W which deletes the next two whitespace separated tokens. Then you might do something like "_di{ inside a braced region, which is something I do all the time. In normal mode this means "delete everything inside this braced region into the black hole" – OregonTrail Nov 5 '15 at 1:21
  • Added nnoremap x "_x to my ~/.vimrc (explanation here: stackoverflow.com/questions/54255/…). – Victoria Stuart Dec 20 '18 at 17:02
11

x doesn't put things on the clipboard, it puts them into a register. If you don't tell it which register to put it into, it puts it into the unnamed register, which of course overwrites the previous contents. So, instead, tell it which register to use: " REGISTER x, where REGISTER is any single letter. You then use the same quote-register prefix to p to paste from that register. (Note: lowercase replaces their content; uppercase appends).

There are also two special registers "0 and "1. "0 is the most recently yanked (y) text, which will remain even if you delete some text with another command (like x). "1 is the most-recently deleted text, as long as that text isn't small (one line). Small deleted text goes in "-.

Finally, as Steve Vermeulen points out, you can tell vim not to save deleted text by specifying the black hole register "_.

The relevant help command is :help registers.

3

The :let command will let you redefine a named register. In vim's command line, pressing Control+R twice1 and then the register name will paste the register's contents into the command. Then you can then modify what it's pasted, and save the register with your modifications.

Example:

  • In normal mode, on a line containing the text "Foo Bar", typing "fyy yanks the whole line (yy) into the f register ("f).
  • Type :let @f=', and Control+r, Control+r again, then f, and you should see it paste in "Foo Bar", so your command line now reads :let @f='Foo Bar, with the cursor at the very end.
  • Now you can use the arrows and delete keys to edit what you've just pasted in, such as :let @f='foo bar baz.
  • Finish off the quoting: :let @f='foo bar baz' and hit Enter.

Now you've redefined the contents of the f register to be "foo bar baz".

(This also works for macros! :help let for more information.)


1 Control+r lets you insert a register when you're already in insert mode, as if you're typing the characters. Using this sequence twice is important because "the text is inserted literally, not as if typed. This differs when the register contains characters like <BS>." —the insert.txt vimdoc, :help i_CTRL-R_CTRL-R

0

Related to the blackhole register "_ mentioned by @derobert you can find an useful screencast in http://vimcasts.org/episodes/meet-the-yank-register/

Vim uses the terms delete, yank, and put, in place of the more standard terminology cut, copy, and paste. Starting with the same letter, put and paste are easily interchangable. Once you get used to the word yank, it becomes synonymous with copy. But Vim’s use of the word ‘delete’ is problematic.In most contexts, the word delete just means remove. But Vim’s delete operation copies the text into a register, then removes it from the document. In other words, Vim’s delete operation behaves like a cut.

  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. – jamessan Apr 17 '15 at 3:21
  • I tried to comment on the original post but I couldn't. The only alternative I had was the the one I chose @jamessan – Marlon León Apr 17 '15 at 18:05
0

Plugin ReplaceWithRegister

http://www.vim.org/scripts/script.php?script_id=2703 (2009-2014)

It suggests the mapping gr.

  1. yank
  2. visually select the text you want to replace and press gr
  3. move to next block you want to replace

Alternatively, use gr<text object> to do it without a visual selection. dot-repeat is supported when the plugins repeat.vim by Tim Pope and visualrepeat.vim by Ingo Karkat are installed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.