Wikia

Vim Tips Wiki

Changes: See the tabs in your file

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 34: Line 34:
 
==Comments==
 
==Comments==
 
In order to tweak the colors
 
In order to tweak the colors
highlight SpecialKey guifg=<color> ctermfg=<color>
+
highlight SpecialKey guifg=<color> ctermfg=<color>
   
 
----
 
----
Line 46: Line 46:
 
if !exists("g:SeeTabEnabled")
 
if !exists("g:SeeTabEnabled")
 
let g:SeeTabEnabled = 1
 
let g:SeeTabEnabled = 1
let g:SeeTab_list = &amp;list
+
let g:SeeTab_list = &list
let g:SeeTab_listchars = &amp;listchars
+
let g:SeeTab_listchars = &listchars
 
let regA = @a
 
let regA = @a
 
redir @a
 
redir @a
Line 58: Line 58:
 
set listchars=tab:\|\
 
set listchars=tab:\|\
 
else
 
else
let &amp;list = g:SeeTab_list
+
let &list = g:SeeTab_list
let &amp;listchars = &amp;listchars
+
let &listchars = &listchars
 
silent! exe "hi ".substitute(g:SeeTabSpecialKey,'xxx','','e')
 
silent! exe "hi ".substitute(g:SeeTabSpecialKey,'xxx','','e')
 
unlet g:SeeTabEnabled g:SeeTab_list g:SeeTab_listchars
 
unlet g:SeeTabEnabled g:SeeTab_list g:SeeTab_listchars
Line 68: Line 68:
   
 
----
 
----
Use syntax highlighting when expand tabs are on. Note the use of &amp;sw to get the current shiftwidth for the expanded tabs.
+
Use syntax highlighting when expand tabs are on. Note the use of &sw to get the current shiftwidth for the expanded tabs.
   
 
<pre>
 
<pre>
Line 77: Line 77:
 
if g:SeeTabEnabled==0
 
if g:SeeTabEnabled==0
 
syn match leadspace /^\s\+/ contains=syntab
 
syn match leadspace /^\s\+/ contains=syntab
exe "syn match syntab /\\s\\{" . &amp;sw . "}/hs=s,he=s+1 contained"
+
exe "syn match syntab /\\s\\{" . &sw . "}/hs=s,he=s+1 contained"
 
hi syntab guibg=Grey
 
hi syntab guibg=Grey
 
let g:SeeTabEnabled=1
 
let g:SeeTabEnabled=1
Line 90: Line 90:
   
 
----
 
----
One correction: expand tabs setting expands tabs according to the tabstop setting not shiftwidth - so in the SeeTab() function, replace &amp;sw by &amp;ts
+
One correction: expand tabs setting expands tabs according to the tabstop setting not shiftwidth - so in the SeeTab() function, replace &sw by &ts
   
 
----
 
----

Latest revision as of 08:44, September 29, 2008

Tip 460 Printable Monobook Previous Next

created April 21, 2003 · complexity basic · author Robert (MetaCosm) · version 5.7


When browsing the jEdit screenshots page, I saw a feature I wanted and didn't have in Vim. It was a vertical line showing the Tab characters.

if (this) {
| if (that) {
| | do stuff;
| }
}

I was wondering how to do this in Vim. Turns out it is simple.

:set list
:set listchars=tab:\|\<Space>

ReferencesEdit

CommentsEdit

In order to tweak the colors

highlight SpecialKey guifg=<color> ctermfg=<color>

Here's a function+command to facilitate use:

Usage: :SeeTab (toggles between normal and bar'd tabbing styles)

" SeeTab: toggles between showing tabs and using standard listchars
fu! SeeTab()
  if !exists("g:SeeTabEnabled")
    let g:SeeTabEnabled = 1
    let g:SeeTab_list = &list
    let g:SeeTab_listchars = &listchars
    let regA = @a
    redir @a
    hi SpecialKey
    redir END
    let g:SeeTabSpecialKey = @a
    let @a = regA
    silent! hi SpecialKey guifg=black guibg=magenta ctermfg=black ctermbg=magenta
    set list
    set listchars=tab:\|\
  else
    let &list = g:SeeTab_list
    let &listchars = &listchars
    silent! exe "hi ".substitute(g:SeeTabSpecialKey,'xxx','','e')
    unlet g:SeeTabEnabled g:SeeTab_list g:SeeTab_listchars
  endif
endfunc
com! -nargs=0 SeeTab :call SeeTab()

Use syntax highlighting when expand tabs are on. Note the use of &sw to get the current shiftwidth for the expanded tabs.

fu! SeeTab()
  if !exists("g:SeeTabEnabled")
    let g:SeeTabEnabled = 0
  end
  if g:SeeTabEnabled==0
    syn match leadspace /^\s\+/ contains=syntab
    exe "syn match syntab /\\s\\{" . &sw . "}/hs=s,he=s+1 contained"
    hi syntab guibg=Grey
    let g:SeeTabEnabled=1
  else
    syn clear leadspace
    syn clear syntab
    let g:SeeTabEnabled=0
  end
endfunc
com! -nargs=0 SeeTab :call SeeTab()

One correction: expand tabs setting expands tabs according to the tabstop setting not shiftwidth - so in the SeeTab() function, replace &sw by &ts


There are now two scripts for this:


Around Wikia's network

Random Wiki