Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Remove html character entities)
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
 
|version=6.0
 
|version=6.0
 
|rating=6/8
 
|rating=6/8
  +
|category1=VimL
  +
|category2=
 
}}
 
}}
 
When you open a file, Vim may load several scripts to customize itself for editing the file type the file is associated with (for example a file "test.c" is associated with the filetype "c").
 
When you open a file, Vim may load several scripts to customize itself for editing the file type the file is associated with (for example a file "test.c" is associated with the filetype "c").
Line 36: Line 38:
 
" we may not do this with a loaded file, since this won't trigger the
 
" we may not do this with a loaded file, since this won't trigger the
 
" configuration file loading as desired.
 
" configuration file loading as desired.
" try calling with 'call Edit_ft_conf("nonexistingfile.<EXT>")' if this
+
" try calling with 'call Edit_ft_conf("nonexistingfile.<EXT>")' if this
 
" gives you troubles
 
" gives you troubles
if bufexists(a:name) &amp;&amp; bufloaded(a:name)
+
if bufexists(a:name) && bufloaded(a:name)
 
echo "!Attention: buffer for " . a:name . " is loaded, unload first."
 
echo "!Attention: buffer for " . a:name . " is loaded, unload first."
 
return
 
return
Line 52: Line 54:
 
let pos = 0
 
let pos = 0
 
let regexp = 'sourcing "[^"]\+"'
 
let regexp = 'sourcing "[^"]\+"'
while match(@u,regexp,pos) &gt;= 0
+
while match(@u,regexp,pos) >= 0
 
let file = matchstr(@u,regexp,pos)
 
let file = matchstr(@u,regexp,pos)
 
let pos = matchend (@u,regexp,pos)
 
let pos = matchend (@u,regexp,pos)
Line 64: Line 66:
   
 
==Comments==
 
==Comments==
 
----
 
[[Category:VimL]]
 

Latest revision as of 05:23, 29 September 2008

Tip 215 Printable Monobook Previous Next

created February 14, 2002 · complexity intermediate · author Mark A. Hillebrand · version 6.0


When you open a file, Vim may load several scripts to customize itself for editing the file type the file is associated with (for example a file "test.c" is associated with the filetype "c").

Such configurations include the setting of syntax highlighting colors (:help syntax) and support for indentation (:help filetype-indent-on).

When you start to override these files for yourself, it can sometimes be confusing, which file sets a specific option.

The following function can be used, to edit the configuration files which are associated with a specific filename. It opens a buffer for all files which get loaded. If I invoke it with ':call Edit_ft_conf("test.c")', for example, I end up with the following buffers / windows:

  1. a "[No File]" line 1
  2. a "test.c" line 1
  3. a= "/usr/local/share/vim/vim60/syntax/c.vim" line 1
  4. a "~/.vim/after/syntax/c.vim" line 1
  5. #a= "/usr/local/share/vim/vim60/indent/c.vim" line 1
  6. %a= "/usr/local/share/vim/vim60/ftplugin/c.vim" line 1

Here is the function:

" Edit filetype configuration files
" Usage: ':call Edit_ft_conf("file")'
" Purpose: open all scripts which get loaded implicitly by opening "file"
" (syntax highlighting, indentation, filetype plugins, ..)
" The order of windows reflects the order of script loading (but "file" is
" the topmost window)
fun! Edit_ft_conf(name)
  " we may not do this with a loaded file, since this won't trigger the
  " configuration file loading as desired.
  " try calling with 'call Edit_ft_conf("nonexistingfile.<EXT>")' if this
  " gives you troubles
  if bufexists(a:name) && bufloaded(a:name)
    echo "!Attention: buffer for " . a:name . " is loaded, unload first."
    return
  endif
  " split-open the file with verbose set, grab the output into a register
  " (without clobbering)
  let safereg = @u
  redir @u " redirect command output to register @u
  exec "silent 2verbose split " . a:name
  " verbose level 2 suffices to catch all scripts which get opened
  redir END
  " Parse register @u, looking for smth like: 'sourcing"/usr/local/share/vim/vim60/syntax/c.vim"'
  let pos = 0
  let regexp = 'sourcing "[^"]\+"'
  while match(@u,regexp,pos) >= 0
    let file = matchstr(@u,regexp,pos)
    let pos = matchend (@u,regexp,pos)
    let file = strpart(file,10,strlen(file)-11)
    exec "silent below split " . file
  endwhile
  " restore the register
  let @u = safereg
endfun

Comments[]