vim - Easy way to change to parent dir of a buffer? -


if buffer list looks this:

 :ls   1 %a   "application/powermodeserver/test/testtimer.cpp" line 1   2      "application/powermodeserver/test/fakeclient.cpp" line 0   3      "application/powermodeserver/test/fakevp.cpp" line 0   7      "application/powermodeserver/private/imsgsender.h" line 0   9      "application/powermodeserver/private/udpsocket.h" line 0  17      "application/powermodeserver/src/powerfsm.cpp" line 0  18      "application/powermodeserver/src/lua/src/lfunc.h" line 0  19      "application/powermodeserver/src/lua/src/lmem.h" line 0  20      "application/powermodeserver/src/lua/src/ltable.h" line 0  41      "application/powermodeserver/src/powermodeserver.cpp" line 0  42      "application/powermodeserver/src/udpsocket.cpp" line 0  43      "application/powermodeserver/src/timer.cpp" line 0 

what's easiest way change vim's current working directory parent directory of, say, buffer 19? i'm used finger-mumbling way there using:

:cd a<tab>p<tab>s<tab>l<tab>s<tab>
requires quite few keystrokes---especially if completions ambiguous. i'd more concise, like:
:cd ~19
recommendations?

edit: added mods suggested zyx here since don't have sufficient points edit jeet's answer directly:

function! cdbufworkingdir(target)     if empty(a:target)         let targetdir = expand("%:p:h")     else         if a:target =~# '^\~\d'             let targetdir = fnamemodify(bufname(str2nr(a:target[1:])), ":p:h")         else             let targetdir = a:target         endif     endif     execute "cd ".fnameescape(targetdir)     echo targetdir endfunction command! -nargs=? cdbuf :call cdbufworkingdir(<q-args>) 

i try in "~/.vimrc" (updated in accordance op's specs):

function! cdbufworkingdir(...)     if a:0 == 0         let l:targetdir = expand("%:p:h")     else         if a:1[0] == "~"             let l:targetdir = fnamemodify( bufname(str2nr(a:1[1:])), ":p:h" )         else             let l:targetdir = a:1         endif     endif     execute "cd ". l:targetdir     echo l:targetdir endfunction command! -nargs=* cdbuf :call cdbufworkingdir(<q-args>) 

then issuing command buffer number preceded "~" argument (e.g., :cdbuf ~3) switch working directory buffer's working directory. if argument not preceded "~", treated directory path directly change. while command without argument switch working directory current buffer's working directory. robustness, should add idiot-checking codes (e.g., happens when multiple arguments given?), or handle special cases (e.g., happens if string or buffer name passed, or file symlink).


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -