How can I use `path` and `lvim` to find files easily? - vim

At the moment I use :Explore to navigate to my project directory, I press 'c' to set that as the current directory. I have set path=.,, in my .vimrc, and then I use:
:lvim pattern ./**/*
to find patterns in files. This does work, but I have to type ./**/* instead of just * or *.rb for example. Is there a setting for path that I can use to avoid having to do that?
Also, I am working in an environment where I cannot use plugins at all. (Also I would rather use vim plain-and-simple anyway) Thanks!

:lvim, :lgrep, :vim and :grep don't use the path option: they work by default from the current directory, from the directory of the current file if you use ./ or from any arbitrary directory if you use an absolute path.
The value of path has no effect whatsoever: it is used for other unrelated commands.
:lvim pattern ./*.rb
would search for pattern in every Ruby file under the directory of the current file.
:lvim pattern *.rb
would search for pattern in every Ruby file under the current directory.
The current directory and the directory of the current file may or may not be the same. If both the current directory and the directory of the current file are identical, the two commands above should have the same outcome but, if they are different, those two commands could have very different outcomes so make sure you are able to tell the difference.
If you want to search in the current directory, use:
*.rb
If you want to search recursively in the current directory, use:
**/*.rb
If you want to search in the directory of the current file, use:
./*.rb
If you want to search recursively in the directory of the current file, use:
./**/*.rb
Since you have changed the current directory with netrw's c, it seems logical to assume that you want that directory to be the base for your search. The right command would therefore be:
:lvim pattern **/*.rb

Related

vim: using "find", how to get the path relative to the working directory

how can I get the path relative to the working directory of the file using find? now I just get a part of the path..
I mean, when I write find **/*Confirm and press Tab I get this:
:find UserArea/activacionGarantiaConfirm.html.twig
but I expected the path relative to my working directory:
:find Project/views/UserArea/activacionGarantiaConfirm.html.twig
By default, :find's completion doesn't print any part of the path of potential files beyond the file name. Adding a "starstar" wildcard forces it to print the parent directory and another "starstar" will print another parent and so on…
If seeing the full path of every file is important to you, you should use :edit which prints the complete path, relative to the current directory by default.

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

./file versus file

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...

Searching with command-T

When I search some file with command-T it often failes to find it because I'm not in the right directory, so I have to change the directory.
Is it possible to set that command-T will search first in the directories that are bookmarked in Nerdtree or somewhere else?
I could change the directory to / but this search very large scope of files. When I change the dir to my home directory and I'm looking for something ordinary like .bashrc I will find rather many files that are located under .wine directory.
In 99 % of time I need to search files in project directories that I actively work with. Can I set these directories in some preferences?
According to the documentation you can exclude directories from your search:
*command-t-wildignore*
|'wildignore'| string (default: '')
Vim's |'wildignore'| setting is used to determine which files should be
excluded from listings. This is a comma-separated list of glob patterns.
It defaults to the empty string, but common settings include "*.o,*.obj"
(to exclude object files) or ".git,.svn" (to exclude SCM metadata
directories). For example:
:set wildignore+=*.o,*.obj,.git
A pattern such as "vendor/rails/**" would exclude all files and
subdirectories inside the "vendor/rails" directory (relative to
directory Command-T starts in).
So if you wanted to exclude a backup dir, you would write:
set wildignore+=project/backup
in your .vimrc
In addition, to ignore dotfiles/dotdirs you can look into these options:
g:CommandTNeverShowDotFiles
g:CommandTScanDotDirectories
g:CommandTMaxDepth
These allow you to:
- ignore dotfiles completely;
- stop searching recursively in dotdirs;
- specify at what depth should Command-T stop scanning.
I found this information in the author's git, but you can probably see this document by issuing in vim:
:help Command-T
(or a similar name)
I did not see any reference to preferences or bookmarks in the plugin.
However if you start vim by opening a file in said project directory you might want to add this line to your .vimrc:
set autochdir
This option will set on startup your directory to the current file's directory.
You could try Ctrl-P. I had the same problems as you do and making the change solved them.
I also ignore some folders (in .vimrc):
let g:ctrlp_custom_ignore = {
\ 'dir': '\.git$\|\.hg$\|\.svn$',
\ 'file': '\.exe$\|\.so$\|\.dll$' }

ctags problem when generating tags for .h and .c file in two different directory

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.

Resources