Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default (permanent) working directory in Vim [duplicate]

Tags:

vim

macvim

Possible Duplicate:
set .vim home directory to something specific

There is already a question about setting a working directory: How to set working/current directory in Vim? However my problem is a little different, I have several thousands files (several different projects) in a folder. Whenever I start Vim (MacVim), the cwd is by default set to home dir. Thus when I'm using plugins like ack.vim they by default are searching in my home dir which is useless. I just want to set it to my /home/user/Code folder (which has all my snippets, projects,etc..)

Is there any settings which sets the cwd to a certain directory? Or any workarounds (like executing :cd /home/user/Code for every startup in Vim?

like image 739
Fatih Arslan Avatar asked Dec 12 '22 18:12

Fatih Arslan


2 Answers

You could just hard-code the current working directory via

:cd /home/user/Code

in your ~/.vimrc. But that leads to a strange setup whenever you edit files elsewhere. (And even if you're mainly editing inside your Code directory, there is the occasional edit of ~/.bashrc, or a config setting in /etc/..., and so on.

What I do is put the :cd into a local vimrc inside the Code directory. (I use the localrc.vim plugin; there are others.) Then, the directory is changed only when I actually edit any file withing that directory tree.

like image 76
Ingo Karkat Avatar answered May 07 '23 05:05

Ingo Karkat


You can

:se autochdir

This will automatically change to the directory containing the opened file.

Or

:cd %:h

to change to the directory containing the currently opened file now

like image 39
sehe Avatar answered May 07 '23 06:05

sehe