Vim ctags behaves strangely - vim

I'm starting out Lua development, so I ran ctags on a simple starter project and tried jumping around the source code with Ctrl + ]. Upon trying this, I got E426: tag not found: cache_objects where cache_objects was a function in another file. I checked the tags file and the correct entry was there for cached_objects. I also checked my tags path and it was correct.
I then tried explicitly executing the tags command: :ta cache_objects. This returned the same error. Now things are about to get weird. I executed: :ta /cache_objects, and it worked! It brought me to the function defined as:
function cache_objects (basedir)
...
I triple checked the spelling to make sure it was right. How could this be happening?

This is a bug in ctags. http://sourceforge.net/p/ctags/bugs/347/
If you noticed in your tags file that fields are tab delimited. However when ctags generated the cache_objects tag it included the space after it. vim only looks for complete words when using <C-]> which is why it didn't find the tag but did find it when you used a regex search. If you change the line to
function cache_objects(basedir)
it works.

Related

problem in jumping to system verilog macros `define in VIM using ctags

I checked following things, tags file path is set properly.
the macro tag i am looking for exist in the tags file. task/function/parameters etc works without hiccups. i have problem only with regex.
the expression present in ctags file are
1. --regex-SystemVerilog=/^\s*`define\b\s*(\w+)/`\1/d,define/
2. --regex-systemverilog=/^[ \t]*`define[ \t]*([a-zA-Z_0-9]+)/`\1/d,define/
both the options does not work.
When :tag `altuvm_test_arg works, but the corresponding CTRL-] key doesn't, this often is caused by incompatible 'iskeyword' definitions, so that actually gets passed to :tag is altuvm_test_arg (without the leading backtick).
You can check the scope of keyword matching (when on such a tag) with the * command, which highlights the same keyword under the cursor. If backticks are excluded, try adding the backtick to the range of keyword characters:
:setlocal iskeyword+=`
If this helps (and doesn't break things like syntax highlighting, or annoys you with the changed navigation of commands like w and e), you can make that setting permanent for verilog filetypes put putting it in ~/.vim/after/ftplugin/verilog.vim.

vim: spell checking in partial LaTeX files

I use a problem set class to type up my problem sets. I often have a main.tex file that looks something like the following.
\documentclass{problemset}
\begin{document}
\input{problem1}
\end{document}
I typically have a different file for each problem. For example, problem1.tex might be as follows.
\begin{problem}
there is a spelling errrrrrrror here
\end{problem}
I would like vim to detect the spelling error in problem1.tex, but unfortunately it does not. As noted in this post, the problem seems to be that vim is not able to identify any syntax region: when I run the command
:echo synIDattr(synID(line("."),col("."),1),"name")
I don't get any output. As another example, if I change problem1.tex to the following, then the spelling error is identified.
\section{dummy section}
\begin{problem}
there is a spelling errrrrror here
\end{problem}
I have tried to create a syntax region for my problem environment, but was unsuccessful. My attempt consisted of creating the following .vim/syntax/tex.vim file.
syntax region texProblem start="\\begin{problem}" end="\\end{problem}" contains=#Spell
Nothing seems to happen when I create this tex.vim file. I used scriptnames to check that that the syntax file is being loaded (the default syntax file is also loaded after mine). I can also get the spelling error to be flagged by setting the filetype to plaintex, as suggested here, but this seems like a terrible hack. It seems like there should be a better way to get spell checking in my problem1.tex file.
The comments above worked for me (syntax spell toplevel) and this setting works for me even when run later (so no need to copy anything from /usr). But I didn't want that setting to be active for all tex files - instead, I customize it for the given file in my .vimrc:
au BufNewFile,BufRead body.tex syntax spell toplevel
I used a hack for this problem. Add the following to the top of your file:
\iffalse{}\section{vim-hack}\fi{}
This way, vim's syntax and spell algorithms find some token to get them started, but due to the "if" the section will be ignored by latex itself.
(I did not test whether more specialized editors ignore this dummy section, so there may be more hackery required if you use both vim and an editor.)

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

Lookup a specific kind of tag in Vim

So here's my problem. I've gotten exuberant ctags working with Vim, and it works great, most of the time. One thing that still irks me though is whenever I try to search for a function that is named the same as some variable name. I sometimes get the right tag on the first try, sometimes not. Then after I pull up the list of alternate tags with :tselect, it comes up with a list of tags for both function definitions or variable definitions/assignments. (I'm in PHP so definitions and assignments are syntactically indistinguishable).
However, I notice that there's a column labeled 'kind' that has a value of 'f' or 'v', for function and variable, respectively. I can't seem to find a whole lot of information about this field, it seems like it may not be exactly standardized or widely used. My question is: can you filter tag results in Vim by "kind"?
Ideally, the default would be to search the whole tags file, but by specifying some extra flag, you could search a specific ('f' or 'v') kind only.
This is such a small problem for me as it doesn't come up THAT often, but sometimes it's the small problems that really annoy you.
You can certainly generate ctag files with any combination of php-kinds that you want (see the ouput of the command ctags --list-kinds.)
If you feel it's worth the effort you can make a vim function tagkind and bind it to a command. The tagkind function can overwrite the current tags vim variable to point at only the tag file with the kinds that you are interested in and call :tag. Optionally, it can store the previous version of the tags variable and restore it after this one call.
Unfortunately, I don't know of anyway other than this. Perhaps someone else would know.
I generate python ctags with --python-kinds=-i to exclude tags for import statements (which are useless). Maybe you could generate with --php-kinds=-v and drop a class of tags completely.
You can read :help tag-priority. Apparently the "highest-priority" tag is chosen based on some hard-coded logic.
fzf with fzf.vim has a :Tags (for the whole project) and :BTags for the current file option that generates ctags on the fly.
An issue raised on the plugin 'Skip tag kinds in :BTags and :Tags' gives the following code that you can use to only generated tags for a particular kind. I've modified the below so that it should only search for the PHP f kind.
command! BTagsEnhanced
\ call fzf#vim#buffer_tags(<q-args>, [
\ printf('ctags -f - --sort=no --php-kinds=f --excmd=number --language-force=%s %s', &filetype, expand('%:S'))], {})
Note as per my comment on the question, there is a potential Vim tagfinder.vim plugin via a blog post on Vim and Ctags: Finding Tag Definitions. But I haven't tried it.

Get the file name without file extension in a Vim function

I want to get the file name without the file extension in Vim.
I wrote the following function in my .vimrc file to compile and run the Java program:
:function! JAVA_RUN()
:!javac %^M
:endfunction
map <F3> :execute JAVA_RUN()<CR> :source $HOME/.vimrc<CR>
How can I get the file name without the extension inside the function?
:help expand() should give you the answer, see expand().
You should use the r modifier for %, with %:r instead of % to get the file name without extension.
If you want to write functions to build and execute files, you should also have a look at the documentation for shellescape, in order to prevent problems with spaces in file name or path.
If you want to expand a filename (other than % etc) take a look at fnamemodify()
fnamemodify({fname}, {mods}) *fnamemodify()*
Modify file name {fname} according to {mods}. {mods} is a
string of characters like it is used for file names on the
command line. See |filename-modifiers|.
fnamemodify("main.java", ":r") returns main.
I literally just read a similar question to this (in that someone else seemed to be trying to configure vim to build automagically for them with the F-key), and wrote an answer about how you can leverage the power of vim's :make command without even needing to write a Makefile. In your case, it's less directly related to the question, but I thought I'd mention it in case you were interested.
Furthermore, someone seems to have written something on Vim Tips Wiki about how to set up vim's :make command to specifically work with Java projects built with ant. I haven't worked with Java in a while myself, but in your case specifically it might be a good place to get started.
I came here looking for an answer for a similar question. I wanted to be able to extract the current class name from the java file being edited. I found a very neat way to do this in vim with an abbreviation:
ab xclass <C-R>=expand('%:t:r')<CR>
Place this line in your .vimrc (or similar) for this to work. An abbreviation will auto-trigger as soon as you press space, and so I usually prefix them with 'x' to avoid their accidental expansion.
The trick here is the combination of :t and :r in the argument to expand(). % is the "current file name", :t selects just the tail of the path ("last path component only") and :r selects just the root ("one extension removed"). (quoted parts are from the official expand() documentation.)
So when you are creating a new class in file /a/b/ClassIAmAboutToCreate.java you would type:
public class xclass {
the moment you press space after "xclass", the abbreviation will be expanded to public class ClassIAmAboutToCreate, which is exactly what you need.
Also, note that an abbreviation can be triggered by pressing Ctrl+] which avoids inserting a space after the class name.

Resources