加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

vim如何插入文本,保存并退出bash

发布时间:2020-12-16 01:52:49 所属栏目:安全 来源:网络整理
导读:我有多个要编辑的文件.我必须进行一些更改,比如根据模式复制一些行,然后我必须在不同的地方插入一些文本.我打算用 vim来自动化我的任务.我写过这个剧本 ggisome text to addsome more text to addesc:%s/text/TEXT/g:wq 但它只是打开vim编辑器并在文件中插入
我有多个要编辑的文件.我必须进行一些更改,比如根据模式复制一些行,然后我必须在不同的地方插入一些文本.我打算用 vim来自动化我的任务.我写过这个剧本

gg
i
some text to add
some more text to add
<esc>
:%s/text/TEXT/g
:wq

但它只是打开vim编辑器并在文件中插入命令,然后我必须手动删除以下文本

<esc>
:%s/text/TEXT/g
:wq

并保存文件.

我正在调用以下命令:

vi -s vimscript mytextfile

我之前使用过vim脚本来做其他事情而不是插入搜索和替换或复制粘贴模式等文本.

解决方法

来自:help -s:

-s {scriptin}   The script file "scriptin" is read.  The characters in the
                file are interpreted as if you had typed them.  The same can
                be done with the command ":source! {scriptin}".  If the end
                of the file is reached before the editor exits,further
                characters are read from the keyboard.  Only works when not
                started in Ex mode,see |-s-ex|.  See also |complex-repeat|.
                {not in Vi}

把它想象成一个宏.这是你的vimscript文件的样子:

Osome text to add^Msome more text to add^[:%s/text/TEXT^M:wq^M

^ M特殊字符是文字??< CR>并且用< C-v> CR获得.

^ [特殊字符是文字??< Esc>并且用< C-v>< Esc>获得.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读