Vim:更改脚本中变量的格式

发布于 2024-08-29 21:14:58 字数 581 浏览 5 评论 0原文

我正在使用 vim 编辑 shell 脚本(没有使用正确的编码标准)。我需要将所有变量从驼峰式记法 startTime 更改为大写加下划线记法 START_TIME

我不想改变方法名称的表示方式。

我想做到这一点的一种方法是编写一个函数并将其映射到一个键。该函数可以执行类似于在命令行上生成此函数的操作:

s/<word under cursor>/<leave cursor here to type what to replace with>

我认为该函数可以适用于其他方便的情况。两个问题:

问题 1:我将如何创建该函数。

  • 我之前在 vim 中创建过函数,但我最不知道的是如何捕捉运动。也就是说,如果你在 vim 中按 dw ,它将删除单词的其余部分。你如何捕捉到这一点?
  • 另外你可以在vim命令行上留下未完成的命令吗?

问题 2:有更好的解决方案吗?您将如何完成这项任务?

I am using vim to edit a shell script (did not use the right coding standard). I need to change all of my variables from camel-hum-notation startTime to caps-and-underscore-notation START_TIME.

I do not want to change the way method names are represented.

I was thinking one way to do this would be to write a function and map it to a key. The function could do something like generating this on the command line:

s/<word under cursor>/<leave cursor here to type what to replace with>

I think that this function could be applyable to other situations which would be handy. Two questions:

Question 1: How would I go about creating that function.

  • I have created functions in vim before the biggest thing I am clueless about is how to capture movement. Ie if you press dw in vim it will delete the rest of a word. How do you capture that?
  • Also can you leave an uncompleted command on the vim command line?

Question 2: Got a better solution for me? How would you approach this task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-09-05 21:14:58
  1. 使用插件

    检查页面底部的 COERCION 部分:

    http://www.vim.org/scripts/script.php ?script_id=1545

  2. 获取 :s 命令命令行

    :nnoremap \c :%s//

    将光标下的单词获取到命令行

  3. 使用 :s 更改光标下的单词

    :nnoremap \c lb:s/\%#/\=toupper(substitute(submatch(0), '\<\@!\u', '_& ;', 'g'))/

    lb 向右移动,然后到单词的开头。我们需要这样做才能得到
    光标位于我们要更改的单词之前,因为我们只想更改
    光标下的单词和正则表达式锚定到当前光标
    位置。需要进行移动,因为 b
    单词的开头移动到前一个单词的开头。

    \%# 匹配当前光标位置

    \= 当替换字符串以“\=”开头时,其余部分将被解释为表达式。 :h 子替换-\=

    submatch(0) 我们正在处理的 :s 命令的整个匹配

    \< 字边界

    \@! 不匹配前一个原子(这是为了不匹配开头的原子)
    单词。如果没有这个,FooBar 将更改为 _FOO_BAR)

    & 在替换表达式中,表示整个匹配

  4. 更改光标下的单词,文件中的所有匹配

    :nnoremap \a :%s//\=toupper(substitute(submatch(0), '\<\@!\u', '_&', 'g'))/g

    参见3.了解说明。

  5. 使用普通模式命令更改光标下的单词

    /\u查找下一个大写字符

    i_ 插入下划线。

    nn 搜索最后搜索的字符串两次(两次是因为退出插入模式后,您向后移动一个字符)。

    . 重复上次更改,在本例中插入下划线。

    重复nn.,直到所有驼峰命名法前面都加了下划线,即FooBarBaz变成了Foo_Bar_Baz

    gUiw 大写当前内部单词

  6. http://vim.wikia.com/wiki/Converting_variables_to_camelCase

  1. Use a plugin

    Check the COERCION section at the bottom of the page:

    http://www.vim.org/scripts/script.php?script_id=1545

  2. Get the :s command to the command line

    :nnoremap \c :%s/<C-r><C-w>/

    <C-r><C-w> gets the word under the cursor to command-line

  3. Change the word under the cursor with :s

    :nnoremap \c lb:s/\%#<C-r><C-w>/\=toupper(substitute(submatch(0), '\<\@!\u', '_&', 'g'))/<Cr>

    lb move right, then to beginning of the word. We need to do this to get
    the cursor before the word we wish to change because we want to change only
    the word under the cursor and the regex is anchored to the current cursor
    position. The moving around needs to be done because b at the
    start of a word moves to the start of the previous word.

    \%# match the current cursor position

    \= When the substitute string starts with "\=" the remainder is interpreted as an expression. :h sub-replace-\=

    submatch(0) Whole match for the :s command we are dealing with

    \< word boundary

    \@! do not match the previous atom (this is to not match at the start of a
    word. Without this, FooBar would be changed to _FOO_BAR)

    & in replace expressions, this means the whole match

  4. Change the word under the cursor, all matches in the file

    :nnoremap \a :%s/<C-r><C-w>/\=toupper(substitute(submatch(0), '\<\@!\u', '_&', 'g'))/g<Cr>

    See 3. for explanation.

  5. Change the word under the cursor with normal mode commands

    /\u<Cr> find next uppercase character

    i_ insert an underscore.

    nn Search the last searched string twice (two times because after exiting insert mode, you move back one character).

    . Repeat the last change, in this case inserting the underscore.

    Repeat nn. until all camelcases have an underscore added before them, that is, FooBarBaz has become Foo_Bar_Baz

    gUiw uppercase current inner word

  6. http://vim.wikia.com/wiki/Converting_variables_to_camelCase

