我想做这样的事情
try
normal! t_
catch
normal! w
endtry
意味着它将转到下一个下划线
_
如果没有下划线,或者到下一个单词边界。您如何实际检查动议是否成功?我想比较
getpos('.')
在命令之前和之后,但我意识到这并不一定意味着该动议不成功。例如,如果光标已经在 _
之前
最佳答案
您最好的拍摄是在运动之前保持当前位置(设置标记或保存当前位置)并在此之后检查位置。
:mark a
# or
:let col = col(".")
:let line = line(".")
# motion command (say w,j,zj,f.)
# check difference
:echo col("'a") . "," . line("'a") . " = " . col('.') . "," . line('.')
# or
:echo col . "," . line . " = " . col('.') . "," . line('.')
文档:
根据您的需要,
col()
可以省略作为运动检测器。使用
getpos()
可以获得更准确的位置:getpos({expr}) Get the position for {expr}. For possible values of {expr} see |line()|. The result is a |List| with four numbers:
[bufnum, lnum, col, off]
文档
:标记一个
# 或者
:let old_pos = getpos(".")
# 运动命令(比如 w,j,zj,f.)
:let mark_a = getpos("'a")
:let current_pos = getpos(".")
# 检查差异
:echo mark_a[2] 。 “,”。标记_a[1] 。 “=”。 current_pos[2] 。 “,”。 current_pos[1]
# 或者
:echo old_pos[2] 。 “,”。 old_pos[1] 。 “=”。 current_pos[2] 。 “,”。 current_pos[1]
检查实际使用情况,如果已使用(lnum > 0),则保存旧标记,此处:
ag.vim autoload/ag.vim:215
关于vim - 你如何检查一个 vim 命令(一个 Action )是成功还是失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474055/