Some of the issues learned here are addressed in the following projects:
Demonstrates how to use Vim effectively in a C++ UE4 project, including:
- Using Vim quickfix for build errors
- Searching Game and UE4 source code
- Builging a ctags database to improve code navigation
- Code completion
This guide is written for developers working on Windows 10, but much of it applies to other environments as well.
Navigating source code includes:
- Browsing directories
- Opening by filename or pattern
- Searching file contents
- Jumping to tags
Documentation: netrw
:e source/code/directory
Documentation: edit-a-file
:e source/code/directory/file.cpp
Documentation: file-searching
To recursively search for files matching a pattern from the current directory:
:e **/f*.cpp
If there are more than one result, the first one will be used. To pick an option from a list instead using the wildmenu
:
:e **/f*.cpp<TAB>
Documentation: quickfix
Visual Studio has Find in Files to search code in projects and directories.
To search source code in vim, either
:grep
or
:vimgrep
is used, which
opens the same quickfix
window that can be used to jump through the results list. :grep
runs the
program set in the
'grepprg' option.
:vimgrep
, is Vim's built in search tool, but is much slower since every file
searched is subject to Vim processing that happens on every file loaded into
Vim.
These examples show you how Vim code searching by default.
:grep SearchTerm *
:vimgrep SearchTerm *
:grep /s SearchTerm *
:vimgrep SearchTerm */**
Code Search Shootout tests
recursive code searches with some popular grep alternatives. I use
ripgrep
because it is the fastest
code search tool that is actively maintained.
To configure vim to use ripgrep
, install ripgrep
, make sure rg
is in your
PATH
, and then add this to .vimrc:
if executable('rg')
set grepprg=rg\ -Hn\ --trim
endif
Here are some suggested key mappings key mappings.
noremap <F3> :cprev<CR>
noremap <F4> :cnext<CR>
noremap <S-F4> :grep <cword><CR>
noremap <F5> :colder<CR>
noremap <F6> :cnewer<CR>
These mappings work with all quickfix windows (search results and build results).
- F3: previous entry
- F4: next entry
- Shift+F4: grep for word under cursor
- F5: go to a previous error/search result list
- F6: go to a newer error/search result list
Documentation:
-
Install Universal Ctags.
-
Build a tags database with:
cd my/source/directory ctags -R .
-
Lookup tags
- :tag SomeSymbol - goto tag
- CTRL-] - goto tag under cursor
- :tselect SomeSymbol - open list of matching tags
Documentation: quickfix
Documentation:
You can also use the tags database for code completion with [i_CTRL-X_CTRL-]](https://vimhelp.org/insert.txt.html#compl-tag).
Snippet plugins like UltiSnips can be used to automate repetative typing tasks, like calling UE_LOG or adding a UPROPERTY. There are several UE4 snippet libraries, including one I maintain called vim-ue4.
To use vim-ue4, install Ultisnips and vim-ue4 like any vim package, and then in insert mode, press tab after the snippet name to expand the snippet.
I also add the following to my .vimrc so that UltiSnips uses Tab and Shift+Tab to move throught the snippet fields:
let g:UltiSnipsExpandTrigger = "<Tab>"
let g:UltiSnipsJumpBackwardTrigger = "<S-Tab>"
let g:UltiSnipsJumpForwardTrigger = "<Tab>"
let g:UltiSnipsListSnippets = "<Leader>l"