I use the tab key to indent my python code in Vim, but whenever I copy and paste a block Vim replaces every tab with 4 spaces, which raises an IndentationError
I tried setting :set paste as suggested in related questions but it makes no difference
Other sites suggest pasting ‘tabless’ code and using the visual editor to re-indent, but this is asking for trouble when it comes to large blocks
Are there any settings I can apply to vim to maintain tabs on copy/paste?
Thanks for any help with this 🙂
edit:
I am copying and pasting within vim using the standard gnome-terminal techniques (ctrl+shift+c / mouse etc.)
my .vimrc is:
syntax on
set ts=4
if has("terminfo")
let &t_Co=8
let &t_Sf="\e[3%p1%dm"
let &t_Sb="\e[4%p1%dm"
else
let &t_Co=8
let &t_Sf="\e[3%dm"
let &t_Sb="\e[4%dm"
endif
I looked up that ts -> Sets tab stops to n for text input, but don’t know what value would maintain a tab character
See
:h tabstopfor all the options and how they interact with each other.These are good settings if you prefer tabs:
With these settings, you hit
<Tab>and you get<Tab>.These are good settings if you prefer spaces:
With these settings, you hit
<Tab>and you get<Space><Space><Space><Space>.Whatever you choose, you should not use your terminal key bindings for copying/pasting. Inside Vim, you should “yank” with
yand “put” withporP; optionally using a specific register like"ay/"apto yank/put to/from the content of@aor"+y/"+pto yank/paste to/from the system clipboard (if your Vim is built with clipboard support).As a side note, you should use the long form names of your settings as they are more readable than their short counterpart. Your future self will thank you.