In vim, it's a common task to search a word in current foler and all its subfolders. There are some plugins for this scenario:
ack plugin
Add the following lines into vimrc:
if executable('ag')
let g:ackprg = 'ag --vimgrep'
endif
cabbrev ack Ack
exec 'cabbrev fw Ack -w <C-r><C-w> **/*.' . expand("%:e")
Plug 'mileszs/ack.vim'
Here <C-r><C-w>
represents word at cursor.
The default behavior of ack
is searching recursively in all files.
So when we only want to search in specific file types,
we have to specify it explicitly with **/*.hs
, **/*.py
, etc.
"%:e" means the extension of the current file.
cabbrev
doesn't accpet variables as arguments.
So we concat the command string with .
, then run it with exec
.
fw
means find word
, as the option -w
specify.
Other useful options of Ack
includes:
-i
(ignore case), -c
(only count the occurances instead of mateched texts),
-h
(print help message of Ack
).
Now you can search the word at cursor in all files with the same file extension
of current file recursively through :fw<CR>
.
The search results is listed in a quickfix window.
Move cursor with j/k/g/G
,
select with <Enter>/o
(open the target file in current window),
O
(like o
but close the quickfix window),
t
(open the file in a new tab),
T
(like t
but keep focus in quickfix window),
go
(like o
but keep focus in quickfix window,
v
(open file in a vertical split window),
gv
(like v
but keep focus in quickfix window).
See all keyboard shortcuts with :h ack
.
Close quickfix window with q
when the cursor in quickfix window,
with ccl
when the cursor in other buffers.
In the editor window, use :cc3
to junp to #3 result.
:cn
to jump to the next result, :cp
to previous result.
See :h quickfix
for details.
Note: Ack
opens a quickfix window, while LAck
opens a location window.
The former is only one per vim session, while the latter is one for each window.
Ref: QuickFix and Location list in vim
easygrep plugin
Add Plugin 'dkprice/vim-easygrep'
into ~/.vimrc.
Search word 'vari' with :Grep vari
.
Install ack-grep
with aptitude.
And use :GrepProgram
to change search utility to ack-grep
.
Use :cw
/:ccl
to open/close search result window;
:cn
/:cp
to jump to next/previous result;
ag.vim plugin (Deprecated)
Install the_silver_searcher
with sudo aptitude install silversearcher-ag
.
Install ag.vim with adding codes below into ~/.vimrc:
let g:ag_highlight=1
Plugin 'rking/ag.vim'
Here "g:ag_highlight=1" make the preview function useful with go
shortcut
in quickfix window.
Search 'pattern" with :Ag [options] pattern [path]
.
"path" is optional with the default value :pwd
.
Search the word under cursor with :Ag
.
See :h Ag
for all commands Ag provides.
Frequently used options:
"-i": ignore case: :Ag -i pattern
.
"-w": match whole word: :Ag -w pattern
.
"-Q": match pattern literally, do not use regular expression.
In the quickfix window, use "o" to jump to the target file, "e" to open file and close the quickfix window, "t" to open file in a new tab, "v/h" to open target file in vertial/horizontal split window, "q" to close the quickfix window, etc.
In the editor window, use ":ccl/:cope" to close/open quickfix window, ":cn/:cp" to move to next/previous target (even when quickfix window is closed). ":cfir/:cla" to jump to first/last target.
Search "keyboard shortcuts" in :h Ag
to see the details.
It's much faster than "ack-grep" below.
Other Options
:vim /ord/ *.hs
You will auto jump to the first matched file.
If you don't want this auto-jump, use :vim /ord/j *.hs
.
Now you can use ":cn" or ":cp" to traverse between all matched files,
or use ":cw" to open search result window (use ":clo" to hide this window).
:lv ord *.hs
There is no search result window (quickfix window) for this "lv" command. And you can't use ":cn" and ":cp" also.
:grep ord *.hs
You will see a grep result in console.
After pressing
Ref:
http://vim.wikia.com/wiki/Find_in_files_within_Vim
google "vim quickfix"
:h :vim, :h :grep, :h lv