377

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?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
devemouse
  • 5,302
  • 5
  • 22
  • 22

3 Answers3

557

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

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • 33
    And then use `.` to repeat the last command if you want to insert multiple tabs. – Xavier T. Jan 24 '11 at 10:53
  • 10
    I had some problems using this combination because I was in Command mode. Make sure you execute this command in EDIT mode. – mgfernan Jun 06 '11 at 09:35
  • 3
    Note: If you're using gVim on Windows, you'll likely need to use `CTRL` + `Q` instead - see http://stackoverflow.com/questions/6951672/how-can-i-insert-a-real-tab-character-in-vim – feedbackloop Jan 15 '12 at 01:12
  • 1
    What if I got mapped? – Kan Li Jul 16 '13 at 20:28
  • If you've mapped , you should really unmap it. Otherwise you're going to need to use horrid hacks for entering non-standard characters (Unicode etc, not just tabs). One hack that kind of works is `imap xx :let @a="\t""api` which will allow you to type `xx` and have it expand to a real tab. (Note that `imap xx ` will not work as the tab will get expanded). – Michael Anderson Jul 17 '13 at 02:49
  • 9
    I've just my a simple shortcut for this case and it seems to be working (not breaking anything else): `inoremap ` -- You can put it in your `~/.vimrc` file. – Dee'Kej Sep 30 '15 at 12:54
  • 1
    @Dee'Kej yep that seems sane, and worthwhile if you often need to insert real tabs. I'm going to add it to the body so its up there for everyone to see. – Michael Anderson Oct 01 '15 at 00:31
  • @Dee'Kej your key mapping doesn't seem to work for me. Pressing Shift+Tab doesn't do anything. Maybe it has to do with vim not sending the Shift+Tab key combo? – Tri Nguyen May 24 '16 at 05:37
  • 2
    @TriNguyen Its likely that your OS is using Shift+Tab for something else, and vim is never seeing it. You may be able to disable this key binding when vim is active, or just choose something your OS is not using. – Michael Anderson May 24 '16 at 06:10
  • I have `set expandtab` in my `~/.vimrc`, so in Insert mode the `` keypress inserts spaces when `` is pressed. Adding `inoremap ` to my `~/.vimrc` allows me to insert tabs in Insert mode, using the "Shift-tab" keypress. – Victoria Stuart Jun 01 '18 at 16:24
  • @VictoriaStuart I also use `expandtab` - but for me `` works just fine without any need for a fix. What vim version are you using? Maybe something has changed between versions? Or maybe somethng else you've got installed is interfering? – Michael Anderson Jun 04 '18 at 23:18
  • Neovim (NVIM v0.2.2) but my ~/.vimrc is huge, so that is likely the difference. – Victoria Stuart Jun 05 '18 at 00:26
41

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

Jahanzeb Farooq
  • 1,948
  • 4
  • 27
  • 27
2

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, e.g.

set pastetoggle=<F2>

you could also do <F2>Tab<F2>.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378