Vim copy/paste messing up indentaton
Solution 1
In the terminal, Vim cannot distinguish between typed text (where you want automatic indenting), and pasted text. So there's the 'paste'
option (and 'pastetoggle'
to simplify handling), which when set disables auto-formatting and -indenting. An alternative is using graphical GVIM, which can detect that.
Or, you use Vim's clipboard access (if configured and working, which you need to try out), and use the "*
/ "+
registers for the selection / system clipboard, e.g. via "+p
or :put +
. Maybe pasting with the middle mouse button also just works; try it out!
Solution 2
:set paste
or see http://vim.wikia.com/wiki/Toggle_auto-indenting_for_code_paste
(herearesomecharsbecausesuperuser.comthinksshortanswersarenotanygoodanswers)
Solution 3
One of the best solutions to this issue that I've encountered is to use the bracketed paste plugin: https://github.com/ConradIrwin/vim-bracketed-paste - this allows you to paste directly into vim without worrying about the indentation going awry .. and without needing to hit different keys prior to pasting etc etc
Solution 4
One possibility, which may not be an option for you, is to use gvim
instead of vim
. The latter is able to distinguish pasting from rapid text typing. From vim
's help for :paste
:
'paste' boolean (default off)
global {not in Vi}
Put Vim in Paste mode. This is useful if you want to cut or copy
some text from one window and paste it in Vim. This will avoid
unexpected effects.
Setting this option is useful when using Vim in a terminal, where Vim
cannot distinguish between typed text and pasted text. In the GUI, Vim
knows about pasting and will mostly do the right thing without 'paste'
being set. The same is true for a terminal where Vim handles the
mouse clicks itself.
This option is reset when starting the GUI. Thus if you set it in
your .vimrc it will work in a terminal, but not in the GUI. Setting
'paste' in the GUI has side effects: e.g., the Paste toolbar button
will no longer work in Insert mode, because it uses a mapping.
When the 'paste' option is switched on (also when it was already on):
- mapping in Insert mode and Command-line mode is disabled
- abbreviations are disabled
- 'textwidth' is set to 0
- 'wrapmargin' is set to 0
- 'autoindent' is reset
- 'smartindent' is reset
- 'softtabstop' is set to 0
- 'revins' is reset
- 'ruler' is reset
- 'showmatch' is reset
- 'formatoptions' is used like it is empty
These options keep their value, but their effect is disabled:
- 'lisp'
- 'indentexpr'
- 'cindent'
NOTE: When you start editing another file while the 'paste' option is
on, settings from the modelines or autocommands may change the
settings again, causing trouble when pasting text. You might want to
set the 'paste' option again.
When the 'paste' option is reset the mentioned options are restored to
the value before the moment 'paste' was switched from off to on.
Resetting 'paste' before ever setting it does not have any effect.
Since mapping doesn't work while 'paste' is active, you need to use
the 'pastetoggle' option to toggle the 'paste' option with some key.
Related videos on Youtube
Luke
Updated on September 18, 2022Comments
-
Luke over 1 year
Whenever I copy something from another application and then go to paste it into vim, it messes up the indentation.
For example, just now I tried to copy the manifest.json file from the hello-world tutorial for creating chrome extensions.
It looks like this:
{ "manifest_version": 2, "name": "One-click Kittens", "description": "This extension demonstrates a browser action with kittens.", "version": "1.0", "permissions": [ "https://secure.flickr.com/" ], "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" } }
but, when I paste it into vim, it looks like this:
My vimrc is as follows:
"se t_Co=256 syntax enable set nowrap set mouse=a set tabstop=4 set softtabstop=4 set shiftwidth=4 set expandtab set number set showcmd set cursorline set showmatch execute pathogen#infect() "filetype plugin indent on "folding settings set foldmethod=indent "fold based on indent set foldnestmax=10 "deepest fold is 10 levels set nofoldenable "dont fold by default set foldlevel=1 set clipboard=unnamed "share one clipboard for everyhting
It has something to do with this line:
execute pathogen#infect() "filetype plugin indent on
If I comment it out, The problem is resolved. However, this is what I use to achieve auto-indent for when I am coding in python. Is there another way to get auto-indent?
-
Ingo Karkat over 9 yearsHow do you paste, with middle mouse button? Read
:help 'paste'
for an explanation and toggle that option before / after pasting, or paste from within Vim (via the register+
, e.g."+p
. -
Luke over 9 yearswoah, so I have always pasted by right-clicking and choosing paste, but I just tried it with the middle mouse button and it worked as expected.
-
Ingo Karkat over 9 yearsAh, great! I've expanded my comment into a proper answer; please accept by clicking on the outlined checkmark next to it.
-
-
Luke over 9 yearsThis did not fix the problem
-
RustyToms over 5 yearsThis worked for me. Outside of insert mode I typed the
:set paste
command, hit enter, then command+v (I'm on a mac) to paste, and it pasted my content without messing up the indentation. :set paste doesn't do it itself, it just sets it up so you can do it. Probably need to:set nopaste
afterwards to get the regular vim features back.