At work, we use vim for our editing needs – which, as programmers, means we spend our day in vim.
We have a lot of mixed-format code – sometimes it has tabs, sometimes it has spaces. Personally, I prefer spaces for my indenting, but I’m a convert to the church of make-your-code-match-the-existing-code. As opposed to reformatting the existing code, which is soooo irresistible until you’re faced with a mountain.
So, I have to switch back and forth a lot. Here, for the sake of posterity, is how to switch from spaces to tabs on-the-fly in vim:
:set noexpandtab
:set copyindent
:set preserveindent
:set softtabstop=0
:set shiftwidth=4
:set tabstop=4
I learned a new trick. Add “inoremap <S-Tab> <C-V><Tab>” to your .vimrc and use shift-tab to insert tabs instead of spaces.