4

I am going to be pasting a lot of characters into a file. Each time I paste something, the pasted element will need to be wrapped in quotes and followed by a comma. Is there a way to automatically wrap these characters after each paste in vim? The paste will be coming from the system clipboard, not a vim paste buffer.

For example, say the system clipboard contains a, when pasting this into vim using a middle-click, I would like it to insert "a",.

3 Answers 3

7

Do you need to use the mouse (middle-klick)? I'd go for a macro:

  1. Press qq to start recording
  2. Press i"Esc"*p`]a",Esc to first insert the first set of quote, then paste from the system clipboard, go to the end of the paste and append quote and comma
  3. Press q to stop recording

Then, each time you want to paste that way, simply hit @q.

Note: If you're on X, you might need to use "+ instead of "*, dpending on which clipboard you really want to use. See :h x11-selection.

Note2: This pastes before your cursor. Change the first i to a to paste after your cursor.

1
  • 1
    When using the same macro often (without using other macros inbetween), it's very convenient to use @@ from the 2nd time on.
    – Tobias
    Mar 17, 2015 at 23:50
4

Assuming your vim includes clipboard support (:version shows +clipboard) and you intend to do this in more than one editing session, I would recommend creating a mapping that does this for you:

nnoremap <F5> i"",<Esc>h"+P

<F5>: what you'll hit to trigger the mapping. Could be any combination of keys.

i"",<Esc>: "", and escape insert mode.

h: move the cursor left 1 character.

"+P: paste from the system clipboard before the current character. If pasting via middle-click, you'd want "*P instead

This is currently formulated to work when in normal mode, but could work in insert mode by removing the leading i (and perhaps adding one at the end)

4

My UnconditionalPaste plugin provides a g,"p mapping that pastes each line in the register (prepend "+ for the system clipboard, as usual) surrounded by double quotes and delimited by commas, all in one line. This is just one particular variant from a plethora of similar mappings; some even query for the separator.

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.