如何在vim函数中执行正常模式命令?

8

我正在编写一个vim函数,用于在c++文件中插入一些文本,请参见下面的函数:

function! InsertDebugInfo()                                                                      
     let i = line('.')                                                               
     call append(i+1, '#ifdef DEBUG')                                                
     call append(i+2, 'std::cout << ""  << std::endl;')                              
     call append(i+3, '#endif')                                                      
     call append(i+4, '')                                                            
     call cursor(i+3, 0)                                                             
endfunction    

在正常模式下,我使用==来重新缩进一行代码。我的问题是如何在上述函数中调用==。此外,如何执行命令,例如将光标移动到第二个"2f"
2个回答

12

要缩进,你可以直接使用

normal ==

要查找也可以使用

normal 2f"

甚至更短

norm <whatever you do in normal mode>

现在你可能明白我想说什么了。
如果不明白,请阅读文档:h normal


3

在您的函数中尝试以下操作:

execute 'normal 2f"'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接