
我grep
可以做 agrep -v "my search"
來獲取所有行,而無需“我的搜索”。
和sed
我可以sed '/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”的每一行上用“pattern”替換“nrettap”。
人為的例子,是的。
答案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
現在您正在編輯原始文件的克隆,刪除了所有不符合“警告”的行,您可以隨意編輯它。
參考:https://vim.fandom.com/wiki/Search_for_lines_not_containing_pattern_and_other_helpful_searches
答案4
另一種方法是執行“external 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
使用 external 的預設值(請參閱 with )grep
,所以我可以這樣做
:grep -v 'my_pattern' %
其中%
指的是目前文件的名稱(請參閱:help :_%
)。
此後,可以使用:copen
或:lopen
在快速修復或位置清單中找到結果(分別取決於是否使用:grep
或)。:lgrep
筆記
外部 grep 將始終在已儲存文件,而不是緩衝區。當得到不一致的結果時請記住這一點。