Vim working directory

When I switch buffers to Vim (using: bn and: bp), I want it to automatically set the working directory, BUT it was not an open file directory. I want Vim to look up recursively for a file called tags, and when it finds this 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 the root of the project. Suppose the tag file is in the foobar directory. Vim should now look in "js", but there are no tag files. Then it should look in "src", there are no tag files there. Then it should look in "foobar" and find the file "tags", and then do:

 :cd ~/programming/projects/foobar/ 

Is there an easy way to do this? :)

+6
source share
2 answers

If all you need to do is get the correct "tag" for the file, then this can be done easier:

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

The tags parameter takes a comma (or space) list of entries. In my example, I have the following entries:

  • ./tags this means that it must first look for the tags file in the current directory
  • tags;$HOME/programming this means searching for tags file from the current directory to $HOME/programming (for what a semicolon does, see file-searching ). If you do not specify a "stop" directory using a semicolon, then it looks up to the root directory /
  • $HOME/programming/your/global/tags Finally, this is a tag file referenced by an absolute file name

My example is probably redundant 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, you can add something like this (change lcd to cd if necessary) to your .vimrc:

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

Disclaimer: I am the author of the mentioned plugin.

I think you could use a small codepath.vim . I wrote this because I needed a little function that would help me reach my project root. The plugin makes the assumption that you have a folder with all your code. Something like $HOME/code . Well, it provides the following function:

 codepath#path() 

I use in combination with plugins like NERDTree or command-t . Therefore, I can open the NERDTree window in the root of my project. This is a really small plugin, but I use it all the time.

0
source

Source: https://habr.com/ru/post/898917/


All Articles