在Vim中重命名参数的函数

时间:2014-10-13 14:52:04

标签: javascript vim arguments rename

我正在通过使用学习VIM,我找不到与我的需求直接相关的东西。

我想在我的函数中快速重命名一个参数。

function( a_fuz, a_roh, a_baz ) {
    return a_fuz + a_roh + a_baz;
}

选择a_baz并调用我的函数,我想输入a_dah并替换函数体内所有出现的单词a_baz(以及代码的标题) )。

我的函数类似于vimrc文件中的一个函数:

function RenameArg()
    let l_word = expand('<cword>')
    let l_replace = input('Name: ')
    " Moves the cursor to the definition, then to the next open bracket
    " character. Then, it selects the whole brackets pair
    execute "normal gdV/{/e\<CR>%"
    " Performs a substitution of the words inside the selected area

    " For debugging purposes
    " echomsg "\'<,\'>s/\\<" . l_word . "\\>/" . l_replace
    execute "\'<,\'>s/\\<" . l_word . "\\>/" . l_replace
endfunction

我不太清楚这里有什么问题。当我单独运行两个execute时,它可以工作。但是当我调用我的函数时,它似乎只选择了第一行。

修改

这是重命名参数和局部变量的函数的当前版本(当gd标识不应匹配的结果时会出现问题。此外,它使用括号作为定位的分隔符):< / p>

"Renames an argument from a function
function RenameArg()
    let l_word = expand('<cword>')
    let l_replace = input('Name: ')
    " Moves the cursor to the definition, then to the next open bracket
    " character. Then, it selects the whole brackets pair
    normal gd
    execute "normal V/{/e\<CR>%:s/\\<" . l_word . "\\>/" . l_replace . "\<CR>"
endfunction

"Renames a variable local to the current scope.
function RenameLocal()
    let l_word = expand('<cword>')
    let l_replace = input('Name: ')
    " Moves the cursor to the definition, then to the next open bracket
    " character. Then, it selects the whole brackets pair
    normal gd
    execute "normal V[{%:s/\\<" . l_word . "\\>/" . l_replace . "\<CR>"
endfunction

1 个答案:

答案 0 :(得分:2)

您可以通过:s:norm进行execute "normal gdV/{/e\<CR>%:s//" . l_replace . "\<cr>" 来解决此问题。

gn

您可能想要跳过自定义功能并使用gdcgna_dah<esc> 动作。例如:

a_dah

这会将您的参数更改为.。最好的部分是你可以继续按gd重复每个实例的更改。 &#39;

说明:

  • cgn转到定义
  • a_dah<esc>更改匹配的文字`
  • .新文本并转到正常模式
  • gn重复更改

注意::h :norm :h :exe :h gn :h . 需要Vim 7.4

如需更多帮助,请参阅:

{{1}}