Configuration files are notorious for their verbosity. Open any .conf, .ini, or YAML file and you’ll likely find dozens of comment lines explaining options, followed by the actual key-value pairs you care about. When you’re trying to get a quick overview of what’s actually configured, all those helpful comments become noise.
I wanted something like rg -v '^$|^#' but inside Vim, with the ability to toggle back to the full view when needed. The solution uses Vim’s folding system in a somewhat unconventional way.
The Problem
Consider a typical config file. It might be 200 lines long, but only 30 of those lines are actual settings. The rest are comments documenting each option and blank lines for readability. When reviewing or auditing a configuration, you often want to see just the active settings without the documentation getting in the way.
The Solution
Instead of filtering content into a separate buffer (which would break editing), we can use Vim’s expression-based folding to hide comments and blank lines in place. The key insight is that folds don’t have to be traditional code blocks. They can be individual scattered lines that match a pattern.
Here’s the complete solution to add to your .vimrc:
" Activate with this key sequence: \cv
" Toggle filtered view of config files (hide comments and blank lines)
function! ConfigFoldExpr()
let line = getline(v:lnum)
" Fold away blank lines and comment lines
if line =~ '^\s*$' || line =~ '^\s*#'
return 1
else
return 0
endif
endfunction
function! BlankFoldText()
return ''
endfunction
function! ToggleConfigView()
if &foldmethod ==# 'expr' && &foldexpr ==# 'ConfigFoldExpr()'
" Turn off: restore normal view
setlocal foldmethod=manual
setlocal foldexpr=
setlocal foldtext=foldtext()
" Note: line below must end with a space after the backslash (escaped space)
setlocal fillchars-=fold:\
normal! zE
echo "Config view: OFF"
else
" Turn on: fold comments and blanks
setlocal foldmethod=expr
setlocal foldexpr=ConfigFoldExpr()
setlocal foldlevel=0
setlocal foldminlines=0
setlocal foldtext=BlankFoldText()
" Note: line below must end with a space after the backslash (escaped space)
setlocal fillchars+=fold:\
echo "Config view: ON (only key/values)"
endif
endfunction
nnoremap <leader>cv :call ToggleConfigView()<CR>
How It Works
The implementation consists of three functions working together.
ConfigFoldExpr() is called by Vim for every line in the buffer. It returns 1 for lines that should be folded (comments starting with # and blank lines) and 0 for lines that should remain visible. The pattern ^\s*$ matches empty lines or lines containing only whitespace, while ^\s*# matches comment lines that may have leading whitespace.
BlankFoldText() returns an empty string. Normally when Vim folds lines, it displays something like +-- 15 lines folded --. By returning nothing, the folded sections become visually invisible rather than replaced with placeholder text.
ToggleConfigView() handles the state management. It checks whether the config view is currently active by examining the foldmethod and foldexpr settings. If active, it restores normal viewing. If inactive, it enables the filtered view.
The fillchars Detail
One subtle issue required attention. Even with an empty foldtext, Vim fills the folded line with a default character (dashes). The setting fillchars+=fold:\ changes this fill character to a space. Note that the backslash escapes a literal space character, so the line must end with a space after the backslash. This is easy to accidentally delete when editing your .vimrc, hence the warning comment in the code.
Usage
Press \cv (backslash followed by c and v) to toggle the view. The cv mnemonic stands for “config view”. If you’ve remapped your leader key to something else like space or comma, use that instead of backslash.
When enabled, you’ll see only the key-value lines with blank space where comments and empty lines were. Line numbers are preserved, so you still know exactly where each setting lives in the file. When disabled, the full file returns to view.
The mapping uses <leader> so it adapts to whatever leader key you’ve configured. If you prefer a different binding, common alternatives include function keys like <F6> or key sequences like cov (mnemonic: change option view):
nnoremap cov :call ToggleConfigView()<CR> " mnemonic: change option view
Limitations and Extensions
This implementation assumes comments begin with #. Many config formats use this convention, including shell scripts, YAML, TOML, and various application configs. For formats using different comment characters like ; (INI files) or // (JSON with comments), you would modify the pattern in ConfigFoldExpr().
For example, to also support semicolon comments:
if line =~ '^\s*$' || line =~ '^\s*[#;]'
The folded lines are still present and editable. If you navigate to a folded region and start typing, Vim will open the fold. This preserves the ability to edit the file normally while the filter is active.
Conclusion
Vim’s folding system is flexible enough to serve purposes beyond its traditional use of collapsing code blocks. By treating scattered comment lines as fold targets and making the fold display invisible, we get a clean filtered view that works in place without losing context or editing capability. The toggle makes it practical for everyday use when reviewing configuration files.