Tip #370: always cd to the current file's directory
tip karma |
Rating 135/59, Viewed by 4624
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
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
<< 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"
|
Dubhead,
June 12, 2003 2:01
|
I'm happy to find that Vim version 6.2 made this tip obsolete: Just use the new 'autochdir' option.
|
Wouter Bolsterlee,
August 16, 2005 1:14
|
I use this in my ~/.vimrc file:
if exists('+autochdir')
set autochdir
else
autocmd BufEnter * silent! lcd %:p:h:gs/ /\\ /
endif
It will:
- use autochdir if available
- use an autocmd if autochdir is not available
- take care of non-existant directories
- take care of spaces in the path
Works wonderfully.
|
[email protected],
April 26, 2006 10:19
|
For some reason none of these options work when a file is opened from the command line
vim a/path/to/some/file.txt
:e <TAB> to list files shows we are still in the cwd where vim was launched and not a/path/to/some
:bp|:bn fixes it though.
Anyone know why?
|
|