Vim で逆検索を行うにはどうすればいいですか? (つまり、パターンを含まない行を取得する)

Vim で逆検索を行うにはどうすればいいですか? (つまり、パターンを含まない行を取得する)

を使用すると、「my search」なしですべての行を取得grepできます。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」のないすべての行で「nrettap」を「pattern」に置き換えます。

確かに、不自然な例ですね。

答え2

行を検索するにはないたとえば、foo を含む場合は次のようにします。

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

ソース:http://vim.wikia.com/wiki/Search_for_lines_not_containing_pattern_and_other_helpful_searches

答え3

:vコマンドの使用編集パターンに一致しない行を見つけるための従来の方法は、:v コマンド:

:v/Warning/p

実際の検索を始める前に、できるだけ多くの無関係な行をフィルター処理したい大きなログ ファイルを扱う場合の便利な方法は、ファイルを一時的な名前で保存し、一致しない行をすべて削除することです。

:sav junk.log
:v/warning/d

これで、元のファイルのクローンを編集することになりますが、"warning" に一致しない行はすべて削除され、自由に編集できます。

参照:https://vim.fandom.com/wiki/Search_for_lines_not_containing_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.

私の場合、grepprgはデフォルトに設定されており( を参照:set grepprg)、外部 を使用しているのでgrep、次のようにすることができます。

:grep -v 'my_pattern' %

ここで、 は%現在のファイルの名前を指します ( を参照:help :_%)。

この後、:copenまたは を使用して、クイックフィックス リストまたは場所リスト (それぞれまたはが使用されている:lopenかどうかに応じて) で結果を検索できます。:grep:lgrep

注記

外部grepは常に保存されたファイルではなくバッファに保存されます。一貫性のない結果になる場合は、この点に注意してください。

関連情報