read tags when buildir is not srcdir - vim

I' using an autotools project with source on
~/jhbuild/checkout/<project>
and compilation on
~/.cache/jhbuild/build/<project>
inside ~/.cache/jhbuild/build/<project> I can make
$ make ctags
and tags are generated correctly for ~/jhbuild/checkout/<project>
but I would like to open vim on ~/jhbuild/checkout/<project> to be able to navigate files directly (not only with tag definitions). Is there a way to instruct vim to search tags recursively on cache dir?
On ~/jhbuild/checkout/<project> I can make ctags -R * and mostly have the same (with a non recursive tag file) Is there any benefit in use autotools ctags rule? I see CTAGS_DEPENDECIES CTAGS_FLAGS CTAGS_ARGS but never see anyone using it

Related

How to run vim commands/scripts from project root?

I have a multitude of commands I'd like to run not in the current directory, but in the project root directory. i.e. going up directories until I reach some indication of a root, like .git directory for example.
For example running vimgrep -r (recursive) on all my project, or running tags generation recursively on whole project.
How do I get that path? The only close indication I found is this:set tags=./tags;~/Projects
But that just saves the string as it is into tags. Assigning something similar but for the use case described, gets me the string verbatim.
Any help is greatly appreciated!
Thanks!
Avoid the idea of "changing the working directory" or distinguishing between "working directory and project root", because almost no tool is prepared to properly handle those concepts.
The only tools that do (e.g. git) are those that don't care about the current directory to begin with.
Otherwise, it's madness to try to get everything working without bad side effects. "Working directory" is a concept too fundamental to even attempt to change within a running program.
The best approach is open a new Vim sessions inside directories where you want to do "local" things - and switch back to the "project" session to run project commands. Vim will protect you from accidentally overwriting changes in another session.
The alternative is to wrap commands in shells so they can have their "own" working directories, e.g.:
:!cd ../../..; ctags -R
(Which would allow you to regenerate tags file for the project, and not just the current dir)
or:
:!cd ../../..; grep -r foo **/*
But any output with file names would be relative to that root directory, and not the current one.
So you may prefer to do:
:!cd ../../..; vim
which creates a new Vim session within the current one, but in the context of the root directory.
Or, you may prefer the reverse (assuming Vim is running in the project root):
:!cd $(dirname %); vim
Which lets you work in the directory of the current file - and you'd have to exit to the main session to run project-wide tools again.
So instead of "changing" directories, you're "changing vim sessions" (either by having 2 sessions or "nesting" one in another like above).
I use a local_vimrc plugin to set project related variables. Then, I use (/write) plugins that rely on (buffer/project)-local variables to do stuff. (Unfortunately, most plugin out there rely of global variables which is not the best choice to specialize their behaviour to the current project in multi-projects sessions)
Regarding ctags generation, in use lh-tags that requires a few variables to be set.
Regarding grepping, as other, I usually start from the current directory which is often the root directory of my project. But you could also easily have a plugin/command that would run something like:
exe "vimgrep -r ".pattern." ".
\ map(file_extensions,
\ string(lh#option#get('project_root_directory').'/**').'v:val')
EDIT:
If you don't want other configuration files, and if the .git/ directory is enough to identify your project root directory, then, you can have this kind of function to get your project root directory:
function! ProjectRootDirectory()
return fnamemodify(finddir('.git', '.;'), ':h')
endfunction
Then when you'll run ctags, you'll have to execute ctags from the result of this vim function. I don't know which plugin you use to run ctags, at this moment. My lh-tags doesn't support functions through its configuration variables. It can easily been added if need be.
However, I don't see a simple way to configure &tags once from the .vimrc in order to configure this setting on a per project basis.
I personally like to keep it simple and have my current directory be my project root. I can use % to represent the current file in commands, e.g. :!git add %. Along with % you can use filename modifiers, e.g. :e %:h/foo.txt. See :h filename-modifiers.
However you mentioned that you use CtrlP and you like the way it changes the current directory. This means you need to be a bit more creative.
Here is what I would recommend:
Update your tags via git hooks. See Effortless Ctags with Git
If your tags are in .git/tags fugitive.vim will automatically setup these tags for you.
Use :Ggrep which uses git grep so it already know about your project root directory and as a bonus is much faster than :vimgrep.
Running arbitrary commands from the root is trickier. I use projectionist to manage my projects. Projectionist provides the command, :ProjectDo, which does exactly what you want.
Now a word of caution: Vim has no understanding of a "project". The closest thing to is 'exrc' option (See :h 'exrc') which is pretty lame.

ctags, generate tags using multiple paths?

When I build/update my tags file, ctags starts at the current directory and works its way down recursively.
I would like for it to also include a completely different search path, a mapped network drive, and add those results to my tags file as well.
Is there any way to do that?
When the files in the other directory are related and often change together with the current directory hierarchy, I'd write a custom :Ctags command that supplies the other path to the :!ctags call.
If the other files are unrelated, and rarely update (as based on your comments seems to be the case), I'd run ctags there separately and include them via
:set tags+=/path/to/other/dir/tags
NOTE: Add the tag filename at the end, else there will be "tag not found" error. By default the name is tags but it could be renamed with -f option as below.
ctags -f my_tags -R
:set tags+=/path/to/other/dir/my_tags

No tags file in GVim on some file but not on others

I just installed ctags via homebrew and appended the following line in my ~/.vimrc:
set tags=./tags,tags;$HOME
And then I ran /usr/local/bin/ctags -R . on some of my directories and opened some files stored in the directories, then some of those scripts succeeded in importing tags file but others didn't.
For example, I opened up test.py on my Python workspace, which I already run the above command in, and then I tried to put Ctrl+] on my GVim, it looks like successfully imported the tags file.
I also opened up hello.go located in ~/go/src/github.com/user/hello, in which I already executed the above ctags command, successfully imported the tags file. However, my test.rb file, which I just put on the Go's directory in order to do test purpose, didn't import the tags file correctly.
Also, when I executed the ctags command on ~/another_go_workspace/src, and then opened up the file located in ~/another_go_workspace/src/hello/hello.go, then the file didn't import the tags file... However, since I appended set tags=./tags,tags;$HOME on my ~/.vimrc, doesn't it automatically look for higher directories, right?
So what am I missing?
And if it doesn't import the tags file in higher directories, do I have to execute the ctag command on EVERY directory, i.e. on ~/go/src/soccer_analysis, ~/go/src/coffee, ~/go/src/utility, etc, etc... ?
Thanks.
Your value for the tags option is correct and your assumptions about its behaviour are correct too.
With your setting, set tags=./tags,tags;$HOME, Vim will search for a tags file in the directory of the current file first then for a tags file from the working directory upward to $HOME.
This allows you to generate a tags file at the root of your project and be sure that Vim will pick it up wherever you are in your project and whatever the working directory is.
With the following structure and your current settings:
project/
bar/
bar.js
foo/
foo.js
project.js
tags
Vim should find tags in all the following scenarios and their variants:
$ vim project.js
$ cd foo && vim foo.js
$ cd bar && vim bar.js
$ vim foo/foo.js
$ vim bar/bar.js
$ cd bar && vim bar.js ../project.js
Every time you add a new file to your project or write to an existing file, you must re-index your whole project. From what you wrote about the ruby file, it looks like you didn't run ctags after adding the file. Try this for a selection of files in your project: :echo tagfiles().
No, vim doesn't go up directories to find tags files. I recommend you start vim from the top level directory (where you generated your tags), then traverse to whatever file you want.
vim go/src/coffee
Vim is capable of navigating filesystems nicely with commands like :Explore.
EDIT: I was wrong, semicolon can be used to search upwards. See :help file-searching
Also, I noticed that you tried to add $HOME to your tags, which isn't going to work for a number of reasons.
Documentation (:help 'tags') says:
Filenames for the tag command, separated by spaces or commas.
Therefore:
The delimiter is incorrect
$HOME is going to be treated like a tags file
So the "correct" way of doing this would be:
set tags=./tags,tags,$HOME/tags
Even if you do that though, I don't think it's going to work. Tags files comprise primarily of 2 elements, a search pattern and a filename. If you generated the file from the top, all filenames will be relative to that directory.
So if you are deep down in some subdir, vim will try to open the file using the relative filepath from the top, starting at that subdir.
The problem may have been caused by a typo. I think
set tags=./tags,tags;$HOME
should be
set tags=./tags;,tags;$HOME

Managing multiple makefiles in vim

I would like to effectively manage different project in Vim. When it is time to rebuild I have several Makefiles in different locations. In Vim if I use :make it will try to compile the Makefile in my current location.
I can use or :make -f URL-MAKEFILE but it's too much work. Is there a way or a plugin to build all those separate makefile in an efficient way without me remebering all Makefile url?
You can instruct vim to find a "Makefile" file upwards in your directory structure, then call :make with it
:exec ":make -f " . findfile("Makefile", ".;")
That way it will call the Makefile related to the project tree of the buffer currently open.
However, I'd advise not to use make -f path_to_makefile that much, because it launches the make command inside the directory where you are. Usually Makefiles are written to be used at the top of your project tree (and then eventually call more Makefile in the subdirectories), so it's more natural to call "make -C path_to_top_of_tree"
To find the first directory upward with a Makefile in it then call :make -C with it:
:exec ":make -C " . fnamemodify(findfile("Makefile", ".;"), ":h")
Don't know if there is a such plugin, but you can create a kind of a shortcuts for your URLs.
Create main makefile and add targets such:
.PHONY : shortcut
make -f URL target
Afterwards you can just call :make shortcut
I'm maintaining multiple projects, each one having its own makefile with one of the numerous local_vimrc scripts.
Actually I'm also encapsulating the update of the &makeprg option with my BuildToolsWrapper plugin (prefer Vim-Addons-Manager to install it). It goes further and enables me to choose the compilation directory and other options (which is quite useful when the makefile has been generated with CMake). See the two *.vim files here to see how I configure my projects.

How to get ctags working inside vim

I'm new to vim and wanted to get ctags integration working so I can more easily navigate a large java project.
I've pulled down the zip from source forge and extracted it but from here I'm not sure how to get it working with vim
Any help for a novice vim user would be great!
As nobody has given one critical function in these answers, I'll provide one more slightly superior answer.
The easiest way to use ctags with vim is by calling:
ctags -R *
from the root of your source repository. This will generate a tags file in that same directory.
In your ~/.vimrc file, add this short block:
" ctags optimization
set autochdir
set tags=tags;
" denotes a comment. set autochdir tells vim that if it doesn't find a tags file in the $PWD it will look in the directory parent for the tags file, recursively. set tags=tags; tells vim that the name of your tags file will always be the same as the default tags file generated by ctags.
So long as you run ctags -R * in your root source directory the first time and occasionally to update it (if you pull new changes from others) then you'll always have a fast and intuitive ctags symbol lookup in vim.
Using exuberant ctags, I use something like this in my project's base directory (excluding the "log" directory):
ctags -R --exclude=log *
You have to run the ctags command with the source files as arguments. This will create a tags file containing all information. Then you can open a file with vim, and e.g. press Ctrl-] when on a line with a function to jump to the code of that function. If vi isn't started in the same directory as the tag file, you can set it with :set tags=<file>
This is what I'm doing:
ctags -n -f [OUTPUT] [SOURCE] to generate the tags (NOTE: the -n applies to me but may not be necessary for your usage)
exec "set tags=" . [OUTPUT] inside of .vimrc to let vim become of aware of the tags
EDIT: I'm using
Exuberant Ctags 5.5.2
VIM 6.1
Additional info:
See ctags usages here
Tips and tricks from SO
look at this article: vim-easytags. i haven't tried this, but it looks quite good. manually creating and updating tags was really annoying. hope this will help. :)

Resources