Whenever I copy text that has been indented into Vim from Chrome, I get a cascading indent instead of a consistent one, so:
def fn(x):
"""Takes x as an input and returns y
if x:
return y
becomes:
def fn(x):
"""Takes x as an input and returns y
if x:
return y
How do I prevent this (preferred), or how do I quickly fix the spacing using other vim commands (acceptable)?
CTRL-C
, switch to vim and insert mode, SHIFT-CTRL-V
.If you're in normal mode, Ctrl-C aborts the current command in progress. Then press u to undo anything that changed before you stopped it.
Add the pastetoggle=<F2> to your . vimrc so you don't have to set it each time.
Paste toggleStart 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).
When you paste using any of your terminal/OS's default method (menu, contextual menu, shortcut, mouse…) the text is not pasted: it is inserted as if you typed it. Because you have enabled autoindenting, Vim indents every line and you end up with that stairway (to hell) effect.
You have two options…
paste/nopaste
and/or pastetoggle
:
You can do :set paste
to disable autoindenting and formatting before you paste and do :set nopaste
afterward.
If you decide to take that path, I suggest you read :h pastetoggle
to make the whole process a little less taxing.
Vim's own commands and clipboard integration:
If your Vim build comes with clipboard support, you can use "+p
or "*p
to paste from other programs without side effects.
The default Vim that comes with most OSes is a "small" build that may lack a number of useful features. If you intend to use Vim as your primary editor for programming it is advised to install a beefier build that comes with clipboard support. On Debian-based systems, the best choice is usually "vim-gtk" or "vim-gnome". On Arch, I think you have to install the "gvim" package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With