How to merge completion candidates for vim? - vim

I often rely on omni-completion to edit source codes, so my current .vimrc contains following setting to gain quick access to intended candidates:
inoremap <C-f> <C-x><C-o>
Now I find there are many kinds of ins-completions except for omni-completion and become interested to use both tags and file names completions too.
1. Whole lines i_CTRL-X_CTRL-L
2. keywords in the current file i_CTRL-X_CTRL-N
3. keywords in 'dictionary' i_CTRL-X_CTRL-K
4. keywords in 'thesaurus', thesaurus-style i_CTRL-X_CTRL-T
5. keywords in the current and included files i_CTRL-X_CTRL-I
6. tags i_CTRL-X_CTRL-]
7. file names i_CTRL-X_CTRL-F
8. definitions or macros i_CTRL-X_CTRL-D
9. Vim command-line i_CTRL-X_CTRL-V
10. User defined completion i_CTRL-X_CTRL-U
11. omni completion i_CTRL-X_CTRL-O
12. Spelling suggestions i_CTRL-X_s
13. keywords in 'complete' i_CTRL-N*emphasized text*
The question is, how can I list up whole candidates from these specific completion sources on a ins-complete-menu with single command, <C-f>.

Use the default completion (CTRL-N / CTRL-P); its completion sources can be configured via the 'complete' option. Unfortunately, from your list, only tags (not file names) can be (and is by default) included in there. (But don't you know beforehand that you want file completion? I particularly like the many different completion commands because they narrow down the result list, which for me is far more valuable than not having to think about which completion to invoke.)
If you really want an all-encompassing completion, you'd have to implement that yourself as a user-completion, and you'd have to re-implement all the built-in sources, as there currently is no way to programmatically get them.

You should check the plugin neocomplcache. It can acomplish this but the setting may not be trivial.

Related

How to make vim show search result list dynamically while :tj SomeSymbol is being typing?

all. I know after generating a tag file, when I use :tj SomeSymbol, I can either jump to the expected location when SomeSymbol is unique within the project, or be given a list to choose. But I want more convenient way.
When I'm typing :tj SomeSymbol, I wish there's a popup menu showing all possible locations as if vim was searching the tag file for the expected symbol. In this way I can choose quickly and conveniently.
The final effect I want may be like what qtcreator gives:
So is there any way to do this ?
There is nothing built-in. Vim's completion popup menu currently can only be used for selecting candidate matches to be inserted into the text; it's not a general-purpose selector / filter. For tags, Vim only offers the selection by number as in the :tselect / :tjump commands. However, some plugins have implemented custom filtering (often in combination with fuzzy matching for easy drill-down into the candidate lists). I still use FuzzyFinder, which (though unmaintained for quite some time) offers (among others) a :FufTag command that lets you interactively select from tag matches.

Is there a one-liner to tell vim/ctags autocompletion to search from the middle of a word?

In vim (in Insert mode, after running exuberant ctags), I am using ctrl-x followed by ctrl-] to bring up a dropdown of various possible words/tokens. It's a great feature.
The problem is that by default, this list starts with a bunch of numeric options and automatically inserts the first numeric option, and if I backspace to get rid of the numbers and start typing a part of a word fresh -- with the idea of searching from the middle of the word -- the autocompletion behavior exits entirely.
I know I could type the first letter of the word that I want, then go from there. But that assumes that I know the first letter of the word, which is not necessarily a given.
For example, if I'm working on a pair-programming project with a friend during a long weekend, I might not remember at any given moment whether he called his method promoteRecordStatus(), updateRecordStatus() or boostRecordStatus(). In this example, I would like to type RecordStatus and get the relevant result, which does not seem to be possible at a glance with the current behavior.
So with that scenario in mind: Is there a simple, vim-native way to tell the editor to start its autocompletion without any assumptions, then search all available tokens for my typed string in all parts of each token?
I will of course consider plugin suggestions helpful, but I would prefer a short, vim-native answer that doesn't require any plugins if possible. Ideally, the configuration could be set using just a line or two.
The built-in completions all require a match at the starting position. In some cases, you could drop separator characters from the 'iskeyword' option (e.g. in Vimscript, drop # to be able to complete individual components from foo#bar#BazFunction()), but this won't work for camelCaseWords at all.
Custom :help complete-functions can implement any completion search, though. To be based on the tags database, it would have to use taglist() as a source, and filter according to the completion base entered before triggering the completion. If you do not anchor this pattern match at the beginning, you have your desired completion.

How to find all occurrences of a variable in Vim?

In vim, how to I find all occurrences of a variable in files under a certain directory?
I know vimgrep works sometimes, but it looks for text only and doesn't work if other classes have variables of the same name and I only want the variable under a specific class.
What should I do? Or should I get an IDE instead?
Why would you want to use another IDE when you already have one? Vim is an IDE that is configurable and usable for different languages..
You could use cscope to build a database of your code. This database
Allows searching code for:
all references to a symbol
global definitions
functions called by a function
functions calling a function
text string
regular expression pattern
a file
files including a file
Further features of Cscope:
Curses based (text screen)
An information database is generated for faster searches and later reference
The fuzzy parser supports C, but is flexible enough to be useful for C++ and Java, and for use as a generalized 'grep database' (use it to browse large text documents!)
Has a command line mode for inclusion in scripts or as a backend to a GUI/frontend
Runs on all flavors of Unix, plus most monopoly-controlled operating systems.
Once your database is created, you could browse through the usages of your variables, functions, etc.
Edit (slightly off-topic):
another cool thing that's quite handy when working with Vim on code is the taglist plugin that uses Ctags:
The "Tag List" plugin is a source code browser plugin for Vim and
provides an overview of the structure of source code files and allows
you to efficiently browse through source code files for different
programming languages.
cscope step by step example
Go to the base directory of your project, and run:
cscope -Rb
This generates a cscope.out file which contains the parsed information. Generation is reasonably fast, even for huge projects like the Linux kernel.
Note that cscope is not designed to work with other languages other than C. Sometimes it does work for other C-like syntax languages like Python, and you can force it to recognize those files with hacks such as cscope -Rb -s * and others mentioned at: Using cscope to browse Python code with VIM? but it won't work as well as for C.
Open vim, and run:
:cs add cscope.out
:cs find s my_func
s is a mnemonic for symbol. The other cscope provided queries are also possible.
The cscope interface (ouside Vim) also has a variable assignment query (subset of symbol occurrences) which Vim does not seem to offer (?)
This adds a list of the callers to the quickfix list, which you can open with:
:copen
Go to the line that interests you and hit enter to jump there.
See also:
automatically add the nearest database (parent directories) when you enter a file: how to auto load cscope.out in vim
for function calls: How to find the callers and callee of a function in C code in vi/vim?

VIM: Respect only current code block

I have started working on a huge PHP application that has thousands of lines of code in each file, with lots of huge if blocks, classes, and functions all existing in the same file. I'm not the only dev working on it, so I cannot refactor!
I have tried using the Tags List plugin but it does not really help. Is there any way to have VIM respect only a particular code block, and ignore the rest of the file? I am hoping for some or all of these features:
Enable line numbering only for the current code block, starting from 1 at the line containing the opening {, and showing no numbering for lines preceding it or after the closing }.
Searching with / would be restricted only to the block in question.
I am thinking along the lines of selecting the current block and editing it in a new buffer when enabling the mode, then replacing the existing block with the edited block when exiting the mode. However, I am having trouble actually implementing this feature. My current version is this:
map <F7> <Esc>mO<C-V>aBy:new<Return>p:set nu<Return>:set ft=php<Return>ggi<?php<Return><Esc>
map <F8> <Esc>ggdd<C-V>aBx:bp<Return>`O<C-V>aBp
However, this has several issues, such as the inability to perform incremental saves.
I would be very surprised if Vim allows the kind of line numbering you ask for.
This plugin (and 1 or 2 similar ones IIRC) allows you to visually select a region of your current file, work on it in another buffer and put everything back in its place in the original file on :w.
Even if it's not the solution you are wanting, I think the following can help you to solve your problem.
You can use phpfolding plugin, which folds by PHP syntax (functions, classes, methods, PhpDoc...)
You can then select a fold by pressing v$ over the closed fold and execute whatever you want with :whatever. For example, :s/this/self/g to substitute all this for self in the fold. When you press :, vim will automatically add '<,'> to denote following command it's only for the visually selected text.

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