从函数将文本插入当前缓冲区 [英] Insert text into current buffer from function

查看:56
本文介绍了从函数将文本插入当前缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到有关此文档的信息.如何在vim函数中从当前缓冲区读取/写入文本?

I'm having a hard time finding the documentation for this. How do I read/write text from the current buffer in my vim functions?

更具体地说,如果我的缓冲区中包含 foo bar 字样,那么如何编写一个函数来用 cat 覆盖 bar 字样,以便最后我的缓冲区包含 foo cat ?

More concretely, if my buffer contains the words foo bar how would write a function to overwrite the word bar with cat so that in the end my buffer contains foo cat?

推荐答案

要访问行,可以使用 getline()函数. setline()更新缓冲区中的这些行.同样,通过 append()插入新行.

To access line(s), you can use the getline() function. setline() updates those lines in the buffer. Likewise, new lines are inserted via append().

后者也可以使用:put = {变量或表达式} 完成,并用:substitute 代替.更好的选择取决于特定的用例.前一个较低级别的函数的好处是它们不会破坏表达式寄存器,上次使用的搜索模式,搜索历史等内容.

The latter can also be done with :put ={variable or expression}, and replacements with :substitute. What is better depends on the particular use case. The benefit of the former, lower-level functions is that they don't clobber stuff like the expression register, last used search pattern, search history, etc.

这篇关于从函数将文本插入当前缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