sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #2: easy edit of files in the same directory

 tip karma   Rating 477/165, Viewed by 11669 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   February 24, 2001 14:57      complexity:   intermediate
author:   scott at kintana.com      as of Vim:   5.7

It was often frustrating when I would open a file deep in the code tree and then realize I wanted to open another file in that same directory. Douglas Potts taught me a nice way to do this. Add the following snipit to your vimrc:

"   Edit another file in the same directory as the current file
"   uses expression to extract path from current file's path
"  (thanks Douglas Potts)
if has("unix")
    map ,e :e <C-R>=expand("%:p:h") . "/" <CR>
else
    map ,e :e <C-R>=expand("%:p:h") . "\" <CR>
endif

Then when you type ,e in normal mode you can use tab to complete to the file. You can also expand this to allow for spitting, etc. Very very nice.

 rate this tip  Life Changing Helpful Unfulfilling 

<< the super star | use vim to quickly compile java files >>

Additional Notes

[email protected], May 30, 2001 2:08
For not-unix-systems you need two backslashes, so use:
map ,e :e <C-R>=expand("%:p:h") . "\\" <CR>

Sometimes it may also be useful to change to the file's directory.
map ,cd :cd %:p:h<CR>
[email protected], July 6, 2001 3:26
map ,e :e <C-R>=expand("%:p:h") . "\\" <CR>
Will leave a space after printing the directory name. Use this, if you don't want the leave space after the directory name.
map ,e :e <C-R>=expand("%:p:h") . "\\"<CR>
Remove the space after "\\".
Jim Hill <[email protected]>, March 20, 2002 9:22
Do `:help path` for the vim-6 builtin way to do this.
Anonymous, August 28, 2003 18:44
Jim Hill, could you point out how to use the Vim6 built-in function to do that?
Run the command
:help path
under Vim6.2 I got this:
This is a list of directories which will be searched when using the
        |gf|, [f, ]f, ^Wf, |:find| and other commands, provided that the file
        being searched for has a relative path (not starting with '/').  The
        directories in the 'path' option may be relative or absolute.
I don't know how to fulfill the goal of this tip.
[email protected], December 8, 2003 23:22
You can use vim 6 built-in command:
:cd %:h

It's really excellent.

--Zhipeng Hu

[email protected], January 15, 2004 4:21
To expand current file directory in the command line you may also do something like

cmap %/ <C-R>=expand("%:p:h")."/"<cr>

in your .vimrc and use this mapping with :e, :w and any other command.
[email protected], December 7, 2005 8:15
It is simple enough to do :set autochdir, really
webget _at_ 163.com, January 16, 2006 0:20
Sometimes, you want to keep the current directory unchange (especially when you
need a [make] that read src/abc.java and output to bin/abc.class) auto change directory may
not always fit.

just tested, this one works:
cmap %/ <C-R>=expand("%:p:h")."/"<cr>
Note: you need to press :e %/
it auto expand correctly, in Windows 2000 (using Vim 6.4, it does not
matter / or \, use / is safe in Windows)

also ,e works, you don't need double \\
map ,e :e <C-R>=expand("%:p:h")."/"<CR>

after pressing ,e you may have something interesting in Windows:
:e C:\eclipse\workspace\zhanguo\src\lib/
Then i type: "View", plus a TAB (autocompletion) it becomes:
:e C:\eclipse\workspace\zhanguo\src\lib\View.java
Nice work on Vim 6.x it automagically change / to \ =)

Found that it's ok to have both \ and / in the path.
not sure whether it works in old version of Vim, probably not in old days.

[email protected], March 5, 2006 16:12
Also check to make sure you don't have any trailing whitespace after the <CR>.
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 the maillist. Help Bram help Uganda.
   
SourceForge.net Logo