How can I make vim show spaces? - vim

I'm currently trying to switch from gedit to vim. I try to make vim look similar to gedit:
Especially, I would like to show spaces with dots.
I currently have:
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.

Show non-space whitespace with
set list
Additionally show spaces as . with
set lcs+=space:·
Turn it off with
set nolist

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.

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

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 / \+/

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'

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

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:$

You don't need to install any plugin to this:
set listchars=tab:\|\
"set listchars=tab:\┊\
"set listchars=tab:\┆\
"set listchars=tab:\¦\
set list

Related

How to test if I my setting in vi is tabs or spaces

I set the setting in the vi file to spaces, and I want to verify that it is working correctly is there a way to test it ?
I know it is a trivial question but I am new to vi editor
take a look at :set list / :help list
you can set listchars like so:
set listchars=tab:>-,trail:~,extends:>,precedes:< " mark all kinds of whitespace
as an example, this setting will show tabs as > so you know where your tabs are.
useful links:
Make Vim show ALL white spaces as a character
http://vim.wikia.com/wiki/See_the_tabs_in_your_file

¬ at the end of each line in vim

this might be a silly question, but I couldn't find an answer to it. So, how can I add a ¬ to the end of each line of my vim files? It's more about styling, avoid trailing spaces, etc, like this colorscheme: http://ethanschoonover.com/solarized/img/solarized-dualmode.png
Thanks in advance.
set list
set listchars+=eol:¬
See :help 'list' and :help 'listchars'.
If you type in:
:set list
then vi should show you the end of each line with a $ character. Assuming all you want is to capture trailing space, that will do the trick. You can then turn it off with:
:set nolist
It actually gives you other things like making tabs visible as well, very handy when trying to track down Python problems with indentation, or things that print differently due to different tab width settings.
Of course, if you're looking to remove white space at line end, you can use regular expressions in your substitute commands:
:g/ *$/s///g
:set list listchars+=eol:¬
Or if you're just interested in seeing trailing spaces you could use:
:set list listchars+=trail:∙

What is the Dollar sign ("$") at the end of every line in Vim

I am relatively new to Vim. Whenever I start Vim using vim LearnRuby.rb, a dollar sign appears at every line.
Why?
:set nolist
will turn off special characters for the current buffer, such as tabs being presented as ^I and end of line characters showing up as $.
However, if it's doing that consistently when you run vim, you need to look into your .vimrc (or other startup file where applicable) and find out what's doing the set list that causes it.
Open ~/.vimrc and check its contents
If you see a line like this:
set list
It means, it will display $ in every line to mark the end of line.
Either remove it or use :set nolist command in the vi editor.
Obviously, the solution is not to :set nolist, as that also disables other characters like tabs and trailing spaces, which can be very useful. :set nolist would be the correct answer if you wanted all of the special characters gone.
If you just want a quick fix, you could add this to your vimrc instead:
set listchars+=eol:\ .
That is a backslash followed by a whitespace.
Alternatively you can set all the arguments of listchars, for exampleset listchars=tab:>\ ,trail:. and not include the eol argument, which determines the character used for end of line.
Refer to :h listhcars and :h list

What is Vim's feature name for this: # vim:sw=4:ts=4:et:

Even after 20 years with Vim, I keep forgetting name for the Vim feature where the editor picks up config statements from a comment at the beginning (or I think end) of a file:
# vim:sw=4:ts=4:et:
Thanks for a reminder!
It's called modeline
:he modeline
If you start editing a new file, and the 'modeline' option is on, a
number of lines at the beginning and end of the file are checked for
modelines. There are two forms of modelines.
The first form: [text]{white}{vi:|vim:|ex:}[white]{options}
[text] any text or empty
{white} at least one blank character (<Space> or <Tab>)
{vi:|vim:|ex:} the string "vi:", "vim:" or "ex:"
[white] optional white space
{options} a list of option settings, separated with white space or ':',
where each part between ':' is the argument for a ":set"
command (can be empty)
Add this to $MYVIMRC:
setglobal modeline
It's called modeline. In help it can be found by grepping
helpgrep # vim
If you wish to check whether modeline are active, do set modeline? (if the are it will say modeline, otherwise nomodeline)
To turn them off for certain, add this in your vimrc
:set modelines=0 "number of modelines vim parses
:set nomodeline "turn off parsing
:h 'ts (i.e. :help 'tabstop') will bring up a detailed explanation of how to use et, ts, and sw as well as giving a pointer to modeline (modeline is the option that is in question).

Showing trailing spaces in vim

I've set the following options in .vimrc
set listchars=tab:▸\ ,trail:·
set list
And expected to see dots in those places where spaces are used for tabulation in the code (I use spaces, not tabs).
However, the result is different:
Could you please recommend how to reach the desired result? Thanks!
You should check this link. I'm using the match command solution :
:highlight ExtraWhitespace ctermbg=red guibg=red
:match ExtraWhitespace /\s\+$/
This page also provides list based solutions which I haven't personally tried.
The vim-better-whitespace plugin incorporates many tips from the Vim Wiki page referenced in #icecrime's answer. It also has some nifty configuration options.
I installed pathogen.vim just to use this plugin and am happy with my life, all things considered.
And expected to see dots in those places where spaces are used for tabulation in the code (I use spaces, not tabs)
Actually this is the other way round, tab option is used to display a character when a tab character is inserted (\t) instead of spaces.
And trail is use to show trailing spaces at the end of lines.
You seem to have single empty line with trailing spaces, and dots are correctly displayed.
If you are only using spaces tab option is not used or displayed.
To highlight trailing whitespace characters:
:set hlsearch, then
/\s\+$
A more programmatic way to do this is via function matchadd():
hi TrailingWhitespace ctermbg=red guibg=red
call matchadd("TrailingWhitespace", '\v\s+$')
The 2nd paramter to matchadd() is the pattern we want to match. Here, we use single quote to avoid having to escape speical characters like backslashes etc, see also literal-string.
Based on the link posted by icecrime, I find this works quite well...
" Be clever about highlighting trailing whitespace (don't highlight it if we are
" in 'insert' mode and the cursor is at the end of the line). Also (regardless
" of 'insert' mode), highlight any tabs that immediately follow space(s).
" EOLWS and EOLWSInsert are colour group names; the latter being toned-down to
" make editing in 'insert' mode easier on the eye
autocmd InsertEnter * match EOLWS // | match EOLWSInsert /\s\+\%#\#<!$\| \+\ze\t/
autocmd InsertLeave * match EOLWSInsert // | match EOLWS /\s\+$\| \+\ze\t/
autocmd WinEnter,BufWinEnter,WinNew * match EOLWS /\s\+$\| \+\ze\t/
" Disable syntax-specific trailing space error handling because it conflicts
" with the above, mostly because the syntax highlighting doesn't take account of
" whether 'insert' mode is active or not. There are other '*_no_trail_space_error'
" settings - refer to syntax files in $VIMRUNTIME/syntax/
let c_no_trail_space_error = 1
let java_no_trail_space_error = 1
Also, make sure the 'Error' highlight group is NOT defined as inverse video - if it is, it conflicts on strange ways with the above
And expected to see dots in those places where spaces are used for tabulation in the code
Vim has 3 options for displaying spaces with listchars:
space show all spaces with the specified character.
lead Shows leading spaces, i.e. spaces at the starting of the line. I think this is what you want.
trail Shows trailing spaces, i.e. spaces at the end of the line.
There is also multispace but that is irrelevant to your problem.

Resources