anon
anon

Reputation: 42577

Vim :e starting directory?

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!

Upvotes: 15

Views: 7419

Answers (9)

Martin Lyne
Martin Lyne

Reputation: 3065

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

Upvotes: 0

Gaelan
Gaelan

Reputation: 1149

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

Upvotes: 0

zigzag
zigzag

Reputation: 1

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

:help :E

Upvotes: 0

pthulin
pthulin

Reputation: 4151

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

Upvotes: 1

michael
michael

Reputation: 11847

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

Upvotes: 14

Alex Budovski
Alex Budovski

Reputation: 18436

  • :cd changes directory
  • :pwd prints the current one.

Upvotes: 0

Dave Ray
Dave Ray

Reputation: 40005

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

Upvotes: 11

Benjamin Bannier
Benjamin Bannier

Reputation: 58547

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

Upvotes: 3

caskey
caskey

Reputation: 12695

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.

Upvotes: 2

Related Questions