tags:

views:

623

answers:

11

In vim, when I have a buffer open, I often need to load another file in the same directory that file is in, but since I don't usually cd into it, the pwd is a parent folder, so I have to retype the path every time. Is there a shortcut for this? or a way to change the pwd to the directory the file is in?

example:

cd /src
vi lib/foo/file.js

lib/foo has two files: file.js and file2.js

in vi:

:e file2.js  # doesn't work
+6  A: 

Add this line to your vimrc configuration

autocmd BufEnter * cd %:p:h

% current file name
:p expand to full path
:h head (last path component removed)

See :help expand for further information.

rcs
That works great, thanks. Now to learn what this `%:p:h` means :)
cloudhead
This site helps me a lot when looking up vim-stuff: http://rayninfo.co.uk/vimtips.html
flokra
thanks for the extra info.
cloudhead
Just be aware that this functionality may break plugins, as noted in ":help 'autochdir'".
jamessan
+1  A: 

try :e %:p:h/file2.js

HTH, flokra

flokra
A: 

Another option is to do:

:e <Tab>

This cycles through folders in your working directory.

Eric
This won't work, because my working directory isn't the one the file I'm editing is in.
cloudhead
+1  A: 

You probably know that once you're in vim, you can use

:cd lib/foo

to change into lib/foo.

On the other hand, to change into the directory of the file you're current editing, try

:cd %:p:h

You can always use

:pwd

to check your current working directory.

And of course, if you forgot what file you're editing, just hit ctrl-g.

Gene Goykhman
+1  A: 

On the "shortcut" front, have you looked into the :Sex command? This opens up the file browser in a split so that you can open other files easily. :Sex default to the directory of the current buffer. Pretty nifty feature, if I say so myself.
I bind it to ;o for easy access:

map ;o :Sex <CR>

You can also use :Ex if you want to keep it in the current buffer instead of creating a split.

Jack M.
awesome. I wish the file would open in the other pane though!
cloudhead
Oh, that's `:Ex`. I'm in love with splits, so I think that way. I'll edit the answer for it.
Jack M.
hehe no I know, what I meant is to keep the split, but when you open a file, it opens it in the other pane, not the file listing.
cloudhead
+1  A: 

I put this in my .vimrc file, which will change the current directory to the file in the buffer. This sets the current directory for the buffer, and updates when you switch buffers.

autocmd BufEnter * lcd %:p:h
Steven Mastandrea
rcs beat you to it!
cloudhead
+2  A: 

So vim has a :find command which will use the variable 'path'. If you have consistent directory structures for your code such as all source in under a directory "src" then you can just add that to the 'path' variable. Then when you do :find foo.js it will search both your current location then the folders listed in your path variable. It basically emulates the way bash or some other shell uses the PATH variable to find the appropriate binary to run.

Neg_EV
+3  A: 

I have the following three lines on my .vimrc:

map ,e :e <C-R>=expand("%:p:h") . "/" <CR>
map ,t :tabe <C-R>=expand("%:p:h") . "/" <CR>
map ,s :split <C-R>=expand("%:p:h") . "/" <CR>

Now ,e <some-file> opens on this buffer. ,t and ,s do the same but on a new tab/split window.

hgimenez
This is my favourite of the solutions posted here. I don't like the idea of changing to the directory of the current file automatically. I prefer to open vim in the root directory of a project, and that means that `:e` always drills down from the top. Up until now, if I wanted to open a file in the same directory as the current file, I would use `:E` which brings up the netrw file explorer. Using your `,e` mapping feels much quicker. It's great how you can tab through the files in the current directory.
nelstrom
Agree with you. Also, if I never have to change directory, there's always `:cd %:h`
hgimenez
+9  A: 

A simple idea is to use the autochdir setting. Try this in your .vimrc:

set autochdir

This will change the working directory of vim to the directory of the file you opened.

Note: I'm not sure what version added this feature. I updated from vim 6 to 7.2 to get this to work.

Cory Engebretson
for some reason this doesn't work for me, on `7.2.108`
cloudhead
It's only available if Vim was built with netbeans integration or Sun visual workshop integration. The former is more common than the latter, but it's not unusual to have neither builtin. This is why gregf's answer tests for the existence of a working 'autochdir' option.
jamessan
autochdir is also a pain because it switches directories before trying to load the file, so if on the command line you do `gvim path/to/file.c` it'll cd into path/to and then try to open path/to/file.c, giving you an empty buffer in a non-existent location. :( (64-bit 7.2 on Windows, anyway.)
dash-tom-bang
+9  A: 

Newer versions of vim have a autochdir command built in, if not you can fall back to a BufEnter like setup.

" set vim to chdir for each file
if exists('+autochdir')
    set autochdir
else
    autocmd BufEnter * silent! lcd %:p:h:gs/ /\\ /
endif
gregf
+2  A: 

The solution with autochdir has its own pitfalls. For example you have to stay at the dir you are because Tags are defined with relative path or you need Makefile or pom.xml in the current dir.

You can use

:e <C-R>%

and then modify path by deleting name of the current file and enter new one. Or. Use the map

nnoremap <leader>e :edit <C-R>=fnamemodify(@%, ':p:h')<CR>/

And then by pressing \e or ,e (depends on leader settings) you will receive command that open directory in which your current file is and you can complete this command manually and can use ctrlD to show all files from the current file directory.

Or. You can try 0scan plugin.

Keys 0f

Mykola Golubyev
Tags can be found "recursively". We have our tags in a _tags/ folder at the source root. `set tags=_tags/tags;c:/work` means "go up until you find something matching `_tags/tags`, but stop if you hit `c:/work`. So if I'm editing c:/work/project/main/src/lib/subdir/file.c (from that file's directory) it can still find my tagfile c:/work/project/main/_tags/tags
dash-tom-bang