I have a project as follows:
/dir
dir1
dir2 -> symbolic-link to /otherdir
file1
tags *
I want vim to use THIS tags file which includes tags for files in dir1 and dir2.
When I edit file1, VIM cannot find the correct tags file.
I have the following setup in .vimrc:
set tags=tags;/
Is there a way to keep this file structure without explicitly telling VIM the absolute path to tags?
You can append to the same ctags other tags, so for example if you want to ctag everything inside dir1 you would execute:
ctags -R *
and if you want to add some other tags from dir two:
ctags -R -a ~/path/to/dir2/*
-a is for appending.
Now what I do to always have my ctags no matter where I open my vim, is to add this line in my .vimrc:
set tags+=./tags;$HOME
this will look for tags in the current directory and will go down recursively to your home folder, if you would like it to search until the root folder or less just change $HOME for / or /path/to/root/project/
With this line in my ~/.vimrc and a similar layout as yours, tags related features (:ts, <C-]>, etc.) use the same tags file situated at the root of dir, alongside dir1 and dir2.
set tags=./tags,tags;$HOME
The tags file is first searched in the current file's directory, then in the cwd, then upwards until it reaches $HOME.
What does :echo tagfiles() say when you are editing file1? Here it says ['/home/romainl/Desktop/dir0/tags'].
EDIT
Throwing a symlink doesn't seem to change anything.
ENDEDIT
I think it's just a question of being in the right directory. When you start working in this project, use :cd /dir to get into the directory with the tags file, and make sure the autochdir option is turned off. Then when you edit a file inside dir2, the working directory will still be dir, and it will still find the same tags file.
If, on the other end, you end up with dir/dir2 as your working directory, that will actually mean you're in /otherdir, so when Vim looks for the tags file from there, it can't find it in that directory or in / . I suspect that's what's happening to you now.
You can see what directory you're in at any time with the :pwd command, just like in the shell.
Related
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
I want to use Ctags for a Zend framework project. I executed this command : ctags -R ./* ../ZendFramework/*to create a list of tags, but the problem is when I press Ctrl-] on a class or method I get this error: ctags: tag not found
I checked the tags file and all classes/methods/variables are listed. The tags files is located in the root of the project. Should I load it manullay when I open a file?
Yes, you should tell Vim where to find your tags file with something like:
:set tags=/path/to/tags
This is not very optimal, though. This line in your ~/.vimrc should help:
set tags=./tags,tags;$HOME
It tells Vim to look for a tags file in the directory of the current file, in the current directory and up and up until your $HOME (that's the meaning of the semicolon), stopping on the first hit.
The 'tags' variable must point to your tags file. See :help 'tags'.
An example to add the path to your tags file:
:set tags+=$HOME/yourpath/tags
I Faced the same problem few days ago. I was applying ctags shortcuts in a .c file and I was getting this error while doing so. I googled the error and found that the ctags was not installed. But the ctags is present in my server. I tried moving the ctag folder to the trunk which i'm currently working and this trick resolved my problem.
steps:
go to your home folder and enter "where is ctags"
it will display the path of the ctags file.
copy that file and move the same to the directory which you are working in
i hope this will resolve your issue.
I'm reading a tutorial on how to set up Exuberant Ctags for a multi-level directory structure, and I saw the following line,
Configure your editor to read the local tag file first, then consult
the global tag file when not found in the local tag file. In Vim, this
is done as follows: :set tags=./tags,tags,~/project/tags
Question is: (and this is probably more general than just Ctags) Why are ./tags and tags both specified as places to look? Don't they evaluate to the same thing?
No. From :help 'tags':
When a file name starts with "./", the '.' is replaced with the path
of the current file. But only when the 'd' flag is not included in
'cpoptions'.
So ./tags searchs for the file, not in the current directory, but in the directory where the current file is located.
However, tags does search for the file in the current directory.
Yes, I'd also expect it to work the other way around...
I wanna use ctags in my project.and everything goes well during installaion.
./configure
make
sudo make install
then unzip Taglist. Get tag file in my source path
ctag -R
add tags path in .vimrc.
set tags=/home/lee/program/apue;
set autochdir
then begin my c file.
vim main.c
but when I press ctrl+], i comes "no tag file found".what's the problem?
Does the file /home/lee/program/apue really contain your tags (i.e. is it the output file of ctags)?
You should point the tags variable in your vimrc to the tags file that ctags generates. By default, that's a file named tags in the directory where you start the ctags command.
Supposing the current directory is populated with a bunch of files, the correct command to generate a tags file is $ ctags -R .. I assume ctag -R is a typo.
Is there a tags file at /home/lee/program/apue? Why is there a ; at the end?
From your question it looks like you start coding a new file AFTER generating a tags file in an empty directory. Since there is no code there is nothing in the tags file.
Now I have two directory, all of header files *.h are included in directory /inc, while all of c file *.c are stored in /src directory.
The directory just like this, (/project is a up level directory):
/project-- |----/inc
|----/src
I want to use ctrl+] to locate definition of one parameter or one function in a source file like example.c. How to generate those tags?
My method is:
(1) cd to the /project directory
(2) ctags inc/*.h src/*.c
Then a tags file is generated there, however, when I open a example file and using "Ctrl+]", it cannot lead me to its definition. Why???
Do I need to generate a tags file under /src???
Any help? Many thanks!
In vim, try typing:
:pwd
:set tags
Verify that the path to your tagfile is present in output of 2, relative to the path that is the output of 1.
NOTE: You can set the tags variable as part of your local .vimrc.
UPDATE: It is common to set tags to a pattern like tags,../tags,../../tags. With this pattern, vim will use the first tags file that it finds your folder structure (again relative to your pwd).
Go to /project, and use the command "ctags -R ." In your .vimrc, put the command "set tags=/project/tags". Exit vim and enter it again. Tags should now work.