How to search the tags file and load it automatically when vim start up - vim

Vim is my favorite editor, when I open a php or python file in vim, the first command is:
set tags=../../../tags
or
set tags=../../tags
I think vim can do this automatically:
first search ./tags, if it is exists, set it, if not exists
search ../tags,if it is exists, set it, if not exists
search ../../tags,if it is exists, set it, if not exists
until it reaches the / or D: directory
However, I am not so familiar with the vim scripts.
Any of your help will be appreciated!_

There are a few comments on this particular subject on this question.
Basically, this is supposed to work:
set tags=./tags;/
It starts with a tags file in the current directory and goes up to the root directory.
Type :help tags-option for more details.

Related

Supply a path for file editing in VIM?

Is there a way to set a PATH-like sequence of directories to search for files in vim? My project has C files split across many directories, and it would be nice to jump back and forth without remembering the full path each time.
For instance, if I have:
platform/drivers/uart.c
ui/display/menu.c
cpu/registers/regs.h
I would like to be able to set PATH to "platform/drivers:ui/display:cpu/registers". Then when I want to switch to a file, I can just type:
:e uart.c
instead of
:e platform/drivers/uart.c
I understand that I can change the working directory, but then I have to type
:e ../../ui/display/menu.c
to get to another directory.
Alternatively, is there a better way to navigate a project like this than using :edit?
There is, and it's called path. The way you use path is with the :find command: :find menu.c would search for menu.c in the directories in path and edit it. There are other commands that use path, like :sfind that opens the found file in a new split. See the documentation of path for details and other commands that use it.
Another thing that may help you find your files is the **-wildcard that can expand to any directory path. For example :edit **/menu.c will look for menu.c in subdirectories, so you don't have remember and type the full path.

.vimrc TAGS command errors

I use etags with vim on linux for source(*.c, *.h) code browsing. I have created a TAGS file bu giving command :
etags --members *.c *.h
TAGS file gets created but when I start browsing say one of the source files named 1.c which has a C structure variable defined and used in one of its function definitions(The structure name is a typedef in some other 1.h file). I open the file 1.c in vim and then I do CTRL - ] by placing cursor on that struct type, etags does not browse to the header file 1.h which has declaration of this structure.
This only happens when i have below line in my .vimrc, when i comment below two lines, etags based source browsing works fine.
set TAGS=./TAGS;$HOME
set tags=./tags;$HOME
I am trying to tell vim where to locate the TAGS file. starting from current folder till my home dir. What is incorrect here?
What is the correct syntax for above command?
Also does ctags/etags browsing with vim, show from where all a given function is called from?
If yes, what is the command to see that?
Locating tags file
Vim's settings are case-sensitive so set TAGS= is invalid. You must use set tags= in lowercase.
Vim will stop at the first match so you can't really expect it to search for a tag in tags and TAGS. These files can be searched in turn with:
set tags=./tags,./TAGS;$HOME " 1. tags, 2. TAGS, 3.… until $HOME
Also, search is not performed by etags, it's done by Vim itself.
Jumping to function calls
No, ctags and etags only index declarations. To jump to usage you'll need cscope

vim set working directory

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? :)
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")
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.

Ignore one "misspelling" in Vim

Is there a way to tell Vim not to highlight a word once? For example, in "the password is abc123", I don't want to add abc123 to the wordlist, but still wouldn't like the big red rectangle around it.
Clarification: I'm looking for a command that makes the spell checker ignore the current word (or last misspelling).
Without having the word stored somewhere, it's hard (not to say impossible) to ignore it always.
But, if you are looking to ignore the word really once, that is only for a moment, you can add it to the internal list with the zG command.
*zG*
zG Like "zg" but add the word to the internal word list
|internal-wordlist|.
*internal-wordlist*
The internal word list is used for all buffers where 'spell' is set. It is
not stored, it is lost when you exit Vim. It is also cleared when 'encoding'
is set.
When your cursor is positioned on a word that is highlighted as misspelled you can add it to your wordlist by pressing zg. Vim allows you to load more than one wordlist at a time, which makes it possible to have (for example) a global wordlist, and a project specific wordlist.
By default, when you run zg it will add the current word to the first spellfile it finds in your runtime path for the current encoding. In my case, that turns out to be ~/.vim/spell/en.utf-8.add when I'm working with UTF-8 encoding. Try running the following commands:
:setlocal spellfile+=~/.vim/spell/en.utf-8.add
:setlocal spellfile+=oneoff.utf-8.add
That will set you up so that zg (or 1zg) adds the current word to your default spellfile. But running 2zg would add the current word to a file called oneoff.utf-8.add, in the same directory as the file that you are working on. If the file doesn't exist, Vim will try to create it for you.
When you open the file again in the future, you will have to run the same two commands to make Vim check the oneoff.utf-8.add spellfile. Unfortunately, Vim does not allow you to set the spellfile option in a modeline, so if you want to run these commands automatically when the file opens, you would have to find some other way. This question includes a few ideas on how you might proceed.

How to tame vim's ":find" command

Say, I have files foo.js and bar.css in my project. There is a ":find" command in vim, which find files, matching string. But this command, alas, has some limitations. For example, if I launch this way - "vim", or even this way - "vim ." - there's nothing to be find in js subdirectory. But if I launch vim this way - "vim js/any_file_other_than_foo.js", then calling ":find foo.js" works pretty well.
Since it is not intuitive (i'm working in the same directory, "pwd" returns the same path), my first question is - can anybody explain how to circumvent this issue? And, even broader, is there any way to type something like find foo - and open first file, which name matches pattern foo.
thanks in advance.
You could try
:e[dit] **/*foo* and then press 'tab' to move to the first match.
the ** is a directory globbing pattern, while * is character matching.
If you were so inclined, you could write a simple fuzzy finder command, for more information you can check out the vim tips wiki: http://vim.wikia.com/wiki/Find_files_in_subdirectories
Vim's :find works by searching each directory in the path variable (and ignores pwd). By default, it does not search recursively. That's why find is only working for you when you open a js file. The '.' in path refers to the directory for the current file -- not pwd.
You can change path to include your desired directories:
set path+=$PROJECT/js
See :help path.
One of the magic bits to use is to add ** to a path to search that path recursively:
" search recursively in my project
set path+=$PROJECT/**
" search recursively from the current file's directory
set path+=./**
See :help file-searching for more magic.
A nice plugin that accomplishes a similar effect is Command-T.
The Command-T plug-in provides an
extremely fast, intuitive mechanism
for opening files with a minimal
number of keystrokes. It's named
"Command-T" because it is inspired by
the "Go to File" window bound to
Command-T in TextMate.
Files are selected by typing
characters that appear in their paths,
and are ordered by an algorithm which
knows that characters that appear in
certain locations (for example,
immediately after a path separator)
should be given more weight.should be given more weight.
Here is a screencast of Command-T in action.

Resources