5

This question is a follow-up to Switching to the local directory after loading a file?

I have the following in my .vimrc:

autocmd BufReadPost *.tex lchdir %:p:h

with the intent of setting the current directory to the same as the edited file for *.tex files, without setting the global autochdir.

It works if I call:

 gvim /home/romano/education/IntroEle-SAP-135/Lab-T1/LabTestExample.tex

Look at the full filename in status line here:

Working autocmd lchdir

But it does not work if I call:

gvim --servername desktop_0 --remote-tab-silent /home/romano/education/IntroEle-SAP-135/Lab-T1/LabTestExample.tex

Screenshot:

Not working autocmd

(the culprit seems to be the --remote-tab-silent switch). How can I correct this so that the autocmd works in the two cases?

Rmano
  • 758
  • 5
  • 18

1 Answers1

1

I found a half workaround. The trick is that if you use --remote-send instead of --remote you can make all the autocmd working.

So I subsituted

gvim --servername $desktop --remote-tab-silent "$file"

with

 gvim --servername $desktop --remote-send "<ESC>:tabe $file<CR><CR>"

...and the autocmd works now. <ESC> is needed because the instance can be in input mode (annoyingly beeps if not) and the double <CR> is needed because otherwise vim is asking me to press a key --- I do not know why. Now, this do not work if $file has spaces in it; but this is OT here I think. Or maybe another question.

Now, there is no --remote-send-silent option do my script for double clicking on a file in nautilus and have it opened in the existing desktop-local gvim instance or open a new one has got a bit more complex (if anyone want to see it I can post it here, but it seems a bit OT).

Rmano
  • 758
  • 5
  • 18
  • 1
    I had this problem too and worked around it by using BufReadPost,BufEnter,BufWinEnter, but it still appeared sometimes. Thanks to this note on --remote-send I was able to find a reason of such behaviour, see this vim-dev post. So maybe it will be fixed in Vim soon. Another temporary half workaround is to set 'autochdir' option, if you're fine with changing directory for all filetypes. – xaizek Mar 01 '15 at 08:35
  • @xaizek thanks --- this is clearly the case for the behavior descried here. I have a workaround in the linked post --- I do not like autochdir for all files (even for TeX files, I do want the switch only for .tex, not for example .sty...) – Rmano Mar 01 '15 at 10:03
  • 1
    The patch has been accepted as 7.4.678. – xaizek Mar 24 '15 at 21:45