code

Vim의 검색 결과를 어떻게 나열 할 수 있습니까?

codestyles 2020. 8. 24. 08:22
반응형

Vim의 검색 결과를 어떻게 나열 할 수 있습니까?


다음을 치면 일치 항목을 나열하고 싶습니다.

/example

모든 경기가 한 번에 어디에 있는지 볼 수 있습니다.


    " put in your ~/.vimrc file
    " START search related configs and helps
    "
    " ignore case when searching
    set ignorecase

    " search as characters are entered, as you type in more characters, the search is refined
    set incsearch

    " highlight matches, in normal mode try typing * or even g* when cursor on string
    set hlsearch

    " yank those cheat commands, in normal mode type q: than p to paste in the opened cmdline
    " how-to search for a string recursively
    " :grep! "\<doLogErrorMsg\>" . -r
    "
    " how-to search recursively , omit log and git files
    " :vimgrep /srch/ `find . -type f \| grep -v .git \| grep -v .log`
    " :vimgrep /srch/ `find . -type f -name '*.pm' -o -name '*.pl'`
    "
    " how-to search for the "srch" from the current dir recursively in the shell
    " vim -c ':vimgrep /srch/ `find . -type f \| grep -v .git \| grep -v .log`'
    "
    " how-to highlight the after the search the searchable string
    " in normmal mode press g* when the cursor is on a matched string

    " how-to jump between the search matches - open the quick fix window by
    " :copen 22

    " how-to to close the quick fix window
    " :ccl

    " F5 will find the next occurrence after vimgrep
    map <F5> :cp!<CR>

    " F6 will find the previous occurrence after vimgrep
    map <F6> :cn!<CR>

    " F8 search for word under the cursor recursively , :copen , to close -> :ccl
    nnoremap <F8> :grep! "\<<cword>\>" . -r<CR>:copen 33<CR>

    " omit a dir from all searches to perform globally
    set wildignore+=**/node_modules/**

    " use perl regexes - src: http://andrewradev.com/2011/05/08/vim-regexes/
    noremap / /\v
    "
    " STOP  search related configs and helps

:g//p

더 긴 형태 :

:global/regular-expression/print

패턴 / 정규식을 생략 할 수 있으며 Vim은 이전 검색어를 재사용합니다.

하찮은 일이 : 그렙 도구는이 명령 시퀀스의 이름을 따서 명명되었다.


다음을 수행 할 수도 있습니다.

g/pattern/#

원하는 패턴과 줄 번호를 인쇄합니다.


이 목록을보고 경기 사이를 빠르게 이동하려면

:vimgrep example %

또는

:grep example %

이렇게하면 "오류 목록"이 모든 일치 항목으로 채워 :copen지므로 빠른 수정 버퍼에 모두 나열하는 데 사용할 수 있고 특정 줄에서 Enter 키를 눌러 해당 일치 항목 으로 이동 하거나 :cn같은 명령을 사용 :cp하여 앞뒤로 이동할 수 있습니다.

for a thorough explanation, see my reply to a similar question


Just learned a new one: the Location List!
Type :lvim foo % to search for foo in the current file and enter all matches containing foo into the location list.
Type :lopen to open the location list in the quickfix window, which is fully navigable as usual.
Use :lnext/:lprevious to to through the list (use tpope/unimpaired mappings for the best experience)


Another possibility is to use the include file search commands.

[I

This will list all occurrences of the word under the cursor. It may be more than you need though, because it will also search any files that are included in the current file.

But the nice thing about this command is that the search result display also shows a count of the number of matches, in addition to the line number of each match.

:help include-search

to see lots of variants.

A note about

:g//p

This can be reduced further to

:g//

because, as others have said, p(rint) is the default action.


Using :set hlsearch will highlight all the matches in yellow allowing you to scan the file easily for matches. That may not be what you want though, after searching, :g//p will give you the listed matches


To elaborate on this ... instead of

/example
:g//p

you can also write directly

:g/example/p

or, as p(rint) is the default action for the :g(lobal) command, this can be shortened to

:g/example

And instead of p(rint), other actions are possible, e.g. d(elete). See :help :global


you can get a nice quickfix window with the matches form your current search pattern

:vim // %
:copen

super handy if you previously crafted a complex search pattern using just /pattern

Edit: just found out this also works for all open buffers

:bufdo vimgrepadd // %
:copen

g/pattern

If you have :set number, the above command displays line numbers as well.

If you haven't :set number, then

g/pattern/#

will display the line numbers.


I have written a piece of code for this. It actually avoids the problems in vimgrep. It works even with unnamed files. And it is easier to use.

function! Matches(pat)
    let buffer=bufnr("") "current buffer number
    let b:lines=[]
    execute ":%g/" . a:pat . "/let b:lines+=[{'bufnr':" . 'buffer' . ", 'lnum':" . "line('.')" . ", 'text': escape(getline('.'),'\"')}]"
    call setloclist(0, [], ' ', {'items': b:lines}) 
    lopen
endfunction

When you call it with a pattern, it opens the location windows with all the matches.

This could be a command

command! -nargs=1 Mat call Matches(<f-args>)

So all you need to do is to type :Mat pattern

I also use the following mapping to get the matches of the current visual selection.

vnoremap Y "xy:call Matches(@x)<CR>

Ctrl-f to list all search result:

nmap <C-f> :vimgrep /<C-r>//g %<CR> \| !:copen <Enter>

참고URL : https://stackoverflow.com/questions/509690/how-can-you-list-the-matches-of-vims-search

반응형