Tags: #vim
The following are native vim solutions to finding files and also searching multiple files for content.
They require no plugins to be installed.
Note: at the end of this gist I’ll demonstrate some plugins that remove the need to use the native solutions, and in fact I use these plugins for day-to-day vim’ing instead.
To find a single file you can use the :find
Ex command and pass it a wildcard glob character to help search recursively for the specified file pattern.
Example: we want to find a file called next.config.js
:
:find **/next.*.js
Note: we could have just done
**/next.config.js
but in case you weren’t familiar with the filename this helps to narrow things down.
You have two options for locating a string within a file (or multiple files) and that’s the following Ex commands…
:vimgrep
:lvimgrep
The difference is that lvimgrep
opens the results in a ‘location’ window and every open split window can have its own location window, while vimgrep
opens the results in a ‘quickfix’ window and there can only be one of those shown.
Meaning if you ran vimgrep
in one split window and then ran it again (e.g. you’re looking for something different now) from another window, then your first set of results would be replaced with the latter results. If you instead used lvimgrep
then you could have multiple search results displayed (one for each split window).
Example: you’re looking for every file that has the contents module.
(e.g. module.foo
, module.bar
etc):
:lvimgrep 'module\.' **/*
Note: I have found that lvimgrep has an empty location window of results which I think is caused by something else in my
.vimrc
file. If you notice a similar issue then you can debug by running vim without your vim configuration (e.g.vim -u /Users/integralist/.vimrc_basic
).
set nocompatible number cursorline expandtab hlsearch visualbell tabstop=2 shiftwidth=2
While in my day-to-day vim usage I’ll use :FZF
to find files or :Ack! '<regex>' <path>
to find files that contain a particular string.
Note: Although I use the Ack vim plugin I configure it to use the
ag
‘Silver Searcher’ shell command.
" Plugin Managment
" https://github.com/junegunn/vim-plug#example
"
" Reload .vimrc and :PlugInstall to install plugins.
" Use single quotes as requested by vim-plug.
"
" Specify a directory for plugins
call plug#begin('~/.vim/plugged')
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim' " Tab to select multiple results
Plug 'mileszs/ack.vim'
" Initialize plugin system
call plug#end()
" PLUGIN CONFIGURATION...
" FZF (search files)
"
" Shift-Tab to select multiple files
"
" Ctrl-t = tab
" Ctrl-x = split
" Ctrl-v = vertical
"
" We can also set FZF_DEFAULT_COMMAND in ~/.bashrc
" Also we can use --ignore-dir multiple times
"
" Note use :map command to see current mappings (also :vmap, :nmap, :omap).
" Can also restrict to specific mapping `:map <Leader>w`
" https://vi.stackexchange.com/questions/7722/how-to-debug-a-mapping
map <leader>f :FZF<CR>
map <leader>b :Buffers<CR>
set wildignore+=*/.git/*,*/node_modules/*,*/.hg/*,*/.svn/*.,*/.DS_Store " Files matched are ignored when expanding wildcards
set wildmode=list:longest,list:full
" Ack
let g:ackprg = 'ag --vimgrep --smart-case --ignore-dir=node_modules'