Hien
Hien

Reputation: 1779

How to 'tab backwards' (remove a tab or tab spaces) in Vim?

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

Upvotes: 71

Views: 87553

Answers (5)

Pluto
Pluto

Reputation: 3026

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.

Upvotes: 0

Orwellophile
Orwellophile

Reputation: 13933

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

Upvotes: 10

Ted Naleid
Ted Naleid

Reputation: 26801

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.

Upvotes: 144

ZyX
ZyX

Reputation: 53614

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.

Upvotes: 22

Matt Briggs
Matt Briggs

Reputation: 42178

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

Upvotes: 3

Related Questions