If I had my way the whole world would come together and agree to use tabs for indentation, but until that happens here is how to manage your indentation when using Vim.
Note that following guide assumes that your space indented file uses 2 spaces for each indent, and that you like your tab indented files to be 4 spaces wide.
" Use tabs for indentation (and convert existing indentation to tabs)
set noet | retab! 2 | set ts=4 sw=4
" Use tabs for indentation (but don't change existing indents)
set noet ts=4 sw=4
" Use spaces for indentation (and convert existing indentation to spaces)
set et ts=2 sw=2 | retab
" Use spaces for indentation (but don't change existing indents)
set et ts=2 sw=2
Add the following lines to your .vimrc
for easy access to the above commands.
Once it’s added you’ll be able to type ,idt
to set indentation to tabs and
,idtc
to set indentation to tabs and convert the existing indents. Note that
when changing from tabs to spaces without converting it may seem as though your
existing indents have been converted when really it’s only the width that has
changed.
" use tabs for indentation
noremap ,idt :set noet ts=4 sw=4 <CR>
" use tabs for indentation (and convert existing indents to spaces)
noremap ,idtc :set noet \| retab! 2 \| :set ts=4 sw=4 <CR>
" use 2 spaces for indentation
noremap ,ids :set et ts=2 sw=2 <CR>
" use 2 spaces for indentation (and convert existing indents to spaces)
noremap ,idsc :set et ts=2 sw=2 \| retab <CR>
Boolean for setting what happens when you press tab. When expandtab
or et
is
set hitting the tab key will actually insert a bunch of spaces (the number of
which is set using the tabstop
setting). You can’t convert a document from
spaces to tabs or vice versa with this command, it just controlls how the tab
key behaves.
Sets the number of spaces each tab takes up. I prefer to use 4 spaces but certain formats such as markdown must be set to 2. Changing this setting will update the whole document.
Number of spaces to use for each step of autoindent. You don’t have to use this setting if you’re just converting the indentation of a document but you’ll probably want to set it if you intend to do any editing. Set it to the same as the tabstop.
Copy indent from current line when starting a new line. You can delete the indent by typing a single backspace followed by the escape key.
Enables smart autoindenting when starting a new line. Indents will be
automatically inserted after a line ending with ‘{‘ and before a line starting
with ‘}’. Normally autoindent
should also be on when working with smartindent.
This is the command you use for converting a document from spaces into tabs and vice versa, it works like a typical find and replace and affects the entire document. You can pass in an optional tabstop number if you like, or don’t to use whatever the current tabstop setting is. The ! makes vim also replace ‘normal’ strings of spaces with tabs where appropriate. I’m not sure what makes a ‘normal’ string normal but I do know that you must use the ! when converting spaces into tabs but not when converting tabs into spaces.