How to execute a normal mode command in a vim function?

99封情书 提交于 2019-12-22 12:23:49

问题


I am writing a vim function to insert some text in a c++ file, please see the following function:

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    

In normal mode, I use == to re-indent one code line. My question is how to call == in the above function. Furthermore, how to execute the command such as 2f" which move the cursor to the second ".


回答1:


To indent, you can just use

normal ==

To find also you can use

normal 2f"

or even shorter

norm <whatever you do in normal mode>

Now you might be getting what I'm trying to say.
If not, read documentation :h normal.




回答2:


Try this in your function:

execute 'normal 2f"'


来源:https://stackoverflow.com/questions/38364675/how-to-execute-a-normal-mode-command-in-a-vim-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!