vim设置工作目录

发布于 2024-12-09 01:12:00 字数 442 浏览 0 评论 0原文

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2024-12-16 01:12:00

如果您的全部目的是获得正确的“标签”文件,那么这可以更容易完成:

set tags=./tags,tags;$HOME/programming,$HOME/programming/your/global/tags

tags 选项接受逗号(或空格)分隔的条目列表。在我的示例中,我有以下条目:

  • ./tags 这意味着它应该首先在当前目录
  • tags;$HOME/programming 中查找 tags 文件 这意味着从当前目录到 $HOME/programming 查找 tags 文件(这就是分号的作用,请参阅 文件搜索)。如果您没有使用分号指定“stop”目录,那么它将搜索到根目录 /
  • $HOME/programming/your/global/tags 最后这个是一个由绝对文件名引用的标签文件,

从您的描述来看,我的示例对于您的目的来说可能有点过分了,您只需要这个:

set tags=tags;$HOME/programming

但是如果您确实需要更改工作目录,那么您可以添加类似的内容(更改 lcd< /code> 到 cd 如果你必须)到你的.vimrc:

au BufNewFile,BufRead *.js execute ":lcd " . fnamemodify(findfile("tags", ".;"), ":p:h")

If your whole point is to get to the correct "tags"-file then this could be done easier:

set tags=./tags,tags;$HOME/programming,$HOME/programming/your/global/tags

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 a tags-file in the current directory
  • tags;$HOME/programming this means look for a tags-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 name

My example is probably overkill for your purpose from your description you only need this:

set tags=tags;$HOME/programming

But if you really need to change the working directory then you could add something like this (change lcd to cd if you have to) to your .vimrc:

au BufNewFile,BufRead *.js execute ":lcd " . fnamemodify(findfile("tags", ".;"), ":p:h")
假扮的天使 2024-12-16 01:12:00

免责声明:我是上述插件的作者。

我认为您可以使用这个小 代码路径.vim。我写它是因为我需要一个小功能来帮助我到达我的项目根目录。该插件假设您有一个包含所有代码的文件夹。类似$HOME/code。嗯,它提供了以下功能:

codepath#path()

我与 NERDTreecommand-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:

codepath#path()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文