6

When running gvim by double clicking with a file in a graphical file manager, like nautilus, the vim instance is called with a strange current directory (seems root sometime, but I am not sure). This has the effect that file name completion (C-x C-f) is, well, not so useful.

I have a map that solve this:

nnoremap <silent> <F3> :lchdir %:p:h <CR>

so that I can hit F3 and I am done. I was trying to make it happens automatically whenever I start editing, so I tried:

autocmd FileReadPost *.tex :lchdir %:p:h <CR>

and with BufReadPost too, but it doesn't work.

What am I doing wrong?

Notice: Although set autochdir is working, it is doing this globally; in the help there is a scary notice:

Note: When this option is on some plugins may not work.

So it would be nice to be able to select for which buffers I want to do it, and not setting it globally.

  • possible duplicate of Make :edit {file} use current directory during :Explore – muru Feb 20 '15 at 11:54
  • @muru I'm not sure this is a duplicate ... The problem (having a strange cwd) should not occur at all in the first place, and netrw isn't involved here... – Martin Tournoij Feb 20 '15 at 11:57
  • @Carpetsmoker alright.. So should I post autochdir as an answer? – muru Feb 20 '15 at 11:58
  • @muru yes (good spotting!) , because I just tested it and it works. Although it would be nice to know why the autocmd did not work; what if I'd like to have it enabled only for *.tex files for example? – Rmano Feb 20 '15 at 11:59
5

I just installed nautilus, and it seems the current working directory is always set to the directory nautilus was started from (ie. Vim inherits nautilus' working directory).

This is a problem in nautilus (IMHO), and not Vim. When nautilus starts a new process (Vim) it can set the working directory, but doesn't. If I check /proc/22656/cwd it's set to /home/martin.

I also have space-fm installed, and if I open the same file in that, /proc/22803/cwd is set to /home/martin/code (where I would expect it to be).


So, now we know what the problem is, let's look at your autocmd:

autocmd FileReadPost * :lchdir %:p:h <CR>

The FileReadPost does something different from what you expect, from :help FileReadPost:

After reading a file with a ":read" command.

But that's not what we're doing; when you open a file Vim doesn't use :read. What you want to use instead, is BufReadPost, from :help BufReadPost.

When starting to edit a new buffer

In Vi & Vim, when you think "file", or usually (though not always) want to think "buffer".

So this is what you want to use. You also don't need the <CR> here; that's just for key mappings (not autocmd)

So a working solution would be:

autocmd BufReadPost * :lchdir %:p:h

But wait, there's more!

Vim has the autochdir option which does the same thing! From :help autochdir:

When on, Vim will change the current working directory whenever you open a file, switch buffers, delete a buffer or open/close a window. It will change to the directory containing the file which was opened or selected.
Note: When this option is on some plugins may not work.

The note about plugins not working also applies to the autocmd.

  • Neither BufReadPost nor BufWinEnter (as suggested by @muru) work. set autochdir do work, but only globally. (BTW, I too think it's a bug/misfeature of Nautilus. Just searching a workaround) – Rmano Feb 20 '15 at 16:31
  • @Rmano Hmm, using BufReadPost works for me (tested it, in Nautilus); perhaps there is another autocmd in that's interfering? You can try starting Vim with blank vimrc file except for this one line. You can also replace the lchdir with echoerr(expand("%:p:h")) to show an "error" with the file path (this is useful for debugging). – Martin Tournoij Feb 20 '15 at 16:36
  • Ok. Found it. If I call gvim /home/romano/path/file.tex it works, but if I call (as is doing my default action) exec gvim --servername desktop_0 --remote-tab-silent /home/romano/path/file.tex it does not work (desktop_0 is automatically generated for the current desktop). Now I am really puzzled. – Rmano Feb 20 '15 at 16:45
  • @Rmano Hm, it seems autocmd works different when using --remote? Not sure ... Need to test more ... – Martin Tournoij Feb 20 '15 at 17:10
  • Ok, marking this as answered --- and posting another question. Thanks! – Rmano Feb 20 '15 at 18:00
4

Try the autodir setting in vimrc.

set autochdir
2

I think the ReadPost events aren't applicable since the documentation describes them as related to the :read command. You could try the Enter events, either BufEnter or BufWinEnter. For example:

autocmd BufWinEnter *.tex  lchdir %:p:h

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.