Is there a fast way to tab backward without pressing backspace (however many number of spaces for which I've set my tab space)?
5 Answers
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.
-
8
-
Wow, thanks. I knew about the << and >>, but those shifts whole line– HienCommented Dec 30, 2010 at 7:05
-
5I mapped Shift+Tab to C-d to make it work like it does in most IDEs.– Mo2Commented Apr 14, 2015 at 17:27
-
2Since 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 -
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.
-
1This 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
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 thetabstop
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 tosts
. Whensts
is negative, the value ofshiftwidth
is used. This will save you some hassle if you change tabstops a lot. Whenexpandtab
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 whenexpandtab
is on, use Ctrl-V Tab. See also :retab
in normal mode, <<
will tab the current line back one, in visual mode, <
will make all selected lines tab back once
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.
softtabstop
without having to use actual tab characters in the file.