15

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!

9 Answers 9

14

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly the answer I was looking for.
11

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

Comments

3

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") 

1 Comment

Great! But autocommands like that one always display an error E32 when you close all files with :bd on each one.
2

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.

Comments

1

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 

1 Comment

I do something similar, but exclude help files: au BufReadPost * if &ft != 'help' | silent! cd %:p:h | endif
0
  • :cd changes directory
  • :pwd prints the current one.

Comments

0

why not just :E? Explore directory of current file.

:help :E

Comments

0

This isn't exactly what you wanted, but check out NERDTree.

1 Comment

This is really a comment, not an answer. With a bit more rep, you will be able to post comments..
0

On vim/gVim I just have cd C:/blah/blah at the top of my vimrc. I imagine it works on all platforms.

I personally use vagrant for each project so one CD is enough, but I think you can get vim to use different config files too, -u flag I think.

Or map a key to each project you have so pressing Ctrl+F1 does cd path/to/project/1 and Ctrl+F2 does cd path/to/project/2 perhaps?

Note: I don't use any plugins

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.