Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickest Way to Revert Spaces to TABs in VIM

Tags:

vim

vi

People also ask

How do I indent 4 spaces in vim?

The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4 , or the abbreviation :set sw=4 . If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default. Use :set expandtab .

What is Expandtab in Vim?

Use expand tab to convert new tabs to spaces The expandtab property will ensure that when you hit tab it will actually use spaces. So first set the number of spaces a tab should be, then set expandtab. set tabstop=2 shiftwidth=2 expandtab. Tabstop determines how many columns a tab counts for.

What does Smarttab do in Vim?

'smarttab' affects how <TAB> key presses are interpreted depending on where the cursor is. If 'smarttab' is on, a <TAB> key inserts indentation according to 'shiftwidth' at the beginning of the line, whereas 'tabstop' and 'softtabstop' are used elsewhere.


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)


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.


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


Only this configuration helped me solve this problem.

filetype plugin indent on
filetype detect
autocmd FileType make set noexpandtab