tags:

views:

574

answers:

7

I code in Vim, not an IDE. My source code is often nested 2-3 directories deep.

~/foo$ find
xyz
bar/abc
bar/def

~/foo$ vim
// inside of vim
:e bar/abc
... some work ...
:e <-- is there a way I can have this :e start in ~/foo/bar instead of ~/foo ?

Basically, I want :e to start the directory in "pathname of last edited file"

Thanks!

+2  A: 

Sorry, but vim's :edit command takes a path which is interpreted relative to the present working directory of the vim instance.

You do have a :cd command which you could use to :cd bar then work for a while, then :cd ...

Hope that help some.

caskey
+2  A: 

To always change the working directory to the current file's directory I have this in my .vimrc:

if has("autocmd")
  autocmd BufEnter * :lcd %:p:h
endif " has("autocmd")
honk
+4  A: 

Try the autochdir option. It will automatically change the current working directory to whatever file was most recently opened or selected. In .vimrc:

set autochdir

For more info, :help autochdir

Dave Ray
A: 
  • :cd changes directory
  • :pwd prints the current one.
Alex
+3  A: 

There's a lot of reasons not to like autochdir as it messes up some plugins and if you end up doing :e ../../../foo.txt you are not gaining anything. Just as an idea try this cmap I knocked up

:cnoremap red edit <c-r>=expand("%:h")<cr>/

then you can type :red and get

:e /the/path/to/your/current/files/dir/

(edit: perhaps use z instead of red as there are commands that start with red)

To expand the topic, also check out the FuzzyFinder plugin and some custom mappings to rapidly jump to common files you are always editing. Eg

10 or so of your regular files should be no more than 2 keystrokes away. It helps if they are systematically named

Here's an idea I use for django.

http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings

michael
This is exactly the answer I was looking for.
anon
A: 

Some time ago I asked questions related to this on the vim mailing list: http://www.mail-archive.com/[email protected]/msg03266.html Maybe you will find useful tips in that thread.

I tested a lot of plugins, but since CLI based GUIs are not my taste, I simply ended up using standard vim with a few configuration settings.

As honk pointed out, this line sets the working directory to the same as the file your working on:

autocmd BufEnter * lcd %:p:h

My other tip is to use the wildmenu. It makes it easier to get an overview of the files in your current directory when you go :e and then TAB. I'm a python programmer so the last line shows how to hide auto generated files that the python interpreter spits out, but you could use it to hide java .class files or c .obj files or whatever.

set wildmode=list:longest
set wildignore=*.pyc,*pyo
pthulin
A: 

why not just :E

:h :E

zigzag