Skip to content

Instantly share code, notes, and snippets.

@bfrg
Last active July 21, 2019 19:09
Show Gist options
  • Save bfrg/05ceb1dad516a99c4cb2504394a72143 to your computer and use it in GitHub Desktop.
Save bfrg/05ceb1dad516a99c4cb2504394a72143 to your computer and use it in GitHub Desktop.
Commands to quickly edit my dotfiles (supports wildmenu tab-completion and accepts partial matches)
" ==============================================================================
" Quickly edit my dotfiles
"
" Commands:
" Edit the dotfile 'foo' ...
" :DotE foo ... in current window
" :DotS foo ... in new split window
" :DotV foo ... in new vertical split
" :DotT foo ... in new tab page
"
" Examples:
" :DotE x<tab>
" will tab-complete to: tmux, xresources, xprofile, xsession, xinitrc
"
" :DotE xinitrc<cr>
" will edit ~/.xinitrc in the current window
"
" :DotE xin<cr>
" will also edit ~/.xinitrc
"
" :DotE vim<cr>
" will list 'gvimrc' and 'vimrc' in the wildmenu
"
" Bonus:
" When an incomplete pattern is supplied that matches several files, wildmenu
" completion is triggered automatically:
" :DotE vim<cr>
" will list 'gvimrc' and 'vimrc' in the wildmenu
" ==============================================================================
let s:my_dotfiles = {
\ 'vimrc' : '$HOME/.vim/vimrc',
\ 'gvimrc' : '$HOME/.vim/gvimrc',
\ 'bashrc' : '$HOME/.bashrc',
\ 'clang-format' : '$HOME/.clang-format',
\ 'gitconfig' : '$HOME/.gitconfig',
\ 'i3config' : '$HOME/.config/i3/config',
\ 'i3status' : '$HOME/.config/i3/i3status.conf',
\ 'profile' : '$HOME/.profile',
\ 'tmux' : '$HOME/.tmux.conf',
\ 'xresources' : '$HOME/.Xresources',
\ 'xprofile' : '$HOME/.xprofile',
\ 'xsession' : '$HOME/.xsession',
\ 'xinitrc' : '$HOME/.xinitrc'
\ }
function! s:OpenDotFile(pattern, cmd) abort
let l:matches = filter(keys(s:my_dotfiles), {idx, val -> match(val, a:pattern) != -1})
if len(l:matches) == 1
" Found a single match, open the file
execute a:cmd resolve(expand(s:my_dotfiles[l:matches[0]]))
elseif index(l:matches, a:pattern) != -1
" Special case when 'a:pattern' matches exactly a key
execute a:cmd resolve(expand(s:my_dotfiles[a:pattern]))
else
" Several matches were found. So re-run the same command and trigger
" wildmenu completion.
" We need this dictionary, otherwise we have to call a different
" function for each DotE, DotV, DotS, DotT command
let l:cmds = {
\ 'edit' : 'DotE',
\ 'vsplit' : 'DotV',
\ 'split' : 'DotS',
\ 'tabedit': 'DotT'
\ }
if &wildcharm
call feedkeys(':' . l:cmds[a:cmd] . ' ' . a:pattern . nr2char(&wildcharm), 'n')
else
" Alternatively, press <c-d> to list possible candidates
call feedkeys(':' . l:cmds[a:cmd] . ' ' . a:pattern . "\<c-d>", 'n')
endif
endif
endfunction
function! s:DotFiles(ArgLead, CmdLine, CursorPos) abort
return filter(keys(s:my_dotfiles), 'v:val =~ a:ArgLead')
endfunction
command! -complete=customlist,<sid>DotFiles -nargs=1 DotE call <sid>OpenDotFile(<q-args>, 'edit')
command! -complete=customlist,<sid>DotFiles -nargs=1 DotV call <sid>OpenDotFile(<q-args>, 'vsplit')
command! -complete=customlist,<sid>DotFiles -nargs=1 DotS call <sid>OpenDotFile(<q-args>, 'split')
command! -complete=customlist,<sid>DotFiles -nargs=1 DotT call <sid>OpenDotFile(<q-args>, 'tabedit')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment