vim设置工作目录
当我在 Vim 中切换缓冲区时(使用 :bn 和 :bp),我希望它自动设置工作目录,但不是打开文件的目录。我希望 Vim 递归地向上搜索一个名为“tags”的文件,当它找到该文件时,将工作目录设置为包含“tags”文件的目录。
示例:
:e ~/programming/projects/foobar/src/js/scripts.js
如您所见,“foobar”是一种“项目根”。我们假设“tags”文件位于 foobar 目录中。现在 Vim 应该在“js”中查找,但没有标签文件。然后它应该在“src”中查找,那里没有标签文件。然后它应该查看“foobar”并找到“tags”文件,然后执行以下操作:
:cd ~/programming/projects/foobar/
有没有简单的方法可以做到这一点? :)
When I switch buffers in Vim (using :bn and :bp), I want it to automatically set the working directory, BUT not to be the directory of the opened file. I want Vim to search recursively upwards for a file called "tags", and when it finds that file, set the working directory to the directory with the "tags" file.
Example:
:e ~/programming/projects/foobar/src/js/scripts.js
As you can see, "foobar" is kind of the "project root". Let's assume the "tags" file is in the foobar directory. Now Vim should look in "js", but there's no tags file. Then it should look in "src", no tags file there. Then it should look in "foobar" and find the "tags" file, then do:
:cd ~/programming/projects/foobar/
Is there a simple way to do this? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的全部目的是获得正确的“标签”文件,那么这可以更容易完成:
tags
选项接受逗号(或空格)分隔的条目列表。在我的示例中,我有以下条目:./tags
这意味着它应该首先在当前目录tags;$HOME/programming 中查找
这意味着从当前目录到tags
文件$HOME/programming
查找tags
文件(这就是分号的作用,请参阅 文件搜索)。如果您没有使用分号指定“stop”目录,那么它将搜索到根目录/
$HOME/programming/your/global/tags
最后这个是一个由绝对文件名引用的标签文件,从您的描述来看,我的示例对于您的目的来说可能有点过分了,您只需要这个:
但是如果您确实需要更改工作目录,那么您可以添加类似的内容(更改
lcd< /code> 到
cd
如果你必须)到你的.vimrc:If your whole point is to get to the correct "tags"-file then this could be done easier:
The
tags
option accepts a comma (or space) delimited list of entries. In my example I have the following entries:./tags
this means it should look first for atags
-file in the current directorytags;$HOME/programming
this means look for atags
-file from the current directory up to$HOME/programming
(that's what the semicolon does, see file-searching). If you don't specify the "stop"-directory using the semicolon then it searches up to the root directory/
$HOME/programming/your/global/tags
lastly this is a tags file referred to by absolute file nameMy example is probably overkill for your purpose from your description you only need this:
But if you really need to change the working directory then you could add something like this (change
lcd
tocd
if you have to) to your .vimrc:免责声明:我是上述插件的作者。
我认为您可以使用这个小 代码路径.vim。我写它是因为我需要一个小功能来帮助我到达我的项目根目录。该插件假设您有一个包含所有代码的文件夹。类似
$HOME/code
。嗯,它提供了以下功能:我与 NERDTree 或 command-t。所以我可以在我的项目根目录中打开一个 NERDTree 窗口。它确实是一个小插件,但我一直在使用它。
Disclaimer: I'm the author of the mentioned plugin.
I think you could use the little codepath.vim. I wrote it because I was in need of a little function that would help me to reach my project root. The plugin makes the assumption you have a folder with all your code. Something like
$HOME/code
. Well, it provides the following function:I use in combinations with plugins like NERDTree or command-t. So I can open a NERDTree window in my project root. It really is a little plugin but I use it all the time.