Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert tab character when expandtab option is on in Vim

Tags:

vim

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?

like image 389
devemouse Avatar asked Jan 24 '11 10:01

devemouse


People also ask

How do I insert a tab in Vim?

While in insert mode or command mode (the : prompt at the bottom of the editor), type CTRL + V then TAB . Using CTRL + V signals Vim that it should take the next character literally. Even in insert mode.

What does the Tab key do in Vim?

Use tab key to switch windows and current file path.

What is smart tab Vim?

Smart tabs plugin or the newer vim-stabs plugin (which stands for smarter-tabs). The plugin ensures that tabs are only used for indentation, while spaces are used everywhere else.


Video Answer


2 Answers

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>.

like image 170
Michael Anderson Avatar answered Sep 24 '22 15:09

Michael Anderson


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).

like image 35
Jahanzeb Farooq Avatar answered Sep 24 '22 15:09

Jahanzeb Farooq