vimtricks.wiki Concise Vim tricks, one at a time.

How do I configure Tab and Backspace to always move by a fixed number of spaces when using expandtab?

Answer

:set softtabstop=4

Explanation

When expandtab is enabled, Vim inserts spaces instead of a real tab character, but without softtabstop, Backspace only deletes one space at a time — not a full indent level. Setting softtabstop to match your shiftwidth makes Tab insert exactly that many spaces and Backspace delete an entire indent unit at once, giving you the fluid indentation feel that most editors provide by default.

How it works

  • tabstop — how wide a literal \t character appears on screen (display only)
  • shiftwidth — how far >>, <<, and autoindent move text
  • softtabstop — how many spaces Tab inserts and Backspace removes in insert mode

When softtabstop=4 and expandtab is on:

  • Pressing Tab inserts 4 spaces
  • Pressing Backspace at the start of an indented line removes 4 spaces at once
  • If the cursor is mid-indent, Backspace removes enough spaces to reach the previous tab stop

Example

Without softtabstop (or softtabstop=0):

    return value  ← Backspace → ...   return value  (removed 1 space)

With softtabstop=4:

    return value  ← Backspace → return value  (removed entire 4-space indent)

Tips

  • The standard setup for Python and many other languages:
    set tabstop=4 softtabstop=4 shiftwidth=4 expandtab
    
  • Set softtabstop=-1 to automatically mirror shiftwidth, so you only need to maintain one value
  • Add filetype-specific settings in ~/.vim/ftplugin/python.vim (or equivalent) to avoid overriding other filetypes
  • set shiftwidth=0 makes shiftwidth mirror tabstop — combine with softtabstop=-1 for a single-value indent system

Next

How do I permanently add a word to my personal spell file so Vim stops marking it as misspelled?