Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim :e starting directory?

Tags:

vim

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!

like image 532
anon Avatar asked Jan 31 '10 02:01

anon


People also ask

What is E in Vim?

E - jump forwards to the end of a word (words can contain punctuation) b - jump backwards to the start of a word. B - jump backwards to the start of a word (words can contain punctuation) 0 - jump to the start of the line. ^ - jump to the first non-blank character of the line.

How do I change the current directory in Vim?

This can be an absolute or relative path, so :cd .. will move up one level. Or you can use :cd %:h which will also change to the directory the current file is in, but without setting autochdir. will change directory to your home directory (or on windows, print the current directory).

How do I navigate to a folder in Vim?

Select a file or directory name and press Enter to open that file or directory. (For example :e /home/user displays the contents of that directory.) To return to the explorer window, press Ctrl-^ (usually Ctrl-6). You can also "edit" a directory to explore that directory.

How do I go back one directory in Vim?

You can go back to the last buffer using :b# . If you just opened a file, then it will bring you just back to the directory browsing.


2 Answers

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

like image 193
michael Avatar answered Nov 03 '22 01:11

michael


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

like image 28
Dave Ray Avatar answered Nov 02 '22 23:11

Dave Ray