13

Just tried pasting some content (in Git Bash on CentOS server) into a file using vim.
I just opened the file and tried using shift+insert and I noticed that the first half (roughly) gets cut off, but if I set vim to insert mode and hit shift+insert everything gets pasted perfectly.
Was just wondering why this is?

Thanks

1
  • Hey, I've got two thumbs, too!!! :D
    – RonJohn
    Mar 7, 2018 at 16:18

1 Answer 1

24

Both Shift + Insert and "pasting" into a terminal behave in a similar way - they emulate key presses.

This is an important distinction that is often impossible for terminals to make - are you typing or pasting? Note: some terminals support "bracketed paste" modes, after @Josh's comment I even came across a vim plugin bracketed paste in xterm which you may be interested in.

Your pasted data will be lost up to the first character that enters an insert mode. You may also find that your cursor has moved and that other parts of the file have changed (e.g: changed case / been deleted / etc)...

In summary, if you're pasting text that you want inserted into the file, enter insert mode first.


Try copying the following text and paste it into a terminal running Vim (not in insert mode):

hello how are you

The result is the same as typing the same letters on your keyboard:

result of pasting into Vim

In this case, it leaves you in insert mode...

Now try exiting insert mode - Esc - and pasting the following

/are
n

This performs a search for "are":

result of pasting into Vim

Now paste this:

:0
dG

Oh no... everything is gone!

all gone


You will also find that if you have indenting enabled, then pasting a block of code into vim (in insert mode) will indent too much - it'll auto indent, and then your pasted code will include indentation.

def my_print(message):
    print(message)
    print('done...')

pasting code

To fix this, use the :set paste and :set nopaste commands

pasting code (paste mode)

4
  • 4
    "This is an important distinction that terminals cannot make - are you typing or pasting?" - this is not entirely true, some terminals and applications support "bracketed paste" where a special sequence is sent before and after the pasted text.
    – ash
    Mar 7, 2018 at 18:10
  • @Josh: interesting, thanks... your comment even lead me to a vim plugin
    – Attie
    Mar 7, 2018 at 18:15
  • Am I the only one bothered by set nopaste ? I'd expect unset paste. Mar 7, 2018 at 21:52
  • @ThisGuyHasTwoThumbs as an alternative, at least when using Vim, you may want to use the "+ register for interacting with the system clipboard, since it will tend to behave more sanely. The downside is that some systems or builds of Vim may use "* instead, or simply not have system clipboard support, but in my experience, "+ has been quite reliable.
    – 8bittree
    Mar 8, 2018 at 0:17

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.