Quickest Way to Revert Spaces to TABs in VIM

I have always been one to replace TABs in VIM with x amount of spaces (usually 4). Four lines I almost always use in my .vimrc config file are:

set tabstop=4 

set shiftwidth=4

set expandtab

syntax on

Basically, there are moments when I NEED to use a single TAB (such as Makefiles), and I don't know how else to work around that other than leaving the file, editing my .vimrc, and then reloading the file of interest.

That said, what is the quickest way (from within VIM) to revert it back to using TABS, and then reverting back to my original settings? I'm looking for the least-hassle, least-keystrokes solution.


Solution 1:

VIM will automatically enable the TAB for a makefile, assuming you name it "makefile," as opposed to "Makefile." Not sure why VIM still doesn't detect the type with a lower-uppercase difference, but such is life. (@Sedrik)

That aside, other alternative solutions are:

Filetype Binding (@ThorstenS @tungd):

autocmd FileType make setlocal noexpandtab

RealTime Switch (@ThorstenS):

Assuming the .vimrc configuration mentioned in the question, do:

:set noet (to switch from spaces to TAB)

and :set et (to switch back)

Solution 2:

Just use the magical escape key available in insert mode.

On the *NIX's it is ^V by default when you are in insert mode. On Windows, you need to find out what the magical escape character is - ^V is taken for something else; I think it may be ^Q or ^S?

So! In your makefile:

this: this.c
<C-V><Tab>cc this.c

where the usual meanings apply:

means hit ctrl-V (you should see a ^ hiding away under the cursor) - hit the tab key. Bingo.

Works for me.

Note: if you use vim settings or startup code which smashes tabs as you read a file, this obviously is a short-term fix. I prefer to learn how to use the retab command to ensure a file is tab-clean, because I don't like a file to be touched unless I consciously choose to do so.

Solution 3:

Just type set noexpandtab . Perhaps you bind this to a function key.

Solution 4:

Only this configuration helped me solve this problem.

filetype plugin indent on
filetype detect
autocmd FileType make set noexpandtab