DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for How to Visualize Tabs in Vim
Jonas B. R.
Jonas B. R.

Posted on • Updated on

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.

gif01

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.

gif02

Extra: Create a Mapping to Toggle list Mode Quickly

Add the following line to your .vimrc file:

noremap <Leader><Tab><Tab> :set invlist<CR>
Enter fullscreen mode Exit fullscreen mode

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


Check the full post about Tabs & Spaces in Vim to know more.

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

Thanks for reading ๐Ÿ™Œ !

Top comments (3)

Collapse
 
jingxue profile image
Jing Xue

The list mode solution is a nice trick!

Collapse
 
rossijonas profile image
Jonas B. R. Author

I am glad it helped! ๐Ÿ™Œ

Collapse
 
waylonwalker profile image
Waylon Walker

invisible characters can be a nightmare. The worst time sucks are those brought on by mixing tabs and spaces or windows style line endings.

๐ŸŒš Friends don't let friends browse without dark mode.

Sorry, it's true.