迷鸟归林 2024-09-05 21:14:58

我不知道你对“捕捉动作”的理解是什么。那
说,对于初学者,我会使用类似这样的函数:

fu! ChangeWord()

  let l:the_word = expand('<cword>')

  " Modify according to your rules
  let l:new_var_name = toupper(l:the_word)

  normal b
  let l:col_b = col(".")

  normal e
  let l:col_e = col(".")

  let l:line = getline(".")

  let l:line = substitute(
  \ l:line, 
  \ '^\(' . repeat('.',  l:col_b-1) . '\)' . repeat('.', l:col_e - l:col_b+1), 
  \ '\1' . l:new_var_name, 
  \ '')

  call setline(".", l:line)

endfu

至于在 vim 命令行上留下未完成的命令,我认为您

:map ,x :call ChangeWord(

可以在正常模式下调用 then 。按 ,x

更新

经过思考,下面这个函数有点短:

fu! ChangeWordUnderCursor()

  let l:the_word = expand('<cword>')

  "" Modify according to your rules
  let l:new_var_name = '!' . toupper(l:the_word) . '!'

  normal b
  let l:col_b = col(".")

  normal e
  let l:col_e = col(".")

  let l:line = getline(".")

  exe 's/\%' . l:col_b . 'c.*\%' . (l:col_e+1) .'c/' . l:new_var_name . '/'

endfu

I am not sure what you understand under 'capturing movements'. That
said, for a starter, I'd use something like this for the function:

fu! ChangeWord()

  let l:the_word = expand('<cword>')

  " Modify according to your rules
  let l:new_var_name = toupper(l:the_word)

  normal b
  let l:col_b = col(".")

  normal e
  let l:col_e = col(".")

  let l:line = getline(".")

  let l:line = substitute(
  \ l:line, 
  \ '^\(' . repeat('.',  l:col_b-1) . '\)' . repeat('.', l:col_e - l:col_b+1), 
  \ '\1' . l:new_var_name, 
  \ '')

  call setline(".", l:line)

endfu

As to leaving an uncompleted command on the vim command line, I think you're after

:map ,x :call ChangeWord(

which then can be invoked in normal mode by pressing ,x.

Update

After thinking about it, this following function is a bit shorter:

fu! ChangeWordUnderCursor()

  let l:the_word = expand('<cword>')

  "" Modify according to your rules
  let l:new_var_name = '!' . toupper(l:the_word) . '!'

  normal b
  let l:col_b = col(".")

  normal e
  let l:col_e = col(".")

  let l:line = getline(".")

  exe 's/\%' . l:col_b . 'c.*\%' . (l:col_e+1) .'c/' . l:new_var_name . '/'

endfu
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文