Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3497662
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:23:52+00:00 2026-05-18T12:23:52+00:00

If my buffer list looks like this: :ls 1 %a Application/PowerModeServer/test/testTimer.cpp line 1 2

  • 0

If my buffer list looks like 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 the easiest way to change vim’s current working directory to the parent directory of, say, buffer 19? I’m used to finger-mumbling my way there using:

:cd A<TAB>P<TAB>s<TAB>l<TAB>s<TAB>

but this requires quite a few keystrokes—especially if the completions are ambiguous. I’d like something more concise, like:

:cd ~19

Any recommendations?

EDIT: Added mods suggested by ZyX here since I don’t have sufficient points to 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>)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:23:53+00:00Added an answer on May 18, 2026 at 12:23 pm

    I would try something like this in your “~/.vimrc” (UPDATED in accordance to 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 the command with a buffer number preceded by a “~” as an argument (e.g., :Cdbuf ~3) will switch the working directory to that buffer’s working directory. If the argument is not preceded with “~”, it will be treated as a directory path directly to which to change. While the command without an argument will switch the working directory the current buffer’s working directory. For robustness, you should add some idiot-checking codes (e.g., what happens when multiple arguments are given?), or handle special cases (e.g., what happens if a string or buffer name is passed, or the file is a symlink).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.