203 votes
Accepted

Displaying tabs as characters

Yes, use listchars: set list set listchars=tab:>- If you put these two lines in your .vimrc, tabs will be shown as > for the start position and - through the rest of the tab. (Sidenote: ...
  • 13.9k
136 votes
Accepted

How to replace tabs with spaces?

You can use the :retab command. From :help :retab Replace all sequences of white-space containing a <Tab> with new strings of white-space using the new tabstop value given. If you do not ...
  • 2,251
73 votes

Displaying tabs as characters

The basic answer is :set list, which causes tabs to display as ^I. However, I recommend going beyond that. Just :set list is problematic in that it fails to preserve visual alignment on screen. For ...
  • 8,859
40 votes
Accepted

Insert tabs in INSERT mode when expandtab is set

Instead of just pressing Tab, first press Ctrl-V and then press Tab. This can be used to insert a variety of special chars. See :help i_CTRL-V for details. Ctrl-V also works in command-line mode (:...
  • 28.9k
35 votes

Displaying tabs as characters

Highlighting tabs Temporary Tab highlighting For occasional use, one can simply highlight all tabs in a document using the following search / command: /\t To remove the highlighting, simply type :noh ...
30 votes

How to replace tabs with spaces?

You can use :retab, as stated, however, this will change all tabs to spaces, not only tabs at the start of the line So this (where ⇥ is a tab character): if :; do ⇥echo "⇥hello" end gets changed to ...
29 votes
Accepted

How can I show tabs as spaces instead of ^I or CTRL-I?

'listchars' takes two characters for tab so you can simply use two regular spaces: set listchars=tab:\ \ ,trail:·,eol:¬,nbsp:_
  • 33.3k
28 votes
Accepted

Setting TAB to 2 spaces

Have you sourced your .vimrc since making these changes? When having vim open just type source ~/.vimrc (assuming it is in it's default location) EDIT: This could help too
  • 451
27 votes

How to replace tabs with spaces?

Vim provides !retab command which will replace all sequences of <Tab> with new strings of white-space using the new tabstop (e.g. :set tabstop=2) value given, but all tabs inside of strings can ...
  • 16.9k
27 votes
Accepted

What is `softtabstop` used for?

As the softtabstop documention mentions, it's useful if you want to keep the default tab stop size of 8, but edit a file as if the tab stop size was some other value. For example, if you wanted an ...
  • 990
25 votes

Insert tabs in INSERT mode when expandtab is set

Rather than change your behavior (which is error prone). I would set up vim to behave differently for this file type. So in my normal editing I expand tabs. But specifically for make files I set the ...
  • 5,910
18 votes

Elastic Tabstops in Vim?

As far as I can tell there is no reasonable way to accomplish this in a plugin. There is a patch for it, but it's hard to predict when / if the patch will get merged.
  • 3,638
18 votes

What is `softtabstop` used for?

The value of 'softtabstop' equals how many columns (=spaces) the cursor moves right when you press <Tab>, and how many columns it moves left when you press <BS> (backspace) to erase a tab. ...
  • 281
17 votes
Accepted

Vim doesn't use the correct indentation in Python files

It gets reset by the Python filetype plugin; from /usr/share/vim/vim74/ftplugin/python.vim: " As suggested by PEP8. setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8 This file is loaded ...
11 votes
Accepted

How can I make Vim position the cursor at the start of a tab character instead of the end?

It's only a clumsy workaround, but if you :set list the cursor will be positioned on the other "side" of the tab.
10 votes

Displaying tabs as characters

I personally like the use of the plugin indentLine, which displays a vertical line at indentation levels. It is of great help especially in python, where the indentation is important. Among other ...
  • 919
9 votes

Elastic Tabstops in Vim?

From the comments: since version 8.1.105, vim has +vartabs compile-time feature, thanks to Christian Brabandt ! As far as I can see, this allows implementing elastic tabstops. See the actual pull ...
  • 203
9 votes

Transforming a long table into multiple columns

If you want to do that in vim you can use the following macro: qa^V3j$d"_4dd^V3jI<tab><Esc>Pq Which can be decomposed like this: qa Record the macro in the register a ^V ...
  • 46.8k
8 votes
Accepted

How can I set expandtab but only for git commit messages?

First possibility Add the following line to your .vimrc: autocmd FileType gitcommit setlocal expandtab Second possibility Create a file ~/.vim/after/ftplugin/gitcommit.vim and in there put: :...
7 votes

What is `softtabstop` used for?

See the example below, // Start vim without loading your vimrc. set only tabstop=8 softtabstop=4. // This makes <Tab> in insert mode equals to 4 <Space> length at max. // In insert mode, ...
  • 101
7 votes
Accepted

Why are <tab> characters variable width?

A tab advances to the next column whose position is a multiple of the tab stop. So, with tabstop=4, if your cursor is in column 7 and you press Tab, the cursor will advance only one column to column 8,...
  • 990
7 votes
Accepted

Expand <TAB> into 2 spaces when and only when editing HTML

Put this in your .vimrc file: autocmd BufRead,BufNewFile *.htm,*.html setlocal tabstop=2 shiftwidth=2 softtabstop=2 Briefly, autocmds get processed when the specified events occur for the specified ...
  • 18.5k
6 votes

How can I make Vim position the cursor at the start of a tab character instead of the end?

The cursor in normal mode is on the character not to either side. So you can do i and a to go to insert mode to the left or right of the tab respectively.
6 votes

softtabstop == tabstop VS softtabstop=0

As mentioned in :h softtabstop, the softtabstop indicates how many columns vim uses when you enter <Tab> in insert mode. Although it performs like <Tab> inserted, in fact, the vim mixes &...
  • 61
5 votes

Displaying tabs as characters

Easiest method is to do :set list, which will show tabs as ^I and end of line as $. I like to use a mapping that calls :set invlist to toggle between regular display and list display. For example:...
  • 8,112
5 votes

Displaying tabs as characters

You may want to show tabs differently in a regular terminal and gvim. set list! if has('gui_running') set listchars=tab:▶\ ,trail:·,extends:\#,nbsp:. else set listchars=tab:>.,trail:.,...
  • 3,443
5 votes
Accepted

How to check if the character under the cursor is a tab?

Just compare the character you got with a tab, which can be represented with the \t escape sequence. That is, getline(".")[col(".")-1] == "\t" The expression will return 1 if the character is tab, 0 ...
  • 266
5 votes
Accepted

Fixing tab and space inconsistency

The problem occurred because there is an inconsistency between the spaces and tabs used for indentation in your code. You have probably opened a tab indented code, and started editing it, while vim ...
4 votes

How can I remove additional whitespace in the middle of line?

This command will remove all whitespaces on both sides of one or more tabs in every line of the current buffer: :%s/ *\([\t]\+\) */\1/g
4 votes

Don't show tab characters as ^I or <09> in the output of :registers

You chose to have them displayed that way by adding the uhex setting to the 'display' option. You can :set display-=uhex to restore the default behavior of displaying them as ^I. Getting :reg to ...
  • 10.4k

Only top scored, non community-wiki answers of a minimum length are eligible