How to insert Tab character when expandtab option is ON in VIM
When I'm in INSERT mode and I have expandtab option switched on pressing tab results in inserting configured number of spaces.
But occasionally I want to insert tab character itself.
Do you know how to do this?
You can use <CTRL-V><Tab>
in "insert mode". In insert mode <CTRL-V>
inserts a literal copy of your next character.
If you find you need to do this often, @Dee`Kej suggested (in the comments) setting Shift+Tab to insert a real tab by
:inoremap <S-Tab> <C-V><Tab>
Also as, noted by @feedbackloop, on windows you may need to press <CTRL-Q>
rather than <CTRL-V>
.
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, use CTRL-V
<Tab>
. See also :retab
and ins-expandtab.
This option is reset when the paste
option is set and restored when the paste
option is reset.
So if you have a mapping for toggling the paste
option, eg
set pastetoggle=<F2>
you could also do <F2>Tab<F2>
.
上一篇: 如何在vim中剪切整行并粘贴它?