Introduction
Ctags generates an index (or tag) file of language objects found in source files that allows these items to be quickly and easily located by a text editor or other utility. A tag signifies a language object for which an index entry is available. Vim is the official editor of ctags (No vim plugin required). Plain and simple: ctags lets us jump around our source code using tags and a stack. Ctags (specifically Exuberant Ctags, not the BSD version shipped with OS X) indexes source code to make it easy to jump to functions, variables, classes, and other identifiers in (among other editors) Vim (see :help tags).
Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as 'vi' with most UNIX systems and with Apple macOS. To install the latest version, use homebrew: brew install vim. Homebrew is ranked 2nd while MacVim is ranked 25th. The most important reason people chose Homebrew is: Once installed, you control Homebrew using the.brew. command. Interaction with Vim is centered around several modes. Each mode has a different purpose and switching between them changes behaviour and keybindings.
Prerequisites
- Any Linux based OS with vim
- Source code in any of the ctags supported programming languages
Installation
- Ubuntu
- CentOS
- MacOS
- Windows
How to generate tags file?
cd
to the root directory of your source code- Run Ctags recursively over the entire source code to generate the tags file
- Command to generate tags:
- It would be silly to specify the long command in every source code directory.
~/.ctags
file to our rescue! Just list all the commonly used arguments ofctags
and place it in your home directory and call it.ctags
- If we have a
~/.ctags
file, we can just enter our source code directory andgenerate atags
file using:
Brew Vim
NOTE:
- Feel free to exclude any other directories you don’t wish to search.
- Feel free to explore and use the wealth of ctags command options
How to use tags?
- To search for a specific tag and open Vim with its definition, run the following command in your shell:
$ vim -t <tag>
Demo:
Open any source file in Vim and use the following basic commands:
Keyboard command Action Ctrl + ]
ORg]
OR:ta[g] Ctrl+rw
Jump to the tag underneath the cursor using the information in the tags file(s) :ts[elect] <tag_name>
List the tags that match <tag_name>
, using the information in the tags file(s). When<tag_name>
is not given, the last tag name from the tag stack is used:pts[elect] <tag_name>
Does :tselect
and shows the new tag in a “Preview” window (horizontal split) without moving the cursorCtrl + w }
OR:ptag Ctrl+rw
Opens a preview window with the location of the tag definition. The cursor does not change its position, so tag stack is not updated Ctrl + wz
OR:pc
Close preview window created by the command Ctrl+w }
Ctrl + w Ctrl + ]
Open the definition in a horizontal split :tn
Jump to next matching tag (If there are multiple matches) :tp
Jump to previous matching tag (If there are multiple matches) Ctrl-t
Jump back up in the tag stack :tags
Show the contents of the tag stack. The active entry is marked with a >
Ctrl+rw
pastes the word under cursor in command mode. It’s just a quick copy paste command in vim
Demo
NOTE: vim commands will appear in the bottom left of the screen. Watching the demo in full screen is recommended for laptops.
Skype for business plug in for mac. NOTE:
- I’ve just listed the basic shortcuts and commands that make sense to me and fit my typing speed. There are tons of other shortcuts and similar looking commands in
:help tag
. Feel free to use anything that suits your speed/workflow - Of course there are plenty of plugins out there with bells and whistles. I just don’t like plugins.
tag-regexp
The tag commands also accept a regular expression argument. When using a pattern, case is ignored. If you want to match case, use C
in the pattern. When the argument starts with /
, it is used as a regex pattern. If the argument does not start with /
, it is taken literally, as a full tag name.Examples:
:tag main
jumps directly to the tag “main”:tag /^get
jumps to the tag that starts with “get”:tag /Final$
jumps to the tag that ends with “Final”:tag /norm
lists all the tags that contain “norm”, including “id_norm”:tag /Final$C
lists all the tags that end with “Final” (Doesn’t match “Cipher_final” or “SHA_FINAL”)
When the argument both exists literally, and match when used as a regexp, a literal match has a higher priority. For example, :tag /open
matches “open” before “open_file” and “file_open”.
Demo:
Notes:
- People have written whole books just for regex.This demo doesn’t cover the complex patterns
- This demo covers the regular expressions that I use frequently and are simple to remember
- Feel free to try out your complex regex (regex101)
Sample regex searches with vim
Requirement | Command |
---|---|
Search tags containing ‘aes’ | vim -t '/aes' |
Search tags ending with ‘sha1’ | vim -t '/sha1$' |
Search tags beginning with ‘evp’ and ending with ‘sha1’ | vim -t '/^evpw+sha1$' |
Search tags beginning with ‘EVP’ and ending with ‘sha1’(case sensitive) | vim -t '/^EVPw+sha1$C' |
Search tags beginning with ‘evp’, ending with ‘sha1’ and containing ‘aes_xxx’ in between | vim -t '/^evpw+aes_dddw+sha1$' |
Search tags beginning with alphabets (no numbers) and ending with ‘sha1’ | vim -t '/^[a-zA-Z]w+sha1$' |
Keeping the index file up-to-date
The major downside to Ctags is having to manually rebuild that index all the time. There are multiple ways of automating this stuff:
However, here’s an easy one using vim autocommands:Add the following lines to your ~/.vimrc
file.
Create a ~/.vimrc
if it doesn’t exist
Where,
Example
If you’re in openssl
source code directory, add the following lines to crypto/aes/aes_cbc.c
and exit the file using vim command :wq!
How to format western digital passport for fat32 on mac os. Verify tags
file:
Of course it’s going to rebuild the entire index and take a lotof time for large projects. Remember, I said it’s easy (not quick)
References:
Credits
I’ve compiled a list of essential Vim commands that I use every day. I have then given a few instructions on how to make Vim as great as it should be, because it’s painful without configuration.
Essentials
Cursor movement (Normal/Visual Mode)
h
j
k
l
- Arrow keysw
/b
- Next/previous wordW
/B
- Next/previous word (space seperated)e
/ge
- Next/previous end of word0
/$
- Start/End of line^
- First non-blank character of line (same as0w
)
Editing text
i
/a
- Start insert mode at/after cursorI
/A
- Start insert mode at the beginning/end of the lineo
/O
- Add blank line below/above current lineEsc
orCtrl+[
- Exit insert moded
- Deletedd
- Delete linec
- Delete, then start insert modecc
- Delete line, then start insert mode
Operators
- Operators also work in Visual Mode
d
- Deletes from the cursor to the movement locationc
- Deletes from the cursor to the movement location, then starts insert modey
- Copy from the cursor to the movement location>
- Indent one level<
- Unindent one level- You can also combine operators with motions. Ex:
d$
deletes from the cursor to the end of the line.
Marking text (visual mode)
v
- Start visual modeV
- Start linewise visual modeCtrl+v
- Start visual block modeEsc
orCtrl+[
- Exit visual mode
Clipboard
yy
- Yank (copy) a linep
- Paste after cursorP
- Paste before cursordd
- Delete (cut) a linex
- Delete (cut) current characterX
- Delete (cut) previous characterd
/c
- By default, these copy the deleted text
Brew Install Vim With Python3
Exiting
:w
- Write (save) the file, but don’t quit:wq
- Write (save) and quit:q
- Quit (fails if anything has changed):q!
- Quit and throw away changes
Search/Replace
/pattern
- Search for pattern?pattern
- Search backward for patternn
- Repeat search in same directionN
- Repeat search in opposite direction:%s/old/new/g
- Replace all old with new throughout file (gn is better though):%s/old/new/gc
- Replace all old with new throughout file with confirmations
General
u
- UndoCtrl+r
- Redo
Advanced
Cursor movement
Ctrl+d
- Move down half a pageCtrl+u
- Move up half a page}
- Go forward by paragraph (the next blank line){
- Go backward by paragraph (the next blank line)gg
- Go to the top of the pageG
- Go the bottom of the page: [num] [enter]
- Go to that line in the documentctrl+e / ctrl+y
- Scroll down/up one line
Character search
f [char]
- Move forward to the given charF [char]
- Move backward to the given chart [char]
- Move forward to before the given charT [char]
- Move backward to before the given char;
/,
- Repeat search forwards/backwards
Editing text
J
- Join line below to the current oner [char]
- Replace a single character with the specified char (does not use Insert mode)
Visual mode
O
- Move to other corner of blocko
- Move to other end of marked area
File Tabs
:e filename
- Edit a file:tabe
- Make a new tabgt
- Go to the next tabgT
- Go to the previous tab:vsp
- Vertically split windowsctrl+ws
- Split windows horizontallyctrl+wv
- Split windows verticallyctrl+ww
- Switch between windowsctrl+wq
- Quit a window
Marks
- Marks allow you to jump to designated points in your code.
m{a-z}
- Set mark {a-z} at cursor position- A capital mark {A-Z} sets a global mark and will work between files
'{a-z}
- Move the cursor to the start of the line where the mark was set'
- Go back to the previous jump location
Text Objects
- Say you have
def (arg1, arg2, arg3)
, where your cursor is somewhere in the middle of the parenthesis. di(
deletes everything between the parenthesis. That says “change everything inside the nearest parenthesis”. Without text objects, you would need to doT(dt)
.
General
.
- Repeat last commandCtrl+r + 0
in insert mode inserts the last yanked text (or in command mode)gv
- reselect (select last selected block of text, from visual mode)%
- jumps between matching()
or{}
Vim is quite unpleasant out of the box. It’s an arcane experience:
- Autocomplete is missing
- System clipboard is not used
- Act of typing
:w
to save is cumbersome - Mouse doesn’t work
- Management of multiple files is tricky
- Ability to indent multiple lines is missing
Brew Vimr
It does have a significant strength though: your fingers can stay on the main keyboard keys to do most editing actions. This is faster and more ergonomic. I find that the toughest part about VIM is guiding people towards getting the benefits of VIM without the drawbacks. Here are two ideas on how to go about this.
Switch caps lock and escape
- I highly recommend you switch the mapping of your caps lock and escape keys. You’ll love it, promise! Switching the two keys is platform dependent.
Visual Studio Code
- VSCode is the simplest way to give you a fantastic editor that also gives you the benefits of VIM. Just install the VIM extension.
- I made a few slight changes which improved the experience for me.
Configure native VIM
Donkey kong wii download. For all the given limitations, you’ll need to find a solution. You can either solve the issues one by one, or you can use a reference .vimrc settings file that fix most of the issues out-of-the-box.
- My .vimrc file could be a good starting point. Honestly, it’s a bit old and not the best. I now use VSCode mainly so I haven’t kept a great vimrc.
Using the system clipboard
'+y
copy a selection to the system clipboard'+p
paste from the system clipboard- If this doesn’t work, it’s probably because Vim was not built with the system clipboard option. To check, run
vim --version
and see if+clipboard
exists. If it says-clipboard
, you will not be able to copy from outside of Vim.- For Mac users, homebrew install Vim with the clipboard option. Install homebrew and then run
brew install vim
.- then move the old Vim binary:
$ mv /usr/bin/vim /usr/bin/vimold
- restart your terminal and you should see
vim --version
now with+clipboard
- then move the old Vim binary:
- For Mac users, homebrew install Vim with the clipboard option. Install homebrew and then run
Sublime Text
- Another option is to use Vintageous in Sublime Text (version 3). This gives you Vim mode inside Sublime. I suggest this (or a similar setup with the Atom editor) if you aren’t a Vim master. Check out Advanced Vim if you are.
- Vintageous is great, but I suggest you change a few settings to make it better.
- Clone this repository to
~/.config/sublime-text-3/Packages/Vintageous
, or similar. Then check out the “custom” branch.- Alternatively, you can get a more updated Vintageous version by cloning the official repository and then copying over this patch.
- Change the user settings (
User/Preferences.sublime-settings
) to include:'caret_style': 'solid'
- This will make the cursor not blink, like in Vim.
- Sublime Text might freeze when you do this. It’s a bug; just restart Sublime Text after changing the file.
ctrl+r
in Vim means “redo”. But there is a handy Ctrl + R shortcut in Sublime Text that gives an “outline” of a file. I remapped it to alt+r by putting this in the User keymap{ 'keys': ['alt+r'], 'command': 'show_overlay', 'args': {'overlay': 'goto', 'text': '@'} },
- Mac users: you will not have the ability to hold down a navigation key (like holding j to go down). To fix this, run the commands specified here: https://gist.github.com/kconragan/2510186
- Clone this repository to
- Now you should be able to restart sublime and have a great Vim environment! Sweet Dude.
Other
I don’t personally use these yet, but I’ve heard other people do!
:wqa
- Write and quit all open tabs (thanks Brian Zick)
Additional resources
- Practical Vim is a fantastic resource on many of the useful hidden features of vim.