71

Is there a fast way to tab backward without pressing backspace (however many number of spaces for which I've set my tab space)?

3
  • I'm not sure what you mean by 'tab backwards'. Do you mean in edit mode to delete spaces that you've just inserted with tab or in exec mode to move back by more than one character at the time (backspace moves back by one char at the time ...) ?
    – stefanB
    Commented Dec 30, 2010 at 4:08
  • 1
    The fastest way is to actually use tab characters for indentation. Then you have (with autoindent) one single keypress for all your indentation level shifts. In edit mode: One more level, press tab. Going back one level, press backspace. In command mode: arrow keys or h/l.
    – hlovdal
    Commented Jul 24, 2011 at 6:37
  • @hlovdal That can also be done by setting softtabstop without having to use actual tab characters in the file.
    – melpomene
    Commented Jul 23, 2016 at 20:45

5 Answers 5

141

If you're in insert mode:

  • Ctrl+d - shift left
  • Ctrl+t - shift right

If you're in normal mode:

  • Shift+<< - shift current line left
  • Shift+>> - shift current line right

If you're in visual mode and have 1 or more lines selected:

  • < - shift selection left
  • > - shift selection right

If you mean just to move backwards a word in normal mode, you can use b to go backwards a word.

5
  • 8
    didn't know that about insert mode Commented Dec 30, 2010 at 7:04
  • Wow, thanks. I knew about the << and >>, but those shifts whole line
    – Hien
    Commented Dec 30, 2010 at 7:05
  • 5
    I mapped Shift+Tab to C-d to make it work like it does in most IDEs.
    – Mo2
    Commented Apr 14, 2015 at 17:27
  • 2
    Since it took me a few tries to make @Mo2 's suggestion work, here's how you map Shift+Tab to Ctrl+d: inoremap <S-Tab> <C-d>
    – Leon S.
    Commented Aug 24, 2017 at 8:19
  • I wish I knew it earlier. Thank you so much Commented Mar 2, 2021 at 13:28
22
set softtabstop=4 expandtab

and you will be able to add up to four spaces when you press tab and remove up to four spaces by pressing <BS> once.

1
  • 1
    This answer should be selected instead of the existing selected one. + Very beginners may not know that this command should be entered in ~/.vimrc, so be sure to let them know. Commented Jan 22, 2020 at 1:06
8

tl;dr: set tabstop=4 softtabstop=-1 shiftwidth=0 expandtab

short form: set ts=4 sts=-1 sw=0 et

Explanation

If you set softtabstop (or sts) to -1 it will automatically behave the same as tabstop (ts), which will save you some hassle if you change tabbing a lot. Setting shiftwidth (sw) to 0 should effectively make that the same as tabstop as well.

In Detail

shiftwidth sw

Number of spaces to use for each step of (auto)indent. Used for cindent, >>, <<, etc.
When zero the tabstop value will be used.

tabstop ts

Number of spaces that a in the file counts for. Also see :retab command, and softtabstop option.

softtabstop sts

Number of spaces that a Tab counts for while performing editing operations, like inserting a Tab or using BS. It feels like Tabs are being inserted, while in fact a mix of spaces and s are used. This is useful to keep the tabs is setting at its standard value, while being able to edit like it is set to sts. When sts is negative, the value of shiftwidth is used. This will save you some hassle if you change tabstops a lot. When expandtab is not set, the number of spaces is minimized by using Tabs.

expandtab et

In Insert mode: Use the appropriate number of spaces to insert a . Spaces are used in indents with the > and < commands and when autoindent is on. To insert a real tab when expandtab is on, use Ctrl-V Tab. See also :retab

3

in normal mode, << will tab the current line back one, in visual mode, < will make all selected lines tab back once

0

This question has better answers for this in my opinion. Instead of using the backspace key to clear out 4 spaces at a time, shift+tab would be a great keyboard shortcut to use (since the shift modifier typically reverses the behavior of another shortcut). All other text editing software I'm familiar with uses this, but vim does not. However it's trivial to add this, and it means softtabstop doesn't need to be used:

set tabstop=4 shiftwidth=0 expandtab
inoremap <S-Tab> <C-d>

You can then use backspace to delete individual spaces and shift+tab to delete sets of 4 tabs when in insert mode. It also works if you want to stick to using tab characters.

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.