sponsor Vim development Vim logo Vim Book Ad

trag : A language-aware source code scanner (with support for git grep, ack, ag, sift)

 script karma  Rating 7/3, Downloaded by 5664  Comments, bugs, improvements  Vim wiki

created by
Tom Link
script type
This plugin uses ad-hoc searches to find strings in files. For certain
languages, it can also find variable/function/class definitions,
function calls etc.

Other than |tags| or |cscope|, it doesn't build a database to speed up
searches but always scans all files. It can make use of the following
external tools in order to gain acceptable performance for medium-sized

    - `git grep`
    - `ack` http://beyondgrep.com/
    - `ag` https://github.com/ggreer/the_silver_searcher
    - `sift` https://sift-tool.org/

The builtin vimscript-based version file scanner and also
|vimgrep| are suitable for small projects. See |g:trag#grep_type| for
available options.


First, define which files belong to your project. See
|g:trag#file_sources| for available sources. If the variable contains
"vcs" and the current buffer is under control of a supported VCS, trag
will scan the files in the VCS. Maybe your project's source files are
already registered in your tags files, in which case those will be used.

Secondly, use |:Trag| to scan your project's files. You can restrict searches
to certain "kinds" like only variable definitions or only function calls. See
|trag-kinds| for details.

You can also type <Leader>r# to search for the word under cursor (see
|g:trag_map_leader| and |TragInstallMap()| for details on maps). In
supported filetypes, <Leader>rd will search for the definition of the
word under cursor.

Currently the following filetypes are supported:

    - java
    - javascript
    - json
    - make
    - r
    - ruby
    - viki
    - vim

Run `:echo globpath(&rtp, 'ftplugin/*/trag.vim')` to get a full listing
of supported filetypes.

NOTE: Some kinds are available only for a subset of known filetypes.

Default maps:

    <Leader>r# ... Trag the word under cursor
    <Leader>r. ... :Trag command-line
    <Leader>r+ ... Show quickfixlist via |:Tragcw|
    <Leader>r* ... Edit a file from the files list, i.e. the list that would be
                   used if no "--file_sources" argument were passed to
    <Leader>rx ... The same as `:Trag -l -i=x -x=i <cword><cr>`
                   where x is a alphabetical letter that describes a kind in
                   |g:trag_kinds| and <cword> is the word under cursor.
                   "-x=i" (exclude commented text) is only added if kind x is
                   in |g:trag_kinds_ignored_comments|.
install details
To install the vimball, edit the vba file and type: >

    :so %

See :help vimball for details.

To install from github, please use
    1. git + a plugin loader like enable_vim, pathogen, vim-unbundle etc.
    2. a plugin manager like VAM, Vundle, NeoBundle, vim-plug etc.

The tlib_vim plugin is required:

