How to Visualize Tabs in Vim

(This is an excerpt taken from the post: Tabs & Spaces in Vim: How to Make Conscious Use of Both)

It will be presented two ways to visualize Tabs in Vim.

Searching for the Tab Character

A quick way to visualize whether there is a Tab character is by searching for it using Vim’s search-commands:

  • In NORMAL mode, type /\t and hit <Enter>. It will search for the Tab character (\t) and highlight the results.

Although it may be good for a quick check, if you need persistent Tabs visibility plus the ability to use the search-commands for other purposes, you might need another solution.

Activating list Mode

Vim’s list mode displays on screen unprintable characters (<Tab>, EOF, EOL, etc…) with strings defined by the listchars option.

By default, it will display ^I for a Tab character but this default representation breaks screen alignment so, the suggestion is to set a string representation to be used for the Tab character:

  • In NORMAL mode, type :set listchars=tab:▷▷⋮ or add set listchars=tab:▷▷⋮ to your .vimrc file.

The command above defines the strings that Vim will display (▷▷⋮) for a Tab character. Vim’s behavior is to repeat or omit the second character (), which means:

A Tab character on a file that the indentation is set to occupy two screen spaces, will display ▷⋮.

A Tab character on a file that the indentation is set to occupy four screen spaces, will display ▷▷▷⋮.

  • Toggle list mode by typing :set invlist in NORMAL mode.

Extra: Create a Mapping to Toggle list Mode Quickly

Add the following line to your .vimrc file:

noremap <Leader><Tab><Tab> :set invlist<CR>

(You may substitute <Leader><Tab><Tab> as you wish. If you’d like to know more about Vim mappings, please check this post.)

Do you have any other tip about visualizing Tabs in Vim? Please leave a comment!

Thanks for reading 🙌 !

44