3

I use gvim to edit my tex files and it is the default editor. So I just double click on the tex files to edit them and that opens them with Gvim. However, the working directory for Gvim is always the home directory which is odd since I would expect the working directory to be the one in which the file that I have just double-clicked and opened is present. This causes a lot of inconvenience since I normally want to run commands in that directory in Gvim and I have to manually change it every time. Any suggestions on how I can have Gvim starting in the proper way? Thanks a lot in adcvance.

I have already checked the .gvimrc file and I see nothing suspicious. It just has my font and color settings, that's all.

ste_kwr
  • 11,210
  • 1
    You can use set autochdir to always change the directory to the directory of the currently edited file. But this might break some plugins. – Marco Apr 12 '13 at 20:50
  • This seems to work, can you elaborate on the plugin part (It is important to me because I use the Vim latex-suite). Also, any idea why this is not the default behavior? Thanks. – ste_kwr Apr 12 '13 at 21:11
  • 1
    It might work for you, but some plugins might rely on autochdir to be switched off, e.g. minitabexpl (if it's not fixed by now). It's not the default since it breaks vi compatibility and it's not always desired. I personally don't use it because when I type :make I want to run make in the main directory. When autochdir is used, vim changes the directory and vim cannot launch make any longer and the build fails. – Marco Apr 12 '13 at 21:25
  • My problem arises when I want to edit more files. For example when I want to edit the bib file in another buffer, :edit <bibfile> will not work since I am in the home directory and I'll need to change the directory. Usually I maintain many versions of my tex files as the article progresses and often need to copy/paste chunks of text from the older versions etc... all in the same folder. Anyway, thanks again. – ste_kwr Apr 12 '13 at 21:30
  • 2
    Use :Ex to open a file explorer in the directory of the current file. Or create a mapping to be able to switch directory without much effort. This is what I use: nnoremap <silent> <leader>p :cd %:p:h<cr> – Marco Apr 12 '13 at 21:35

1 Answers1

5

Sorry for resurrecting such an old question, but I found it when I had the same problem, so maybe the solution I finally found can help someone who stumbles upon this like me.

Just add

cd %:h

to your .vimrc. That way, (g)vim changes to the directory of the current file at start, which means, unlike with autochdir, no plugins get broken. Also, you can still edit a file in a subdirectory and still run commands in the working directory.

That means, when you have you bibfile in a subdirectory, you could edit it and run bibtex while you have that file still open. With autochdir you would have to change to a file in the working directory first.

Hope that helps someone! :)

proedie
  • 71