How to generate a ctags files and use it with vim

The ctags command allows you to index source – any source. The command generates a single file called a tags file (which [ironically] is the name of the file). Then you can point to this file with editors like VIM/GVIM/EMACS, etc. for auto-complete and for cross-probing.

Let’s see it in action…

First tag the source you want to index using:

ctags -V -R --c++-kinds=+p --fields=+iaS --extra=+q --languages=c++ .

This will recursively index all files from this point down and generate a ‘tags’ file in the current directory. You should open the tags file and look around to see if it’s getting what you expect and that it isn’t getting things that you don’t expect.

From here we can open vim and start auto-completing. This works because VIM will automatically search the current directory for a tags file, which we just generated. If you want to point to a tags file that lives in another directory, then use:

:set ctags=/path/to/file/with/tags

Now you can start typing. Let’s act like we have a class called MyClass, in VIM you must be in insert mode (press ‘i’) then type…

 MyC<CTRL+N>

…and a drop-down menu should show with the option to auto-complete to MyClass (presuming some MyClass.cpp was indexed with the ctags command).

CTRL+N : to go forward through tags

CTRL+P : to go backward through tags

Now the cool part. Once you’ve auto-completed a word or you put your cursor on any keyword and press CTRL+], then you will see a menu that will let you pick the implementation you would like to go to and once selected VIM will open the source file that defines that keyword.

CTRL+] : to push into tags

CTRL+T : to pop out of keywords

 

Advertisements

How can I easily access my Linux command history?

One of the fastest ways to search your previous commands is to use CTRL+R and start typing, once you’ve entered enough text you can use CTRL+R again and again to search your history for matches.

Let’s assume we execute the following commands:

$ echo dog
dog
$ echo cat
cat
$ echo hotdog
hotdog

Now press CTRL+R and you will see a new “bck:” prompt at the bottom:

$
bck:

Now if you type “dog” you will see the last command that had that string anywhere in it, populated on the previous command prompt:

$ echo hotdog
bck:dog

Pressing CTRL+R again will cycle through the history:

$ echo dog
bck:dog

You can press ENTER to execute the command, or CTRL+E to go to the end of the command without executing it.

I also hear you can use CTRL+S to go backwords through the search results, but that never works for me – I believe my terminal or window manager is swallowing the CTRL+S.