27

I'm a mac user giving vim a serious try. Most of the GUI editors I'm used to allow me to open a directory as a "project" by executing a command like:

edit ~/www/example.com/

The vim equivalent vim ~/www/example.com/ will show me a list of files in the directory, and I can open them. But it does not set vim's working directory to that path, I have to run :cd . to set the working directory.

Is there some way, perhaps with a shell script, to open vim and have it's working directory set to a given path?

I'm actually using MacVim, if that makes any difference.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
  • 4
    In directory view just hitting `c` will cd you into that directory - doesn't do exactly what you want, but its worth knowing. – Michael Anderson May 08 '11 at 23:27
  • 2
    It's ok to propose answers to your own question. I recommend moving your work in progress answer out of the question, since it is not part of the question. There is even a badge you can earn for answering your own question with a score of 3 or higher. (ps nice answer!) – Ziggy May 09 '11 at 04:49
  • @Ziggy thanks. I did try answering my own question, but it did not allow it (something about waiting 24 hours before answering your own question?). Wasn't sure what to do, but have now posted it as an answer. – Abhi Beckert May 10 '11 at 04:55

5 Answers5

11
$ cd ~/my/working/directory
$ vim .
kzh
  • 19,810
  • 13
  • 73
  • 97
8

Thanks to @sehe's suggestions, I came up with this. Not sure if it's the best solution, but it seems to work.

#!/bin/bash

if [ "$#" -eq 1 ];then # is there a path argument?
  if test -d $1;then # open directory in vim
    vim $1 +':cd %'
  else # open file in vim
    vim $1 +':cd %:h'
  fi
else # no path argument, just open vim
  vim 
fi
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
7

Braindead:

 (cd /path/to/dir && vim file)

Less so:

 vim /path/to/dir/file +':cd %:h'

You can always map :cd %:h to a convenient key or put in an autocommand (I wouldn't actually recommend the latter, but there is no arguing about taste)


Oh and for directories instead of files:

:cd %

is quite enough

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    Thanks, that pointed me in the right direction! But isn't the perfect answer, because the "file" you refer to does not exist, I want to open vim with a given working directory, not open a file in vim (the file I want to open is likely to be several sub directories deep) – Abhi Beckert May 08 '11 at 23:01
2

Would this help?

set autochdir

I found it http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file

Ziggy
  • 21,845
  • 28
  • 75
  • 104
  • I think autochdir would break my workflow, I work with projects containing several thousand files across hundreds of directories and need the working directory to be the "root" of the project. – Abhi Beckert May 08 '11 at 23:03
1

Try adding the following to your .vimrc

let g:netrw_liststyle=3
let g:netrw_keepdir=0

This will make the directory browsing use a tree style for showing the files (you can expand a directory by putting the cursor on a directory and hitting enter) and make the current working directory also be the one you are browsing.

You might also be interested in the NERDTree plugin that provides a directory browser that is more advanced than the built in one. It has an option

let g:NERDTreeChDirMode=2

to make the current directory match the root of the displayed tree or

let g:NERDTreeChDirMode=1

to change the directory whenever you use a command (:e or :NERDTree) to browse a new directory.

Geoff Reedy
  • 34,891
  • 3
  • 56
  • 79