Vim에서 역검색을 수행하는 방법은 무엇입니까? (즉, 패턴을 포함하지 않는 행을 가져옵니다)

Vim에서 역검색을 수행하는 방법은 무엇입니까? (즉, 패턴을 포함하지 않는 행을 가져옵니다)

grepI를 사용하면 grep -v "my search""내 검색" 없이 모든 행을 얻을 수 있습니다.

와 함께sedsed '/baz/!s/foo/bar/g'"baz"가 없는 줄에서 대체 텍스트를 찾을 수 있습니다.

Vim에서도 동일한 작업을 수행할 수 있는 방법이 있나요? "s///" 구문 없이 "/" 검색 구문만 사용하여 검색하는 것이 가능합니까?

답변1

:g/pattern/

패턴이 발견된 모든 라인과 일치합니다.

:v/pattern/

그 반대입니다. 자세한 내용은 을 참조하세요 :h global.

다음과 같이 사용할 수 있습니다.

:v/pattern/norm Ipattern not found - <CR>

"패턴을 찾을 수 없음 -"을 "패턴"이 없는 모든 줄 앞에 추가하거나

:v/pattern/s/nrettap/pattern

"pattern"이 없는 모든 줄에서 "nnrettap"을 "pattern"으로 바꾸려면

인위적인 예입니다. 그렇습니다.

답변2

라인을 검색하려면~ 아니다예를 들어, foo를 포함하면 다음을 수행합니다.

/^\(\(.*foo.*\)\@!.\)*$

원천:http://vim.wikia.com/wiki/Search_for_lines_not_ Contains_pattern_and_other_helpful_searches

답변3

:v commandEdit 사용하기 패턴과 일치하지 않는 행을 찾는 전통적인 접근 방식은 다음을 사용하는 것입니다.:v 명령:

:v/Warning/p

실제 검색을 시작하기 전에 관련 없는 줄을 최대한 많이 필터링하려는 대규모 로그 파일 작업 시 깔끔한 방법은 파일을 임시 이름으로 저장하고 일치하지 않는 줄을 모두 삭제하는 것입니다.

:sav junk.log
:v/warning/d

이제 "경고"와 일치하지 않는 모든 줄이 제거된 원본 파일의 복제본을 편집하고 있으며 원하는 대로 편집할 수 있습니다.

참조:https://vim.fandom.com/wiki/Search_for_lines_not_ Contains_pattern_and_other_helpful_searches

답변4

또 다른 접근 방식은 "외부 grep"을 수행하는 것입니다. 설명서에서 (:help :grep):

5.2 External grep

Vim can interface with "grep" and grep-like programs (such as the GNU
id-utils) in a similar way to its compiler integration (see |:make| above).

                                *:gr* *:grep*
:gr[ep][!] [arguments]  Just like ":make", but use 'grepprg' instead of
            'makeprg' and 'grepformat' instead of 'errorformat'.
            When 'grepprg' is "internal" this works like
            |:vimgrep|.  Note that the pattern needs to be
            enclosed in separator characters then.
            If the encoding of the program output differs from the
            'encoding' option, you can use the 'makeencoding'
            option to specify the encoding.

                                *:lgr* *:lgrep*
:lgr[ep][!] [arguments] Same as ":grep", except the location list for the
            current window is used instead of the quickfix list.

나에게는 가 external 을 사용하는 grepprg기본값( 참조)으로 설정되어 있으므로 다음을 수행할 수 있습니다.:set grepprggrep

:grep -v 'my_pattern' %

여기서 는 %현재 파일의 이름을 나타냅니다( 참조 :help :_%).

그런 다음 :copen또는 를 사용하여 :lopen빠른 수정 또는 위치 목록에서 결과를 조회할 수 있습니다( 각각 :grep또는 :lgrep사용 여부에 따라 다름).

메모

외부 grep은 항상 작동합니다.저장됨파일이며 버퍼에는 없습니다. 일관성 없는 결과를 얻을 때 이 점을 염두에 두십시오.

관련 정보