如何在 vimrc 中编写 if 语句来检查后续字符?
how to write an if statement in vimrc for checking subsequent character?
我想检查下一个字符是否是 }
,然后做一些事情。
在伪代码中:
if true
do <Esc>la
初始情况:
{some code|}
^(cursor is before the '}')
按}
结果:
{some code}|
^(cursor is after the '}')
在插入模式下,下面的代码片段应该会给出光标后的字符:
getline(".")[col(".")-1]
在有条件的情况下,它看起来像这样:
if getline(".")[col(".")-1] == "}"
" do something
else
" do something else
endif
由于您可能正在尝试编写自定义映射,因此最好在“表达式映射”中使用函数:
function! JumpOver(char)
if getline(".")[col(".")-1] == a:char
return "\<Right>"
else
return a:char
endif
endfunction
inoremap <expr> } JumpOver('}')
参考:
:help getline()
:help col()
:help :function
:help <expr>
这与 romainl 的解决方案非常相似,将 } 转换为 → 以跳过已经存在的右大括号:
:imap <expr> } nr2char(screenchar(screenrow(),screencol()))=="}" ? "<Right>" : "}"
我想检查下一个字符是否是 }
,然后做一些事情。
在伪代码中:
if true
do <Esc>la
初始情况:
{some code|} ^(cursor is before the '}')
按
}
结果:
{some code}| ^(cursor is after the '}')
在插入模式下,下面的代码片段应该会给出光标后的字符:
getline(".")[col(".")-1]
在有条件的情况下,它看起来像这样:
if getline(".")[col(".")-1] == "}"
" do something
else
" do something else
endif
由于您可能正在尝试编写自定义映射,因此最好在“表达式映射”中使用函数:
function! JumpOver(char)
if getline(".")[col(".")-1] == a:char
return "\<Right>"
else
return a:char
endif
endfunction
inoremap <expr> } JumpOver('}')
参考:
:help getline()
:help col()
:help :function
:help <expr>
这与 romainl 的解决方案非常相似,将 } 转换为 → 以跳过已经存在的右大括号:
:imap <expr> } nr2char(screenchar(screenrow(),screencol()))=="}" ? "<Right>" : "}"