Skip to content

Instantly share code, notes, and snippets.

@kien
Created June 2, 2012 15:09
Show Gist options
  • Save kien/2858768 to your computer and use it in GitHub Desktop.
Save kien/2858768 to your computer and use it in GitHub Desktop.
mru working directories
fu! s:savecwd()
if !exists('g:CWDS')
let g:CWDS = []
en
let cwd = getcwd()
let idx = index(g:CWDS, cwd)
if idx >= 0
cal remove(g:CWDS, idx)
en
cal insert(g:CWDS, cwd)
endf
fu! s:chdir(cmd)
if !exists('g:CWDS')
retu feedkeys(":".a:cmd." ", 'n')
en
if len(g:CWDS) > 50
cal remove(g:CWDS, 50, -1)
en
ec 'Recent working directories'
ec '=========================='
let [idx, cidx] = [0, index(g:CWDS, getcwd())]
for each in g:CWDS
let marker = cidx == idx ? '>' : ' '
let idx += 1
let spad = repeat(' ', strlen(len(g:CWDS)) - strlen(idx))
ec marker.' '.spad.idx.'. '.each
endfo
ec '=========================='
echoh MoreMsg
cal inputsave()
let id = input('Enter an ID to use "'.a:cmd.'": ')
cal inputrestore()
echoh None
if id == '' || id =~ '\D'
retu
en
let path = get(g:CWDS, id - 1, '')
if path == ''
retu
en
if exists('*fnameescape')
let path = fnameescape(path)
el
let path = escape(path, " %#*?|<\"\n")
en
cal feedkeys(":".a:cmd." ".path."\<CR>", 'n')
endf
set viminfo+=!
au CursorMoved,CursorHold * cal s:savecwd()
com! -bang CD cal s:chdir('cd!')
com! -bang LCD cal s:chdir('lcd!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment