Vim logo vim online Vim Book Ad

basic Tip #370: always cd to the current file's directory

 tip karma   Rating 74/32, Viewed by 950 

created:   November 15, 2002 0:59      complexity:   basic
author:   Dubhead      as of Vim:   6.0

When I have several or more files from various directories opened and am going to open another, I find it counter-intuitive to specify the file path from Vim's current directory.  I think it's more natural to specify it from current file's directory (though this is not the traditional vi way).  If you feel similarly, this simple tip may help you.

My solution is to have Vim always cd to the current file's directory.  Put this in your .vimrc:

function AlwaysCD()
        if bufname("") !~ "^ftp://";
                lcd %:p:h
        endif
endfunction
autocmd BufEnter * call AlwaysCD()

Note that Vim doesn't cd to a remote machine's directory.  If you never open a file through network, just use this instead:

autocmd BufEnter * lcd %:p:h

 rate this tip  Life Changing Helpful Unfulfilling 

<<Comment/UnComment visually selected text | Encrypting a file within vim session and not leaving behind traces. >>

Additional Notes

Dubhead, November 15, 2002 1:05
Oops, please remove that semicolon at the end of the 'if' line.  I'm sorry.
Anonymous, November 16, 2002 18:23
I've been using this for a long time and it's really convenient. I did notice the shortcoming on remote files but so far hadn't bothered to fix it. Thanks for the hint.

Here's a version that will work for all URI schemes, not just ftp, and doesn't require a function:

:autocmd BufEnter * if bufname("") !~ "^\[A-Za-z0-9\]*://" | lcd %:p:h | endif

Perfect. :)
Anonymous, November 17, 2002 19:11
I small problem that you'll probably never have to deal with, is if you type "vim fgfdg/fdgfdg/file.txt" and the directories don't exist, you'll get a nasty error message...add sil! before the lcd call and you won't :)
[email protected], November 18, 2002 6:20
What do I need to do in this case, when I have spaces or other symbols in path? For example under "Program Files".
Dubhead, November 18, 2002 22:51
mr_july, quote the space in the pathname with a backslash.  It would look like
c:\Program\ Files\
I guess you can automatically get it when you hit the space bar to complete the pathname, though.

If you're using Windows 2k/XP (not 98/ME), there's another workaround:  Create a 'junction'.  You can create a junction point, say "C:\ProgramFiles" (note the lack of the space), as an alias for "C:\Program Files".  Then you can refer to "C:\Program Files\SomeApp\readme.txt" as "C:\ProgramFiles\SomeApp\readme.txt".

Junction is a feature of NT filesystem, but Windows somehow lacks a UI to create it.  There are some third party utilities for that, such as  'Winlinks' by Thirty4 Interactive (go to http://www.thirty4.com/ and select PROJECTS).

And thanks for the function-less solution and the 'silent!' suggestion.  I combined them and it works :-)
Anonymous, November 27, 2002 0:07
Another useful thing for Windows user:

add a link under your user configuration in the directory 'SendTo' and edit the target like:
    U:\MyTools\vim\gvim.exe  --cmd "lcd %:p:h"

So you can start gvim with the selected file(s) from Windows explorer by selecting gvim via the context menu / send to / gvim.

In the same way you can start a vimdiff for the selected files from Windows explorer.
Just add '-d' to the target line:
    U:\MyTools\vim\gvim.exe -d --cmd "lcd %:p:h"
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo