vimscript在搜索模式之前插入字符串

时间:2021-09-16 20:07:47

Im currently write a coffeescript refactore plugin.

我目前正在写一个coffeescript重构插件。

Currently i'am experimenting an issue, when i try to add parameter to a function.

目前我正在试验一个问题,当我尝试向函数添加参数时。

so far in can do

到目前为止可以做到

#my code
->
# after call vim function
(new_param) ->

but when now i want insert it just before the arrow like

但是现在我想在箭头之前插入它

#my code
foo ->
# what i want
foo (new_param) ->

i have this unwanted result

我有这个不想要的结果

(new_param) foo ->

What i use in vim script is

我在vim脚本中使用的是

exec "?->"
execute "normal i(new_param) \<Esc>"

What should i do to insert just before the arrow. I read about searchpos() but i don't find good example to understand how it work.

我该怎么做才能插入箭头之前。我读了关于searchpos()但我没有找到好的例子来理解它是如何工作的。

Know the cursor position is important i guess for handel corner case like

知道光标位置很重要我猜对于汉德尔角的情况就好了

(old_param) ->
#add parameter to existing parameter
(old_param, new_param) ->

1 个解决方案

#1


0  

this command :

这个命令:

%s/\s\?[-=]>/ (new_param)&/g

will transform:

会改变:

foo ->
foo =>

into:

成:

foo (new_param) ->
foo (new_param) =>

for the adding new_param case:

添加new_param案例:

you could do :

你可以这样做:

%s/)\s\?[-=]>/, new_param&/g

#1


0  

this command :

这个命令:

%s/\s\?[-=]>/ (new_param)&/g

will transform:

会改变:

foo ->
foo =>

into:

成:

foo (new_param) ->
foo (new_param) =>

for the adding new_param case:

添加new_param案例:

you could do :

你可以这样做:

%s/)\s\?[-=]>/, new_param&/g