Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Vim show ALL white spaces as a character

People also ask

How do I check space in Vim?

Upgrading my gvim solved my problem. For others that are getting an error, type :help listchars to check whether "space" is a valid option for "listchars". If it is not, upgrade your vim to the patch @brettanomyces specified. Or just leave "space" out of the set command if you don't care to see spaces.

How do I show special characters in vi?

To find a character string, type / followed by the string you want to search for, and then press Return. vi positions the cursor at the next occurrence of the string. For example, to find the string “meta,” type /meta followed by Return.

How do I Retab in Vim?

vim Whitespace Convert tabs to spaces and spaces to tabs If you enable expandtab again :set expandtab then and run the :retab! command then all the tabs becomes spaces. If you want to do this for selected text then first select the text in visual mode.

How do I view Crlf in vi?

vi shows newlines (LF character, code x0A ) by showing the subsequent text on the next line. Use the -b switch for binary mode. For example , vi -b filename or vim -b filename -- . It will then show CR characters ( x0D ), which are not normally used in Unix style files, as the characters ^M .


As others have said, you could use

:set list

which will, in combination with

:set listchars=...

display invisible characters.
Now, there isn't an explicit option which you can use to show whitespace, but in listchars, you could set a character to show for everything BUT whitespace. For example, mine looks like this

:set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<

so, now, after you use

:set list

everything that isn't explicitly shown as something else, is then, really, a plain old whitespace.

As usual, to understand how listchars works, use the help. It provides great information about what chars can be displayed (like trailing space, for instance) and how to do it:

:help listchars

It might be helpful to add a toggle to it so you can see the changes mid editing easily (source: VIM :set list! as a toggle in .vimrc):

noremap <F5> :set list!<CR>
inoremap <F5> <C-o>:set list!<CR>
cnoremap <F5> <C-c>:set list!<CR>

As of patch 7.4.710 you can now set a character to show in place of space using listchars!

:set listchars+=space:␣

So, to show ALL white space characters as a character you can do the following:

:set listchars=eol:¬,tab:>·,trail:~,extends:>,precedes:<,space:␣
:set list

When you are finished, to hide the non-visible chars you would:

:set nolist

Discussion on mailing list: https://groups.google.com/forum/?fromgroups#!topic/vim_dev/pjmW6wOZW_Q


:set list to enable.

:set nolist to disable.


I think other answers here are more comprehensive, but I thought I'd share a trick I usually use to differentiate tabs and spaces visually:

:syntax on
:set syntax=whitespace

These are syntax highlighting rules for the Whitespace programming language - tabs show in green and spaces in red. :)

Can be combined with :set list as mentioned by many other answers, although the tabs will then show as ^I without a green higlight, but the spaces will show in red.


:set list will show all whitespaces as a character. Everything but a space will look different than its normal state, which means that if you still see a plain old space, it's really a plain old space. :)


If you set:

:highlight Search cterm=underline gui=underline ctermbg=none guibg=none ctermfg=none guifg=none

and then perform a search for a space, every space character will be shown as an underline character.

You can use this command in a handy function that toggles "underscoring" of spaces.

set hls
let g:HLSpace = 1
let g:HLColorScheme = g:colors_name
function ToggleSpaceUnderscoring()
    if g:HLSpace
        highlight Search cterm=underline gui=underline ctermbg=none guibg=none ctermfg=none guifg=none
        let @/ = " "
    else
        highlight clear
        silent colorscheme "".g:HLColorScheme
        let @/ = ""
    endif
    let g:HLSpace = !g:HLSpace
endfunction

Map the function to a shortcut key with:

nmap <silent> <F3> <Esc>:call ToggleSpaceUnderscoring()<CR>

NB: Define the function in vimrc after the colorscheme has been set.