4

When I paste any code into Vim, I the following happens.

enter image description here

How could I tell Vim not to screw up my tabs?

picardo
  • 2,527
  • 7
  • 26
  • 25

1 Answers1

6

Use paste mode, which is a special mode informing vim that you're going to paste text instead of typing it. Just type in:

:set paste

It's useful to have a mapping like:

:set pastetoggle=<F2> 

to quickly switch between paste and regular mode.

Also you might want to consider pasting from clipboard using just p, if your vim supports * and/or + registers. In that case use:

"+p

to paste from clipboard, it'll keep indentation.

Note: it's sometimes useful to have this in your vimrc:

" better yank to clipboard
if has('clipboard')
  if has('unnamedplus')  " When possible use + register for copy-paste
    set clipboard=unnamed,unnamedplus
  else         " On mac and Windows, use * register for copy-paste
    set clipboard=unnamed
  endif
endif

BTW, I just yanked it from my vimrc using y, and pasted it here with CTRLv, so it saves you some work ;)

I hope it helps :)

qiubix
  • 161
  • 4
  • Alas this doesn't answer question asked, nor does the linked question. This stops auto-indent, but tabs are not preserved as required. – Graham Leggett Apr 22 '22 at 20:25