0

When I need to copy and paste large blocks of code from a webpage, I need to do the following one step at a time:

  1. :set paste
  2. enter Vim insert mode
  3. paste clipboard
  4. set nopaste
  5. enter normal mode

I'm hoping to do this all at once with a leader key mapping for P.

Thanks for the help!

2
  • A couple of questions: Have you tried building a macro using 'q'? Do you need to be in insert mode to paste? Can you provide a basic example?
    – Shadoath
    Jun 19, 2017 at 16:37
  • I haven't ever used a macro to be honest. Currently, I do need to be in insert mode to paste. Essentially, anytime I paste from a web page (like Stack Overflow) it's a total mess - so I'm looking for a way to emulate "normal" copy and paste outside of VIM. Perhaps I'm going at it the wrong way.
    – Josh
    Aug 6, 2017 at 22:38

1 Answer 1

0

If your Vim has access to the X selection / system clipboard, you can just paste via <C-r>* / <C-r>+ in insert mode, or directly in normal mode via "*p / "+p.

Only if that isn't working you need to :set paste, as Vim otherwise cannot tell whether you've typed this (very quickly), or pasted it. Because any keyboard action could be from the input, it's hard to automate this, but Vim fortunately offers the 'pastetoggle' option to make the switching faster:

'pastetoggle' 'pt'    string  (default "")
                        global
                        {not in Vi}
        When non-empty, specifies the key sequence that toggles the 'paste'
        option.  This is like specifying a mapping:
            :map {keys} :set invpaste<CR>
  Where {keys} is the value of 'pastetoggle'.
        The difference is that it will work even when 'paste' is set.
        'pastetoggle' works in Insert mode and Normal mode, but not in
        Command-line mode.

For example, with :set pastetoggle=<F11>, you just need to i<F11> (paste) <F11><Esc>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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