When I'm in insert mode and I have the expandtab
option switched on, pressing Tab ↹ results in inserting the configured number of spaces.
But occasionally I want to insert an actual tab character.
Do you know how to do this?
Join Stack Overflow to learn, share knowledge, and build your career.
You can use <CTRL-V><Tab>
in "insert mode". In insert mode, <CTRL-V>
inserts a literal copy of your next character.
If you need to do this often, @Dee`Kej suggested (in the comments) setting Shift+Tab to insert a real tab with this mapping:
:inoremap <S-Tab> <C-V><Tab>
Also, as noted by @feedbackloop, on Windows you may need to press <CTRL-Q>
rather than <CTRL-V>
.
.
to repeat the last command if you want to insert multiple tabs.
– Xavier T.
Jan 24 '11 at 10:53
CTRL
+ Q
instead - see stackoverflow.com/questions/6951672/…
– feedbackloop
Jan 15 '12 at 1:12
inoremap <S-Tab> <C-V><Tab>
-- You can put it in your ~/.vimrc
file.
– Dee'Kej
Sep 30 '15 at 12:54
You can disable expandtab option from within Vim as below:
:set expandtab!
or
:set noet
PS: And set it back when you are done with inserting tab, with "set expandtab" or "set et"
PS: If you have tab set equivalent to 4 spaces in .vimrc (softtabstop), you may also like to set it to 8 spaces in order to be able to insert a tab by pressing tab key once instead of twice (set softtabstop=8).
From the documentation on expandtab
:
To insert a real tab when
expandtab
is on, useCTRL-V
<Tab>
. See also:retab
and ins-expandtab.
This option is reset when thepaste
option is set and restored when thepaste
option is reset.
So if you have a mapping for toggling the paste
option, e.g.
set pastetoggle=<F2>
you could also do <F2>Tab<F2>
.