36

I'm currently trying to switch from gedit to vim. I try to make vim look similar to gedit:

gedit

Especially, I would like to show spaces with dots.

I currently have:

enter image description here

There are some differences, but it looks quite similar (this is my current .vimrc file). But I don't get those dots for spaces.

How can I make vim show spaces?

I found some answers (like this one) that seem to suggest to replace spaces by a special visible character and then replace it back. I don't want to do this every time. I want to be able to open vim and see those spaces.

Community
  • 1
  • 1
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • Vim Wikia article on [highlighting unwanted spaces](http://vim.wikia.com/wiki/Highlight_unwanted_spaces) might get you started... – Mark Rushakoff Apr 09 '13 at 14:42
  • I don't think it can be highlighted as a character, but you can highlight the background rather easily – Explosion Pills Apr 09 '13 at 14:45
  • possible duplicate of [Make Vim show ALL white spaces as a character](http://stackoverflow.com/questions/1675688/make-vim-show-all-white-spaces-as-a-character) – Hauleth Apr 09 '13 at 15:07
  • 1
    "see those spaces" for what purpose? – romainl Apr 09 '13 at 15:11
  • mainly because I'm used to see them – Martin Thoma Apr 10 '13 at 09:47
  • Type ":set list" or ":unset list" to show/hide non-space whitespace, keeping you sane in python under vim. But what you _really_ want is to switch to PyCharm. Honestly. It is the only way to code python. Says who? I do. Opinion. Sorry. But a strong one, from a longterm Linux fanboy who codes a lot of python and does almost everything else in vim. – Markus-Hermann Nov 12 '20 at 06:37

8 Answers8

61

Show non-space whitespace with

set list

Additionally show spaces as . with

set lcs+=space:·

Turn it off with

set nolist
Community
  • 1
  • 1
Cedric Simon
  • 735
  • 5
  • 11
37

There are times when I absolutely need to see which whitespaces are tabs and which are true space characters, as well as when spaces appear at the end of a line.

When I'm using vim, I use:

:set list

(which I can turn off with :set nolist)

With :set list, literal spaces show up as spaces, tabs show up as ^I, and the end-of-line shows up as a bright-pink $. (So any blank space you see to the left of the pink $ you'll know is a true space character, and the blank spaces to the right of it is just the "nothing" between the end-of-line and the right side of the editor.)

It's ugly, but it works well.

I don't recommend using it all the time -- just those times when it is crucial to see where literal spaces and literal tab characters exist.

J-L
  • 1,786
  • 10
  • 13
13

While you can't do exactly what you want here, if your reasoning is like mine, you're wanting to see those spaces because it helps verify proper indentation (do I have 2 spaces there, or is it 3?). For Vim >= 7.3, you can use the indentLine plugin to add a marker at each indentation. It's awesome.

To install if you're using Pathogen:

cd ~/.vim/bundle
git clone https://github.com/Yggdroot/indentLine

enter image description here

m59
  • 43,214
  • 14
  • 119
  • 136
6

It may be worth using undercurl to do the job.

hi WhiteSpaces gui=undercurl guifg=LightGray
match WhiteSpaces / \+/

or you can put this in your .vimrc

autocmd ColorScheme * highlight WhiteSpaces gui=undercurl guifg=LightGray | match WhiteSpaces / \+/ 

enter image description here

ernix
  • 3,442
  • 1
  • 17
  • 23
  • I've added these two lines to my `.vimrc`, but I don't get the spaces underlined. Do you know why? – Martin Thoma Apr 14 '13 at 17:25
  • Custom highlight settings will be cleared while changing `:colorscheme`. try following instead: autocmd ColorScheme * highlight WhiteSpaces gui=undercurl guifg=LightGray | match WhiteSpaces / \+/ – ernix Apr 15 '13 at 01:51
  • It still doesn't work. Do I perhaps need to install plugins? How do I do so? – Martin Thoma Apr 15 '13 at 03:58
  • No need any external plugins. Note that `undercurl` might be only available in GUI. – ernix Feb 11 '14 at 19:28
4

Show leading spaces (indent spaces) using the above mentioned plugin indentLine. If you use Vundle as plugin manager you can add Plugin 'Yggdroot/indentLine' to your .vimrc and then run vim +PluginInstall +qall to install the plugin.

Add the following two lines to your .vimrc to show leading spaces as ·.

let g:indentLine_leadingSpaceChar='·'
let g:indentLine_leadingSpaceEnabled='1'
patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35
chloesoe
  • 403
  • 4
  • 15
3

Vim has been providing the 'listchars' option to show Tab vs. Space, and space characters in critical places, i.e. trailing at the end of lines. In previous versions (when the question was written), it did not offer a modification for all spaces: a blank square is a space, period. Other answers provided some workarounds, though.

In current Vim versions, there are many space-related options; cp. :help 'listchars' for details.

You can get the effect seen in your screenshot; persist by putting that command into your ~/.vimrc:

:set list listchars+=space:. listchars-=eol:$
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Your answer seems to contradict ernix answer. Is your answer wrong? It this is the case, you might want to delete it. – Martin Thoma Apr 15 '13 at 04:39
  • ernix answer is just another variation of the linked workarounds. The fact that you're struggling to get it to work shows its limitations already. – Ingo Karkat Apr 15 '13 at 06:19
  • Oh, ok. I thought the reason why I am struggeling to get it to work might be that I am just beginning to learn vim. – Martin Thoma Apr 15 '13 at 07:19
2

Using the answers of J-L and Cedric Simon. Edit ~/.vimrc and add:

set lcs+=space:·
nmap <F2> :set invlist<CR>
imap <F2> <ESC>:set invlist<CR>a

and when you work with vim just pres F2

Junior Usca
  • 415
  • 7
  • 10
0

You don't need to install any plugin to this:

set listchars=tab:\|\ 
"set listchars=tab:\┊\ 
"set listchars=tab:\┆\ 
"set listchars=tab:\¦\ 
set list
Adrian Miranda
  • 315
  • 3
  • 9