Vim Tips Wiki
m (Duplicate added + Added to syntax)
(fixed function formatting - now using pre tags)
Line 18: Line 18:
   
 
------------------------------------------------
 
------------------------------------------------
  +
<pre>
 
 
function! ShowWhitespace()
 
function! ShowWhitespace()
   
Line 32: Line 32:
   
 
endfunction
 
endfunction
  +
</pre>
   
  +
<pre>
 
 
 
function! HideWhitespace()
 
function! HideWhitespace()
   
Line 48: Line 48:
   
 
endfunction
 
endfunction
  +
</pre>
   
  +
<pre>
 
 
 
function! ToggleShowWhitespace()
 
function! ToggleShowWhitespace()
   
Line 78: Line 78:
   
 
endfunction
 
endfunction
  +
</pre>
 
 
------------------------------------------------
 
------------------------------------------------
   
Line 84: Line 84:
   
   
  +
<pre>
 
 
highlight Tabs term=standout cterm=standout gui=standout
 
highlight Tabs term=standout cterm=standout gui=standout
   
 
highlight link LineEndWS Error
 
highlight link LineEndWS Error
  +
</pre>
   
   
Line 93: Line 94:
 
And some mappings (or autocommands):
 
And some mappings (or autocommands):
   
  +
<pre>
nmap &lt;Leader&gt;ws :call ToggleShowWhitespace()&lt;CR&gt;
+
nmap <Leader>ws :call ToggleShowWhitespace()<CR>
 
  +
</pre>
  +
<pre>
 
if has("autocmd")
 
if has("autocmd")
   
Line 100: Line 103:
   
 
endif
 
endif
  +
</pre>
 
}}
 
}}
   

Revision as of 07:02, 12 September 2007

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #1274 - Highlight some whitespace characters

Created: July 1, 2006 5:57 Complexity: basic Author: Andreas H�bner Version: 6.0 Karma: 3/9 Imported from: Tip#1274

Sometimes you work on multiple projects with many people and all of them have different opinions when it comes to the "tabs vs. spaces" debate.

To quickly spot if someone prefers to use tabs or spaces I wrote thos little functions (also highlights whitespace at end of line):



function! ShowWhitespace() 

 " show tabs and whitespace at eol 

 let b:showWS = 1 

 syntax match Tabs "\t" containedin=ALL 

 syntax match LineEndWS "\s\+$" containedin=ALL 

 echo "show Whitespace" 

endfunction 
function! HideWhitespace() 

 " hide tabs and whitespace at eol 

 let b:showWS = 0 

 syntax clear Tabs 

 syntax clear LineEndWS 

 echo "hide Whitespace" 

endfunction 
function! ToggleShowWhitespace() 

 if !exists('b:showWS') 

 let b:showWS = 0 

 endif 



 " toggle variable 

 let b:showWS = !b:showWS 



 if b:showWS == 1 

 call ShowWhitespace() 

 else 

 call HideWhitespace() 

 endif 

endfunction 

You will also need some highlighting:


highlight Tabs term=standout cterm=standout gui=standout 

highlight link LineEndWS Error 


And some mappings (or autocommands):

nmap <Leader>ws :call ToggleShowWhitespace()<CR>
if has("autocmd") 

 au VimEnter,BufRead * silent call ShowWhitespace() 

endif

Comments

VIM has built-in abilities to quickly spot if someone prefers to use tabs or spaces. You just need to set option 'list' and optionally option 'listchars':

set list set listchars=eol:$,tab:>-

So, -1

shvarts--AT--akmosoft.com , July 1, 2006 6:12


And this forum is meant to be friendly and helpful and not flame tips. So +4

anon , July 1, 2006 19:49


Thanks shvarts--AT--akmosoft.com :set list this is really great.. I once read someone said, take your cat to the vet, so I always do cat -vet filename but now I can do the same withing vim with :set list. Thanks very much! http:/

http://www.rocteur.cc , July 2, 2006 2:13


I took no offense from shvarts--AT--akmosoft.com, he is right: if vim provides ways to do the tip with builtins that's a -1. Thanks for pointing that out (the 'list' option). However, I don't like my screen getting filled with "virtual" (control) characters, so I still prefer the highlighting solution.

Andreas H�bner , July 2, 2006 2:26


Try this instead of "listchars=eol:$":

set listchars=trail:<C-K>M.

(digraph M-dot or what else you like)

Maybe you like tuning the SpecialKey highlight as well.

vim at bertram dash scharpf dot de , July 2, 2006 4:08


"set listchars=trail:<C-K>M. (digraph M-dot or what else you like) "

I can not get that to run. What keys do i need to press? (Sorry ;)

Anonymous , July 3, 2006 8:28


Digraphs. Press Ctrl-K, ., then M for a mid-dot. As I noticed, M-. yields an M with a dot above it.


vim at bertram dash scharpf dot de , July 3, 2006 18:52