Vim Tips Wiki

Edit configuration files for a filetype

1,613pages on
this wiki
Revision as of 05:23, September 29, 2008 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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."
  " 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
  " restore the register
  let @u = safereg


Around Wikia's network

Random Wiki