Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在vim脚本中替换光标下的单词?_Vim - Fatal编程技术网

如何在vim脚本中替换光标下的单词?

如何在vim脚本中替换光标下的单词?,vim,Vim,在用expand(“”获取当前单词并处理结果字符串后,我将尝试用它替换当前单词 我该怎么做 编辑添加的源文件。我用python写的 cur_word = vim.eval('expand("<cWORD>")') parts = cur_word.split('.') if parts: obj, accesses = parts[0], parts[1:] result = obj + ''.join("['%s']"%a for a in accesses)

在用
expand(“”
获取当前单词并处理结果字符串后,我将尝试用它替换当前单词

我该怎么做

编辑添加的源文件。我用python写的

cur_word = vim.eval('expand("<cWORD>")')
parts = cur_word.split('.')
if parts:
    obj, accesses = parts[0], parts[1:]
    result = obj + ''.join("['%s']"%a for a in accesses)
    # how do I replace the current word with result?
cur\u word=vim.eval('expand(“”))
parts=当前字分割('.'))
如果部分:
obj,访问=零件[0],零件[1:]
结果=obj+''.join(['%s']%a用于访问中的a)
#如何用result替换当前单词?
编辑

看起来你想要这个:

viW
:s/\%V\.\(\w\+\)\%V/\="['" . submatch(1) . "']"/g
例如,对于以下文本,第二行的curosr:

x = a.get.property;
x = a.git.another.property;   # cursor on the first letter 'e'
结果将是

x = a.get.property;
x = a['git']['another']['property'];

你可能想要你

  • 那就扯一个字
  • 移动光标(您没有提到)
  • _将光标下的单词替换为先前拖动的单词
  • 那就是

    姚 (移动光标) 振动

    例如:

     the lazy cow mooned over the racy hump
     cursor here:   ----> +        
    
    现在,做yiW(yank内部单词),Fa(返回:)

    现在,viWp取代了当前的单词:

     the over cow mooned over the racy hump
        --> +
    

    在Vim的python接口中,您可以执行普通模式命令

      vim.command("normal BcW%s" % result)
    

    会成功的。

    嗯。您是否尝试将当前单词替换为其本身?想举个例子吗?@sehe我的意思是在处理后保存回去。只是用v替换了y。和+1,因为这是正确的。不,我得到光标下单词的内容并对其进行处理(例如,添加一个尾随的“X”),然后用更改的内容替换光标下的单词。我知道如何在命令模式下执行此操作,但我不知道如何在函数中执行此操作。您可以使用
    py pos=w.cursor
    (获取一个元组(行,列)),然后从那里开始(您可能可以编辑缓冲区,或者执行vimscript,比如
    调用setline(.“,“blablabla”)
    )您编写的
    替换
    命令非常简洁,你能告诉我regex部分中那些
    %V
    是什么意思吗?@Satoru.Logic它们指定匹配应该在(最后)视觉选择范围内。请参阅建议使用
    ciW
      vim.command("normal BcW%s" % result)