107

When I have my vimrc here:

set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
set smarttab

And I have supertab plugin installed. Whenever I am in insert mode I press tab, it shows the auto completion, but sometimes I would like to insert a real tab character in a string literal like. So what I mean whenever I press tab in double quotes string literal, it should input we a real tab character.

Samnang
  • 5,536
  • 6
  • 35
  • 47

2 Answers2

194

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.

UPDATE:

As noted by Herbert Sitz, if gVim is in Windows mode (default), you must use CRTL + Q in place of CTRL + V.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
  • 2
    That's it, but on Windows if Win compatibility is set on in vimrc (which is the default) you need to do `CTRL` + `Q` then `TAB`. – Herbert Sitz Aug 06 '11 at 23:05
  • @Herbert Sitz: Good point. I forgot all about Windows mode for gVim. I'll note that as well. – Jason Down Aug 07 '11 at 12:30
  • CTRL+Q simply puts me into visual block mode so a subsequent TAB does nothing. Am I missing something here? Something wrong with my configuration maybe? – Ben Franklin Mar 14 '16 at 11:45
  • @BenFranklin You have to be in last line mode (the command prompt with : at the bottom of the screen) or in insert mode for CTRL+V (or Q) to take the next character literally. When you are in the default command mode, it will put you in block mode as you have noted. – Jason Down Mar 14 '16 at 17:42
7

@Samnang: I have a similar setup as you; unfortunately, Jason's answer did not work, for me.

This is a workaround:

  • Substitute some character (e.g. a backtick: `) or characters (e.g. a unique alphanumeric string: zzz) where you want your tab(s)

  • Select the text (Visual mode) and do a search/replace,

    :'s/`/\t/g

Updated answer, inspired by @Cyryl1972 's comment.

To insert a tab at beginning of all lines (note also: no need to select lines, for any of the following code, as that's included in the line matching part of the expression):

:1,$s/^/\t\1/

Insert tab after first 10 characters in all lines:

:1,$s/^\(.\{10}\)/\1\t/

Explanation - first part:

:1,$      Match from line 1 to end of file
^(.{10}   Collect (preserve) all text from beginning of line to position 10
          (you need to escape the parentheses, \( and \), as well the FIRST
          (left) curly brace, only: \{ -- as it, { , appears to have special
          meaning in regex when used for this purpose

Explanation - second part:

/1        Add back the preserved text
\t        Insert a tab

... and the rest of the line is automatically restored, as well.

Current line, only:

:s/^/\t\1/

Example: insert tab at position 10 (0-indexed) at lines 2-4:

1234567890abcdefghij 
1234567890abcdefghij 
1234567890abcdefghij 
1234567890abcdefghij 
1234567890abcdefghij 

:2,4s/^\(.\{10}\)/\1\t/

1234567890abcdefghij 
1234567890  abcdefghij 
1234567890  abcdefghij 
1234567890  abcdefghij 
1234567890abcdefghij 

References (StackOverflow):

References (other):

Victoria Stuart
  • 4,610
  • 2
  • 44
  • 37
  • the solution works too: :1,$s/^\(.\)/\t\1/ (insert a tabulation before the first character of all lines) – Cyryl1972 Nov 08 '17 at 09:21
  • @Cyryl1972 Fixing it it should be: `:1,$s/^/\t`. If you want to waste the resources for capturing you'd have to do: `1,$s/^\(.\)/\t\1` but this is entirely unnecessary and so pointless. – Pryftan Nov 09 '22 at 11:11
  • I don't like modifying other's answers but you made a typo it seems. You wrote: `/1` ... Add back the preserved text ... but it should be `\1`. – Pryftan Nov 09 '22 at 11:13
  • Of course @Cyryl1972 a further optimisation is using `%` instead of `1,$` of course like `:%s/^/\t/`. – Pryftan Nov 09 '22 at 11:19