Why does shift insert only work fully in insert mode?

13

2

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

treyBake

Posted 2018-03-07T10:40:54.693

Reputation: 229

Hey, I've got two thumbs, too!!! :D – RonJohn – 2018-03-07T16:18:41.000

Answers

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)

Attie

Posted 2018-03-07T10:40:54.693

Reputation: 14 841

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 – 2018-03-07T18:10:34.103

@Josh: interesting, thanks... your comment even lead me to a vim plugin

– Attie – 2018-03-07T18:15:56.007

Am I the only one bothered by set nopaste ? I'd expect unset paste. – Eric Duminil – 2018-03-07T21:52:31.133

@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 – 2018-03-08T00:17:54.540