How to trace a word back to where it was declared across multiple files in vim? [duplicate] - vim

This question already has answers here:
Jump to function definition
(11 answers)
Closed 7 years ago.
When reading through a person's code, I may be looking through a program that comprises 10+ files. I would like the ability to search for where an object/struct/type def have been declared.
Does vim allow you to do this kind of search? If so how?

You can use the CTRL-] command to "Jump to the definition of the keyword under the cursor." (see :h ctrl-]).
For this to work, you will need to create a tags file, for example with a program like ctags. The manual has more on this, see :h tag.

Sounds like you could use lvim to grep a word across multiple files.
From the documentation :
to search for the words "house" or "home" in all .txt files in the
current directory, use:
:lvim /\<\(house\|home\)\>/gj *.txt
:lw
You can also integrate external programs, like grep or findstr into vim for faster searching, but those will depend on your OS: http://vim.wikia.com/wiki/Find_in_files_within_Vim#Using_external_programs_for_fast_searches

Related

Open muiltiple(100+) files in `gvim` - not in tabs, not in vim [duplicate]

This question already has answers here:
How to open a file in a list of files in Vim?
(11 answers)
Closed 7 years ago.
I'm analyzing a regression of thousands of test cases in linux. So I want to open many files(failed) in gvim at once.
I have a file as mention in below with 100+ lines specifying 100+ files in a directory. I want to open all these files in gvim(in single gvim).
./Linux2/Lint/case1/DIFF
./Linux2/Lint/case4/DIFF
./Linux2/Lint/case10/DIFF
./Linux2/range/case1/DIFF
./Linux2/operator/case7/DIFF
100+ lines like this.
Is there any way to open files in all these files in gvim with some navigation mechanism.
I know it is possible to use,
gvim file1 file2 .....
But it is not easy with 100+ files.
I need to do this in gvim, not in vim.
I just used find . -name DIFF | xargs vim and was able to navigate through all the files using :next and :prev and :buffers
Only ugly bit was when I hit about 1500 files I'd get two invocations of vim and would have to finish editing the first 1500 files before moving on to the next 1500 (Due to the way xargs works).
Another option is to use vims error file parsing. List all the files in an file with information about the line and error, open that file and run :cbuffer in that buffer (it will close the error file and move you to the first listed erro). :copen will open the list of errors as the Quickfix List. Then you can use the usual :cn and :cp to move between forward and backward in the quickfix list (and <CR> in the quickfix list will work too. Something like this should work out of the box for you.
:./Linux2/Lint/case1/DIFF:1: Not really an error
:./Linux2/Lint/case2/DIFF:1: Not really an error
:./Linux2/Lint/case3/DIFF:1: Not really an error
Now this can be improved since vims error parsing is flexible. You could remove the requirement for the leading : by using set errorformat=%f:%l:\ %m or just use a raw list of files using set errorformat=%f.
It sounds like you want to produce a command line argument from a file. You can do that with command substitution in bash.
gvim $(cat ListOfFiles.txt)

Generally, how do I "go to definition" in VIM? Then how do I with golang?

Two part question:
First, when using VIM what process do I take and what keys do I type to "go to definition" or "go to declaration" etc.? This document might be the answer to my question, but I can't get it to work, so I'm unsure. It looks like its merely text matching the string rather than finding the true definition. If I can get this to work, then will I be able to jump outside of the current document to a definition/declaration? Or does this only work within a single document?
Second, how do I make this work specifically with the Go programming language? It sure would be nice to "click" the Client in
clnt := &http.Client{Transport: tr}
And be taken to the actual code that defines an http.Client.
Possible? How?
As you guess, gd (and other commands) is merely text matching, vim doesn't understand the syntax as it is just a text editor, :h gd will explain how gd works.
Usually, 'go to definition' is brought by using CTRL-] and tag files. A user manual about this topic can be read by :h 29.1.
First you need to generate a tags file for your project, as latest Exuberant Ctags has supported golang (from here), command
cd /path/to/your/project
ctags -f tags -R --fields=+K+a
will do the job.
Second, open vim, by default vim will find tag files under working directory (according to 'tags' option), if the tag file is found successfully, then CTRL-]` should works well.
Also check two useful plugins Tagbar and Easytags.
For golang, you can use the application godef to do it. The pluging vim-go helps you on setting everything, so, you just type 'gd' in a definition and it goes to the exact definition.
https://github.com/fatih/vim-go/blob/master/doc/vim-go.txt

Vim Search with "Current Occurrence / Total Occurrences" Shown at the Bottom [duplicate]

This question already has answers here:
Show Count of Matches in Vim
(11 answers)
Closed 8 years ago.
I am always using */# to search for the next/previous occurrence of the variable or function under cursor. Is there a way to display the current occurrence and the total occurrences of my the search result at the bottom of Vim. For example, it could be something like 1 of 5 when you search a word in Chrome.
It doesn't need to list all the occurrences in a window, but I guess it should be able to know all the occurrences in the background.
You can use vimgrep that will open the list in the bottom
:vimgrep foo %
:copen
:cclose to close the list.
You can use :cnext or :cprevious to navigate in the list of results.
% is an alias for current file name & path.
You can also use the option grepprg and the command :grep to use system grep.
As mentioned by sehe, lgrep or lvimgrp is another possible variable.(with associated lopen, lclose,...)
Have a look at :help grep to see what options are better for you.

run perl script on file I'm editing with vim (macvim)

I have been using bluefish to edit text that are to be published in html. Bluefish has an external filter function that allows me to call on scripts that I have written in perl to "filter" the text I am editing and format them basically using regex.
Having started exploring vim and macvim, I find the program to be very powerful and worth learning. I just would like to be able to use those scripts I already have without having to rewrite them as vim plugins. I have spent the past 2 hours searching but answers seem to be only for running perl as an external command or incorporating perl inside vim scripts.
Just to be clear: I want to get perl scripts I already have to act on text that I am presently editing inside vim/macvim, either the whole text or (better) selected text only.
You can use
:%!command
for example
:%!sort
to sort the whole file being edited.
If you have a range selected it will be added automatically and you complete the command
:'<,'>!command

Search tags only in current file

I am using ":ta " to jump to a method.
For example i got two classes named A.java and B.java. They both have a foo() method and B.java have another method called fooBar(). Then i open A.java and input :ta foo then press TAB then i will got two completion : foo and fooBar. But what i want to jump now is just tag in current file, i don't like tag in other file to display.
And i found tagslist does very good in this job. So if i can use the tag generated by taglist to search from, it will be very nice.
Depending on how many times you call your methods a couple of * may be enough.
Without using tags, gd can be used to go to the local declaration of the method under your cursor. I tend to choose the most low-tech solution usually, so I would go with this one.
But ctags is also able to generate tags for a single file only or for an arbitrary selection of files. It can be done in a few steps but it's definetely not as straightforward as what you are accustomed to do…
Create a file with the name(s) of the file(s) you want to scan. Let's say it's called files.txt and it's located at the root of your working directory.
Generate your tags file using the -L <file> argument: ctags -L files.txt.
At this point you should have a tags file containing only the tags present in the file(s) specified at step 1.
Generating different tags files for the whole project and for single files may be useful, here. A short script generating a tags file named after the current file and making it the sole tags source may make the whole thing easier.
EDIT
Actually, TagList and TagBar don't generate tags files. The output of the ctags <options> command they run is used internally and parsed with all kinds of regexp to filter by scope or filename or whatever.
Unfortunately this cannot be done using ctags. Ctags does not respect context, it is a pure list of all possible "functions". Try to open a tag file with an editor (e.g. vim) and you will see it is just a list of "functions" (in case of Java they are "methods"). Example:
getDesc src/com/redhat/rhn/internal/doclet/Handler.java /^ public String getDesc() {$/;" m class:Handler
getDoc src/com/redhat/rhn/internal/doclet/ApiCall.java /^ public String getDoc() {$/;" m class:ApiCall
Vim just search the file "as is" without giving it any context - it just search for a "function". It is able to search for files, classes, methods, enums etc. Tags format is described in more detail here: http://ctags.sourceforge.net/FORMAT
In Vim you have few possibilities. There are several plugins that gives Vim some context sensitivity, but you cannot use tags for that. Vim itself has a feature called OmniComplete and there are few plugins dedicated for Java. Then you can use Ctrl-X Ctrl-O to start a completition. I recommend you to map this to a different key (maybe Ctrl-Space if you like). More info about Java OmniComplete plugins here:
Vim omnicompletion for Java
Eclim (http://eclim.org/) is very comperhensive, but difficult to setup (you need to run Eclipse in the background). JDE script is easier and also robust (http://www.vim.org/scripts/script.php?script_id=1213). And please note IntelliJ IDEA Community Edition (free) also has a very nice Vim plugin that is free to use. But I understand you - Vim is Vim.
Good luck!
Not exactly an answer to your question, but it seems like there's no way to do exactly what you need, so, i would recommend you the following: for your Java development in Vim, try eclim.
This tool helps you to use your favorite text editor Vim with power of an Eclipse (IDE).
I can't find analogue for tab-completion of :ta, but i know a smart analogue for g] : this is a command :JavaSearchContext. You can map it to something.
For example, if you have two classes A and B, and you have method foo() in each class, then g] will ask you every time you want to jump to foo(), but :JavaSearchContext will always jump to the proper declaration of foo().
Of course, there are many other features.

Resources