I use 2 spaces as indentation. So I set shiftwidth using :set sw=2.

However when I do the shift operator(>>) on a line twice, the indentation converts the 4 spaces automatically to a tab. So to avoid this, I set tabstop to a high value say 60 using :set ts=60 and the shift operator works (does NOT convert space to tab).

My question is, Is this the right way to do? Am I missing a simpler solution?

3 Answers 3


Set the 'expandtab' option setting. This will prevent shifting from replacing indent by using TABs.


I have two functions that swap back and forth between tabs and spaces, as well, if interested.

function! Tabstyle_tabs()
  " Default to using 8 column tabs
  set softtabstop=8
  set shiftwidth=8
  set tabstop=8
  set noexpandtab

function! Tabstyle_spaces()
  " Default to using 4 column indents
  set softtabstop=4
  set shiftwidth=4
  set tabstop=4
  set expandtab

You can set the 8 and 4 to whatever sizes you like of course.
I'd started to work on a function that takes variable size tabstop, but haven't bothered, as I use autocmds for different file types and this use these functions only when necessary otherwise.

  • 1
    Minor point: you don't need to set shiftwidth and softtabstop in your functions. If you :set shiftwidth=0 softtabstop=-1 then they will both use the value of tabstop.
    – Rich
    Jan 29, 2018 at 16:22

The YAIFA plugin may be of interest.

It tries to auto-detect the indentation method used in the current file, and set your expandtab tabstop and shiftwidth options appropriately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.