How can I keep the code formated as original source when I paste them to vim?

38

15

When I copy some code from webpages and paste it to VIM,I find it becomes a mess style like a ladder as follows

xxxxxx
   xxxxxx
      xxxxxx
         xxxxxxxxxx

Since it messed so regularly so I think maybe there's something wrong with my .vimrc which is as below:

set number
set nocompatible
set nowritebackup
set noswapfile
syntax on
filetype indent on
filetype plugin on
filetype on
set background=light
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set showmatch
set guioptions=T
set fileencodings=utf-8,prc
set ruler
set incsearch
map gs :%s
set t_Co=256
:colorscheme evening
filetype plugin indent on

Usually I write python in VIM.And help would be appreciated.

SpawnST

Posted 2010-04-26T04:02:13.110

Reputation: 1 891

1Thanks. This irked me for years, and I never got around to asking anyone about it. +1. – DevSolar – 2010-04-26T09:15:58.947

Answers

52

Do this before:

:set paste

Then after:

:set nopaste

Ignacio Vazquez-Abrams

Posted 2010-04-26T04:02:13.110

Reputation: 100 516

Not needed for gvim; it turns these on and off automatically when pasting. – Benjamin Bannier – 2010-04-26T04:49:26.320

any idea why that is? – Nathan Fellman – 2010-04-26T10:13:39.517

1Just guessing: Plain vim is command-line only, and doesn't "know" about the clipboard. It takes the "paste" as a input character sequence, and applies auto-indenting. The GUI-aware gvim might "know" that the incoming data is from the clipboard, and thus disable the autoindenting for the purpose. – DevSolar – 2010-04-26T10:55:48.637

That's correct. The terminal treats the "paste" operation as raw character input. – Ignacio Vazquez-Abrams – 2010-04-26T11:16:59.273

This worked perfect. Finally got rid of this issue in Vim paste (os:Mac) – Stryker – 2017-02-27T02:20:15.433

This isn't purly a vim issue - you can get a similar affect pasting into the old DOS editor in a command prompt. This program seems to be missing in Windows 7 64-bit - presumably because it really is an old 16-bit DOS app and it wasn't worth the hassle of updating it. It's in Windows XP, though, and pasting into that (1) works really slowly, acting out the character-by-character input, and (2) makes a real mess. – Steve314 – 2012-07-17T04:17:55.093

16

http://vim.wikia.com/wiki/Toggle_auto-indenting_for_code_paste

Toggle auto-indenting for code paste

Background

If you use Vim commands to paste text, nothing unexpected occurs. The problem only arises when pasting from another application, and only when you are not using a GUI version of Vim. In a console or terminal version of Vim, there is no standard procedure to paste text from another application. Instead, the terminal may emulate pasting by inserting text into the keyboard buffer, so Vim thinks the text has been typed by the user. After each line ending, Vim may move the cursor so the next line starts with the same indent as the last. However, that will change the indentation already in the pasted text.

Paste toggle

Put the following in your vimrc (change to whatever key you want):

set pastetoggle=<F2>

To paste from another application:

  • Start insert mode.
  • Press F2 (toggles the 'paste' option on).
  • Use your terminal to paste text from the clipboard.
  • Press F2 (toggles the 'paste' option off).

Then the existing indentation of the pasted text will be retained.

You do not have to start insert mode first, but if you are in normal mode and have a mapping for F2, that mapping will apply, and the 'pastetoggle' function will not operate.

Some people like the visual feedback shown in the status line by the following alternative for your vimrc:

nnoremap <F2> :set invpaste paste?<CR>
set pastetoggle=<F2>
set showmode

The first line sets a mapping so that pressing F2 in normal mode will invert the 'paste' option, and will then show the value of that option. The second line allows you to press F2 when in insert mode, to toggle 'paste' on and off. The third line enables displaying whether 'paste' is turned on in insert mode.

aXqd

Posted 2010-04-26T04:02:13.110

Reputation: 443

why not just map the paste keys to do this automatically, like <S-Insert> or <Ctrl+V>, along with their original functions? Or, write a function that disables paste when more than a few input characters are written to the buffer in the time of a second, its just an idea, of course for me I have almost ALL my keys mapped to other stuff so it's a pain, I even have a keyboard with 12 F keys and + 15 more F keys (G keys), and with that, three levels, which still isn't enough :) but thats just me. The only problem is, the right-click menu, which no esc seqs get sent to signal a start-of-paste op... – osirisgothra – 2014-10-27T14:51:30.303

1+1 for the hotkey solution. For the not-too-firm in VIM (like me) who already have mapped the F-keys to something else (like me), <C-F2> would be Ctrl-F2... – DevSolar – 2010-04-26T10:53:00.983

excellent - I am now using this – hoju – 2011-02-12T00:11:38.797

2

It is the autoindent that is messing with you.

set autoindent
set smartindent

Try to disable them when you cut and paste your code, and then enable them again when you are done.

Johan

Posted 2010-04-26T04:02:13.110

Reputation: 4 827

-2

The lowest amount of effort to achieve this is (can be put into .vimrc):

:set paste

Then paste your code.

h2obro

Posted 2010-04-26T04:02:13.110

Reputation: 1

3The 'paste' option was never meant to be permanently set. It disables a lot of things, including mappings and abbreviations. Besides, a previous answer already covers the 'paste' option. – Heptite – 2016-11-09T05:14:18.730

2Welcome to Super User! This duplicates an existing answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – Toby Speight – 2016-11-09T09:17:35.463