Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

On Vim i paste this script

#!/bin/sh
VAR=1
while ((VAR <  10))
    do
        echo "VAR1 is now $VAR" 
        ((VAR = VAR +2))
    done
    echo "finish"

But i obtain this strange thing

#!/bin/sh
#VAR=1
#while ((VAR <  10))
#       do
#                       echo "VAR1 is now $VAR" 
#                                       ((VAR = VAR +2))
#                                               done
#                                                       echo "finish"
#                                                       

Why vim put # and tabs?

share|improve this question
21  
This might be a better question for vi.stackexchange.com –  Eric Renouf 20 hours ago
    
@EricRenouf why? (honest question) –  Qix 9 hours ago
2  
@EricRenouf I can't believe that is StackExchange. Let's see if it comes out of beta. –  Jared Burrows 9 hours ago
    
@qix my thought was this is a question about how vim works, which is a common tool to be used on *nix, but *nix isn't a requirement for using vim –  Eric Renouf 3 hours ago
    
@JaredBurrows I sure hope it makes it –  Eric Renouf 3 hours ago

3 Answers 3

up vote 26 down vote accepted

There're two reasons:

For pasting in vim while auto-indent is enabled, you must change to paste mode by typing:

:set paste

Then you can change to insert mode and paste your code. After pasting is done, type:

:set nopaste

to turn off paste mode. Since this is a common and frequent action, vim offers toggling paste mode:

set pastetoggle=<F2>

You can change F2 to whatever key you want, and now you can turn pasting on and off easily.


To turn off auto-insert of comments, you can add these lines to your vimrc:

augroup auto_comment
    au!
    au FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o
augroup END

vim also provides a pasting register for you to paste text from the system clipboard. You can use "*p or "+p depending on your system. On a system without X11, such as OSX or Windows, you have to use the * register. On an X11 system, like Linux, you can use both.

Further reading

share|improve this answer
1  
I have been using Vim forever and never knew about this. Many thanks. –  Caja 19 hours ago
    
Most modern graphical terminal emulators support a feature called "bracketed paste mode". Vim can ask the terminal to enable this mode, which, in turn, will surround pasted text with certain escape sequences. Vim can recognize this and turn off wrapping/indenting for the duration of the paste. Long story stort: pasting will "work as expected". I'm not a vim user, but a quick websearch will sure tell you how to configure this mode. –  egmont 19 hours ago
    
I added set paste to my .vimrc so it is always on which has seemed to work for some time now. Why would I ever need to turn it off / toggle it ? –  Michael Durrant 14 hours ago
    
That would be if you ever want to use auto indent/comment, which can be pretty useful when you're writing/editing code rather than copying it. –  David Lord 12 hours ago
1  
@MichaelDurrant set paste should never be in your vimrc. It disables or resets a lot of things, including insert mode mappings, command line mappings, abbreviations, textwidth, wrapmargin, autoindent, smartindent, softtabstop, formatoptions, indentexpr, and a couple of others. :h paste has the whole list. Most people use some or all of these things and wonder why there settings are ignored. –  FDinoff 10 hours ago

Use the vim paste. What you want is to paste what is on the clipboard buffer "+p

This selects the + and pastes it in place.

If your using Linux * is the x buffer (the last selected text)

Then vim knows its a paste

What happens otherwise is vim thinks you have typed the keys being pasted (which include the indentation combined with vim doing auto indentation leaves with 2 lots per instead of one which continues all the way to the end of the paste

As a note for this to work over SSH you need to set the option for your clipboard to be shared -Y

See man ssh for more details

share|improve this answer
2  
This is actually the correct answer imho –  Kristoffer Sall-Storgaard 7 hours ago
    
It looks like this only works for the graphical vim (gvim); vim running in a terminal emulator pastes the last deleted text with "*p, not the current selection. –  wurtel 7 hours ago
    
I dont use gvim, only the terminal vim. what is your mouse setting ? my mouse setting is c. Not sure how it works when the mouse selection is allowed. set mouse? –  exussum 6 hours ago
1  
also check vim --version se see if it was compiled with clipboard support +xterm_clipboard should be in the output –  exussum 6 hours ago
    
@wurtel: Maybe you have set clipboard with unnamed string. –  cuonglm 5 hours ago

The tabs were inserted because you have autoindent turned on and you can disable that behavior by turning off autoindent (:set noai) before you paste into terminal.

The commented lines are produced by auto commenting and can be disabled by turning that off.

Alternative to those you should get the desired behavior using the toggles :set paste, pasting your formatted code and :set nopaste to restore normal behavior.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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