我想在搜索完成后更改输出消息。
pe 当我使用很长的正则表达式进行搜索并且没有找到任何内容时 vim 返回消息E486: Pattern not found: .... very long regex code ...
我想捕获输出并更改此消息。我怎样才能做到这一点?
该:s[ubstitute]
命令具有/e
抑制错误的标志,但 AFAIK 甚至:silent /foo
不会抑制该错误消息。好吧,你不想压制它,无论如何,你想“捕获”它并展示其他东西。
像每种语言一样,vimscript 也有自己的try/catch
. 您可以在页面下方阅读有关它的内容:h :try
。
try
/foo
catch /^Vim\%((\a\+)\)\=:E486/
let @n = v:exception
echo "No luck!"
endtry
您可以将您包装try/catch
在一个函数noremap
中/
我认为您正在谈论在 vimscript 中捕获错误消息。
然后你可能想签出catch
::h catch
*:cat* *:catch* *E603* *E604* *E605*
:cat[ch] /{pattern}/ The following commands until the next |:catch|,
|:finally|, or |:endtry| that belongs to the same
|:try| as the ":catch" are executed when an exception
matching {pattern} is being thrown and has not yet
been caught by a previous ":catch". Otherwise, these
commands are skipped.
When {pattern} is omitted all errors are caught.
Examples: >
:catch /^Vim:Interrupt$/ " catch interrupts (CTRL-C)
:catch /^Vim\%((\a\+)\)\=:E/ " catch all Vim errors
:catch /^Vim\%((\a\+)\)\=:/ " catch errors and interrupts
:catch /^Vim(write):/ " catch all errors in :write
:catch /^Vim\%((\a\+)\)\=:E123/ " catch error E123
:catch /my-exception/ " catch user exception
:catch /.*/ " catch everything
:catch " same as /.*/
在try
块中进行搜索,然后您可以捕获错误消息,然后做任何您想做的事情。