在VIM中搜索文件夹

现在主流的编辑器都有搜索文件夹的功能,VIM同样也能实现。

vimgrep是VIM寻找模式匹配的内部方法,可以自动识别换行符和编码,并且
可以使用VIM强大的正则表达式。缺点是相对慢些,因为所有文件都要读
入内存。

语法
:vim[grep][!] /{pattern}/[g][j] {file} …
            Search for {pattern} in the files {file} … and set
            the error list to the matches.
            Without the ‘g’ flag each line is added only once.
            With ‘g’ every match is added.

            {pattern} is a Vim search pattern.  Instead of
            enclosing it in / any non-ID character (see
            |’isident’|) can be used, so long as it does not
            appear in {pattern}.
            ‘ignorecase’ applies.  To overrule it put |/c| in the
            pattern to ignore case or |/C| to match case.
            ‘smartcase’ is not used.

            When a number is put before the command this is used
            as the maximum number of matches to find.  Use
            ":1vimgrep pattern file" to find only the first.
            Useful if you only want to check if there is a match
            and quit quickly when it’s found.

            Without the ‘j’ flag Vim jumps to the first match.
            With ‘j’ only the quickfix list is updated.
            With the [!] any changes in the current buffer are
            abandoned.

来个简单的例子
在 d:mydocs下查找含有“弹冠相庆”的txt文档,但不跳到第一个匹配
:vimgrep /弹冠相庆/gj d:/mydocs/*/*.txt
如果要包含子文件夹,则用

:vimgrep /弹冠相庆/gj d:/mydocs/**/*.txt
打开quickfix窗口查看匹配结果
:cw

参考资料:

http://vimcdoc.sourceforge.net/doc/quickfix.html

:h vimgrep

My _vimrc

" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible

" sytle
colo torte

" set encoding
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1

" file format
set fileformats=dos,unix,mac

" allow backspacing over everything in insert mode
set backspace=indent,eol,start

" keep a backup file
set backup

" keep 100 lines of command line history
set history=100

" show the cursor position all the time
set ruler

" display incomplete commands
set showcmd       

" do incremental searching
set incsearch       

" In many terminal emulators the mouse works just fine, thus enable it.
if has(‘mouse’)
  set mouse=a
endif

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

" backspace in Visual mode deletes selection
vnoremap <BS> d

" CTRL-X and SHIFT-Del are Cut
"vnoremap <C-X> "+x
vnoremap <S-Del> "+x

" CTRL-C and CTRL-Insert are Copy
"vnoremap <C-C> "+y
vnoremap <C-Insert> "+y

" CTRL-V and SHIFT-Insert are Paste
"map <C-V>        "+gP
map <S-Insert>        "+gP

" set guioptions
"set guioptions-=a

" CTRL-TAB is Next tab
noremap <C-Tab> gt
vnoremap <C-Tab> <esc>gt
inoremap <C-Tab> <esc>gt

" Shift_TAB is Previous tab
noremap <S-Tab> gT
vnoremap <S-Tab> <esc>gT
inoremap <S-Tab> <esc>gT

" Use CTRL-S for saving, also in Insert mode
noremap <C-S> :update<CR>
vnoremap <C-S> <C-C>:update<CR>
inoremap <C-S> <C-O>:update<CR>

" Don’t use ALT keys for menus.
set winaltkeys=no

" tab mapping
map <M-1> 1gt
map <M-2> 2gt
map <M-3> 3gt
map <M-4> 4gt
map <M-5> 5gt
map <M-6> 6gt
map <M-7> 7gt
map <M-8> 8gt
map <M-9> 9gt
map <M-t> :tabnew<CR>
map <M-w> :bd<CR>
map! <M-1> <esc>1gt
map! <M-2> <esc>2gt
map! <M-3> <esc>3gt
map! <M-4> <esc>4gt
map! <M-5> <esc>5gt
map! <M-6> <esc>6gt
map! <M-7> <esc>7gt
map! <M-8> <esc>8gt
map! <M-9> <esc>9gt
map! <M-t> <esc>:tabnew<CR>
map! <M-w> <esc>:bd<CR>

" display the line number
set number

" smartcase for searching
set ignorecase smartcase

" tab size
set tabstop=2

" 2 space when auto-indent
set shiftwidth=2

" replace tab with space
set expandtab

" linebreak
set linebreak

" linebreak module for asian language
set fo+=mB

" cursor keys wrap to previous/next line
set whichwrap=h,l,~,b,s,<,>,[,]

" auto indent
set autoindent

" When a bracket is inserted, briefly jump to the matching one
set showmatch

" set up statusline
set laststatus=2
set statusline=%<%f %h%m%r%=%k[%{(&fenc=="")?&enc:&fenc}%{(&bomb?",BOM":"")}] [%{&fileformat}] %-14.(%l,%c%V%) %P

"if (has("gui_running"))
" set nowrap
" set guioptions+=b
" colo torte
"else
" set wrap
" colo ron
"endif

" full screen
"au GUIEnter * simalt ~x

" set editor size
let g:editorLines=30
let g:editorColumns=125

" set editor size
function! SetScreenSize()
  let &lines=g:editorLines
  let &columns=g:editorColumns
endfunction

call SetScreenSize()

" get editor size
function! GetScreenSize()
  let g:editorLines=&lines
  let g:editorColumns=&columns
endfunction

" full screen
let g:fullScreened = 0
function! Fullscreen()
  if g:fullScreened == 0
    call GetScreenSize()
    let g:fullScreened = 1
    simalt ~x
    set go-=m
    set go-=T
    set go-=r
    set go-=b
  else
    let g:fullScreened = 0
    call SetScreenSize()
    set go+=m
    set go+=T
    set go+=r
    set go+=b
  endif
endfunction

map <F11> :call Fullscreen()<CR>

function! SaveSession()
  "set sessionoptions-=curdir
  "set sessionoptions+=sesdir
  mksession! $VIM/session.vim
  wviminfo! $VIM/session.viminfo
endfunction

map <F5> :call SaveSession()<CR>

function! LoadSession()
  source $VIM/session.vim
  rviminfo $VIM/session.viminfo
endfunction

map <F6> :call LoadSession()<CR>

set magic

" don’t highlight search result
map <F2> :noh<CR>

" for bufexplorer
map <F3> be
map! <F3> <esc>be

" share clipborad with windows
set clipboard+=unnamed

"When _vimrc is edited, reload it
autocmd! bufwritepost _vimrc source $VIM/_vimrc