Also available via git: http://github.com/tomtom/trag_vim

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
trag.vba 2.00 2015-11-09 7.0 Tom Link - Require tlib 1.14; use tlib#file#Edit()
- Move ftplugin/* -> autoload/trag/ft/*
- trag#ScanWithGrepDefs() etc.
- a:leader .'+' map -> :Tragcw
- trag#ScanWithGrepDefs(): Return values(qfl)
- Misc fixes
- GrepWith_external(): strip text
- trag#utils#GrepaddFiles(): Use shellescape()
- Support for sift (https://sift-tool.org)>- Support for markdown
- Move qfl/loclist browser to tlib; require tlib 1.15
- trag#BrowseList: deepcopy g:trag#world
- trag#BrowseList: Use copy()
- Breaking change: Use tlib#arg#GetOpts
- :Tragsearch --file_sources command-line option
- trag#Grep: Use return
- s:GrepWith_external: Don't check if executable for grep_type == vcs
- s:GrepWith_external(): Don't use deepcopy
- s:GetRx(): don't precalc fnamemodify()
- Removed :Tragfile, :Traggrep, :Tragsetfiles, :Tragaddfiles, :Tragclearfiles, :TragGitFiles, :TragRepoFiles
- g:trag#file_sources: support for function sources; various improvements
- Re-introduce :Tragfiles command
- Deprecate trag#Grep()
- FIX maps
- Remove Tragsearch command; s/TLibTrace/Tlibtrace/g
MD5 checksum: 099e3abc4020163be874d233f90b59f1
trag.vba 1.02 2014-11-02 7.0 Tom Link - trag#Edit(): Don't set initial filter when the source of the files listing was a VCS
- trag#InitListBuffer: Use set_syntax
- trag#GetFilename() instead of s:GetFilename()
- FormatBase(): Allow customization via format_item
MD5 checksum: 738c7a98299f22507000ab21a53d7b2f
trag.vba 1.01 2014-07-07 7.0 Tom Link - trag#utils#GrepaddFiles(): Use shellescape(..., 1)
- g:trag#debug: Hide warnings by default
- Make sure not to run git grep on files outside of the repo (use trag instead; require tlib 0.112)
- Minor improvements; g:trag#grep_fallback_type
- GrepWith_external(): bufnr optimization
- GrepWith_external(): normalize filenames
- ag: Don't use -Q for "identity"
MD5 checksum: 3c5f2c7653a0345612891fe614655997
trag.vba 1.00 2014-07-05 7.0 Tom Link - addon-info
- <c-l> Edit line
- Help template
- trag#RunCmdOnSelected(): Optionally use w.GetBufferLines() if provided (see vikitasks #104b45b)
- trag#RunCmdOnSelected(): Preliminary support for w.AfterRunCmd()
- trag#Grep(): Takes filetype as second optional argument
- g:trag#grep_type replaces g:trag_search_mode
- g:trag_map_leader, TragInstallMap(), TragInstallKindMap(): Make it easier to set up maps
- trag#GetGitFiles(): Use g:tlib#dir#sep, not the obsolete g:tlib_filename_sep
- Rename most commands matching TRag* -> Trag*
- trag#SetRepoFiles(): Use files in current VCS repo
- trag#Grep(): Refactor to enable use of external grep commands
- s:GetRx(): Return [posrx, filetype]
- First draft for use of external grep
- vim: Use :grepadd
- Further improvements to allow use of external grep commands
- Initial support for VCS (e.g. git grep)
- g:trag#file_sources: Define source for file lists
- trag#external#vcs#Run(): Use fnameescape()
- Fixed some regexps
- Rename s:trag_filenames to g:trag_extension_filetype
- TRagDefKind: accepts an optional "!"
- g:trag#file_sources can be buffer-local (b:trag_file_sources)
- Remove references to trag#Process_*
- trag#GetGitFiles(): Use \%()
- trag#external#grep#IsSupported(): Minor simplification
- trag#external#vcs#Run(): Minor optimization
- trag#DefFiletype()
- trag#external#vcs#Run(): Use
- Move filetype definitions to ftplugin/*/trag.vim
- json support
- FIX javascript regexp
- Use viki for *.TXT
- Draft for "hg grep" support (currently disabled due to poor performance)
- rx.vim: Convert regexps
- trag#utils#GrepaddFiles(): Process files in batches
- Adapt vcs to previous changes
- Improved support for grep
- Support for ack
- b:trag_grep_type: Buffer-local g:trag#grep_type
- GrepWith_grep, GrepWith_ack
- g:trag#file_sources: tags support
- Rename g:trag#external#ack#ack_params to g:trag#external#ack#opts
- Support for ag (the_silver_searcher)
- FIX javascript l regexp
- g:trag_kinds_ignored_comments: A list of kinds for which |TragInstallKindMap()| will install maps that ignore comments
- New kind: "u" (uses of word; ignore comments)
- trag#utils#GrepaddFiles(): Escape "()" in filenames
- trag#rx#ConvertRx_perl(): Handle \{-}, \+, \?
- trag#utils#GrepaddFiles(): Use shellescape()
- Support for Makefiles
MD5 checksum: c87df75189cfb7574a10ba093bf60a0a
trag.vba 0.11 2012-09-28 7.0 Tom Link - trag#LocList(): Take world & suspended as arguments (like #BrowseList())
- Be more clever at guessing the filetype (allow filenames as patterns)
- No direct external access to trag_filenames
- g:trag#check_vcs: If the file is under a VCS, scan through all files in the VCS (requires tlib 0.44)
- trag#AgentWithSelected(): cmd as optional argument
- s:DoAutoCmd(): call event only if one is defined
- Show item in balloon (if available)
- trag#RunCmdOnSelected(): Optional argument: Don't close scratch
MD5 checksum: e4bc34f9c3ef3ce134f2c41918d0922d
trag.vba 0.09 2012-01-21 7.0 Tom Link - Don't hide the window if all error numbers are -1
- .gitignore
- trag#Grep(): trigger QuickFixCmdPre & QuickFixCmdPost events for "trag"
- trag#LocList() used qfl
MD5 checksum: 255a58b855b4e21462b517826c88fd91
trag.vba 0.8 2010-03-31 7.0 Tom Link - Moved the definition of some variables from plugin/trag.vim to autoload/trag.vim
- :TRagcw! (show :cw even if there are no recognized errors)
- Require tlib 0.37
trag.vba.gz 0.7 2009-12-21 7.0 Tom Link - trag#QuickList(): Accept a dict as optional argument.
- trag#Grep(): rx defaults to '\.{-}'
- trag#Grep(): use :g (instead of search()) for non-vimgrep mode
trag.vba.gz 0.6 2009-10-18 7.0 Tom Link - trag#viki#Rename()
- Generalized trag#rename#Rename()
- Enabled "trace cursor" functionality (mapped to the <c-insert> key).
- :Traglw
- TRagGitFiles, trag#SetGitFiles(), g:trag_git
trag.vba.gz 0.5 2009-02-25 7.0 Tom Link - Update the qfl when running a command on selected lines
- Enable certain operations for multiple choices
- Java, Ruby: x ... find subclasses (extends/implements)
- Experimental rename command for refactoring (general, java)
- NEW: [bg]:trag_get_files_{&filetype}
- Traggrep: If the second argument (glob pattern) is missing, the
default file list will be used.
trag.vba.gz 0.4 2008-11-22 7.0 Tom Link - trag_proj* variables were renamed to trag_project*.
- Traggrep: Arguments have changed for conformity with grep commands (an implicit .-argument is prepended)
- Make sure tlib is loaded even if it is installed in a different rtp-directory.
- Post-process lines (strip whitespace) collected by vimgrep
- tlib#Edit(): for list input, set pick_last_item=0, show_empty=1
- Aliases for some commands: Trag, Traggrep ...
trag.vba.gz 0.3 2008-02-26 7.0 Tom Link - Use vimgrep with set ei=all as default search mode (can be configured via g:trag_search_mode); by default trag now is a wrapper around vimgrep that does the handling of project-related file-sets and regexp builing for you.
- FIX: ruby/f regexp
trag.vba.gz 0.2 2008-02-19 7.0 Tom Link - Quite a few things have changed and I haven't had the time yet to test
these changes thorougly. There is a chance that nested patterns thus
don't work as described (please report).
- Enable search for more than one kinds at once (as comma-separated
- Enabled <c-e>: Run ex-command on selected lines (e.g. for refactoring
- Enabled <c-s>, <c-v>, <c-t>: Open selected lines in (vertically) split
windows or tabs.
- Renamed vV kinds to lL (~ let)
- New kind: r/R (right hand side argument of an assignment/let, i.e.
- New kind: fuzzy (typo-tolerant search)
- INCOMPATIBLE CHANGE: Renamed "mode" to "kind"
- TRag now has some idea of negation. E.g., "TRag !i,w call" will search
for the word "call" but ignore matches in comments (if defined for the
    current filetype)
- Alternative methods to define project files: g:trag_files,
g:trag_glob, g:trag_proj.
- Improved support for ruby, vim
- TRagKeyword, trag#CWord(): Customize keyword rx.
- g:trag_get_files
- [bg]:trag_proj_{&filetype}: Name of the filetype-specific project
files catalog (overrides [bg]:trag_proj if defined)
- trag#Edit() will now initally select files with the same "basename
root" (^\w\+) as the current buffer (the command is thus slightly more
useful and can be used as an ad-hoc alternative file switcher)
- FIX: Match a line only once
- FIX: Caching of regexps
trag.vba.gz 0.1 2007-09-30 7.0 Tom Link Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Vim at Github