Toggling Between Indentation Styles in Vim

Author: Jake Bauer | Published: 2020-06-02

Indentation style, much like which editor is superior, is a topic that is constantly bickered about on the Internet to no end. Usually programmers are either on the side of indenting with spaces (and typically with 4 spaces), or indenting with tabs. Relatively few are in the camp of indenting with both spaces and tabs, though this is still common in certain programming circles.

I typically use 4 spaces of indentation because that’s what I learned to use from using Python as my first language. It wasn’t until a couple years later when I encountered Makefile’s strict reliance on tab characters and the argument of spaces versus tabs for indentation. I didn’t see any really convincing arguments to switch from using spaces, my peers were using spaces, and spaces also helped me to line things up in languages like Scheme so I saw no real reason to change.

I have, however, recently encountered quite a few projects which use tabs and have noticed many system configuration files using tabs instead of spaces for indentation. When contributing to other projects, it’s essential to match the style of the pre-existing code and when editing those system configuration files I wanted to be consistent with the existing style so I needed a quick way to toggle between indentation styles.

Since I use Vim, basically any possible indentation configuration is supported. It’s also easy to set up a keybind to toggle between indentation styles by writing a function into my vimrc. I came up with the following VimScript to switch between indentation styles:

" Toggle between 4 spaces and pure tab indentation styles
func! ToggleIndentStyle()
    if &expandtab == 1
        set noexpandtab
        set softtabstop&
        set shiftwidth&
        echom "Switched to: Indent with tabs."
        set expandtab
        set softtabstop=4
        set shiftwidth=4
        echom "Switched to: Indent with 4 spaces."
noremap <C-_> :call ToggleIndentStyle()<CR>

Indenting with tabs is the default in Vim, so all I need to do to switch to indenting with tabs is to unset my settings for softtabstop and shiftwidth as well as unset expandtab so that Vim won’t expand my Tab keypresses into space characters. To switch back to spaces, I revert to my normal settings.

I chose the Ctrl+_ keybinding because it was free and the underscore is reminiscent of a “space” character so it would be easy to remember.

Honestly, I do not strongly lean one way or the other in the indentation camp. For some languages, such as Common Lisp, using spaces for indentation also allows consistent alignment without mixing spaces and tabs. For other languages, either spaces or tabs are required by the language’s style guides like Python or Go respectively. For most languages, though, it makes no difference.

I have heard murmurings of tabs being better for accessibility, but I’ve never seen this claim backed up with hard evidence (I have seen anecdotal evidence for both sides). If you have compelling arguments for one style over another, I’d like to hear them.

This is my thirty-sixth post for the #100DaysToOffload challenge. You can learn more about this challenge over at