Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
Created November 17, 2017 17:36
Show Gist options
  • Save PeterRincker/c6528495acf2b0ae32810d26ae2b02bf to your computer and use it in GitHub Desktop.
Save PeterRincker/c6528495acf2b0ae32810d26ae2b02bf to your computer and use it in GitHub Desktop.
Quickly expand current working directory into cmdline
" Use // in certain commands like :edit, :split, :read, etc. to insert the current buffer's directory
" :e// -> :e foo/bar/
"
" Compatible commands:
" :edit/:split/:vsplit
" :find/:sfind
" :buffer/:sbuffer
" :read
" :diffpslit/:diffpatch
" :vimgrep/:grep
cnoremap <expr> / <SID>current_dir("/", "\<bs>")
function! s:current_dir(char, start)
let cmdline = getcmdline()
let type = getcmdtype()
if type != ':'
return "\<c-]>" . a:char
endif
if cmdline =~# '\v<(e%[dit]!?|sp%[lit]!?|vs%[plit]!?|fin%[d]!?|sf%[ind]|b%[uffer]!?|sb%[uffer]!?|r%[ead]|diffs%[plit]|diffp%[atch]|vimg%[rep](\s*/.{-}/|\s\S.{-})\s|grep.{-}\s)\s*/$'
return a:start . (cmdline =~ '\s/$' ? '' : "\<space>") . expand('%:h:gs?\\?\/?') . '/'
endif
return "\<c-]>" . a:char
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment