如何防止vim设置当前目录

时间:2013-11-28 18:14:35

标签: ruby-on-rails vim

最近我的vim将改变当前目录,无论我做什么。我正在使用spf13发行版,当我在rails app根目录并且vi时,我的pwd将正确地位于应用程序根目录中。但是,一旦我打开某个文件,任何文件,它都会将pwd更改为abosolute/path/to/myrailsapp/app/assets/stylesheets

当我的.vimrc中没有let g:spf13_no_autochdir = 1时,vim会将pwd更改为当前文件目录;当我这样做时,每当我打开文件时它都会变为样式表目录。

我也在使用rails.vim。以下是我.vimrc

中的相关代码
if !exists('g:spf13_no_autochdir')
     autocmd BufEnter * if bufname("") !~ "^\[A-Za-z0-9\]*://" | lcd %:p:h | endif                                                                                         
     " Always switch to the current file directory
endif

更新: 我想要的是:pwd始终保持在absolute/path/to/myrailsapp/,每当我打开文件时都不会自动更改为stylesheet目录。

3 个答案:

答案 0 :(得分:1)

我根据Ben's second answer解决了这个问题。

spf13加载配置文件in order as follows

  1. .vimrc.before - spf13-vim before configuration
  2. .vimrc.before.fork - 配置前分叉
  3. .vimrc.before.local - 用户配置之前
  4. .vimrc.bundles - spf13-vim bundle配置
  5. .vimrc.bundles.fork - fork bundle configuration
  6. .vimrc.bundles.local - 本地用户捆绑配置
  7. .vimrc - spf13-vim vim configuration
  8. .vimrc.fork - fork vim configuration
  9. .vimrc.local - 本地用户配置
  10. if !exists('g:spf13_no_autochdir')检查在(7)完成,因此应在此之前加载let g:spf13_no_autochdir = 1

    我把它放在.vimrc.before.local中,它按预期工作。

答案 1 :(得分:0)

有两种方法可以实现。

最有可能的是,这个“spf13”配置包括set autochdir。要查明是否是这种情况,请正常启动Vim,然后键入:verbose set autochdir?并按Enter键。这应该告诉你如果设置了autochdir并且WHICH FILE将它设置为该值。

如果设置了autochdir,那么你只需要设置一个VimEnter autocmd,或者在〜/ .vim / after / plugin中粘贴一个文件,在spf13加载后再将其关闭。

如果未设置autochdir,则可能是autocmd正在为您设置目录。如果SPF13中有一个插件选项将其关闭,那么就这样做。如果没有,您需要找到插件中的目录更改位置。如果你很幸运,autocmd将自己处于一个augroup中,然后你可以用:au! GroupName删除那个autocmd。这个命令可以在同一个地方;一个VimEnter autocmd,或〜/ .vim / after / plugin中的文件。

答案 2 :(得分:0)

其实我刚刚找到并查看了该插件。我认为就是这样:

https://github.com/spf13/spf13-vim/blob/3.0/.vimrc

在第75行附近,您可以看到:

" Most prefer to automatically switch to the current file directory when
" a new buffer is opened; to prevent this behavior, add the following to
" your .vimrc.before.local file:
"   let g:spf13_no_autochdir = 1

所以只需将最后一行(不带注释标记引用)添加到.vimrc中,您就可以摆脱自动目录更改。

我注意到我的其他答案中的方法都不起作用,因为插件作者无论出于何种原因决定不使用内置选项,也不将其autocmd放入组中。顽皮,顽皮!

相关问题