【问题标题】:vim how to insert text, save and exit from bashvim如何插入文本,保存和退出bash
【发布时间】:2014-04-29 18:52:25
【问题描述】:

我有多个文件要编辑。我必须进行某些更改,例如根据模式复制一些行,然后我必须在不同的位置插入一些文本。我打算使用 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 脚本来做其他事情,而不是插入文本,例如搜索和替换或复制粘贴模式等。

【问题讨论】:

    标签: bash shell vim automation


    【解决方案1】:

    替代方案

    除非您真的需要特殊的 Vim 功能,否则最好使用 非交互式工具,例如 sedawk 或 Perl/Python/Ruby/您最喜欢的脚本语言在这里

    也就是说,你可以以非交互方式使用 Vim:

    静默批处理模式

    对于非常简单的文本处理(即像增强的 'sed' 或 'awk' 一样使用 Vim,也许只是受益于 :substitute 命令中增强的正则表达式),请使用 Ex-mode .

    REM Windows
    call vim -N -u NONE -n -es -S "commands.ex" "filespec"
    

    注意:静默批处理模式 (:help -s-ex) 会弄乱 Windows 控制台,因此您可能需要在 Vim 运行后执行cls 来清理。

    # Unix
    vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"
    

    注意:如果"commands.ex" 文件不存在,Vim 将挂起等待输入;最好事先检查它的存在!或者,Vim 可以从标准输入读取命令。如果使用 - 参数,您还可以使用从标准输入读取的文本填充新缓冲区,并从标准错误读取命令。

    全自动化

    对于涉及多个窗口的更高级处理,以及 Vim 的真正自动化(您可能与用户交互或让 Vim 运行以让用户接管),请使用:

    vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"
    

    这里是使用的参数的摘要:

    -T dumb           Avoids errors in case the terminal detection goes wrong.
    -N -u NONE        Do not load vimrc and plugins, alternatively:
    --noplugin        Do not load plugins.
    -n                No swapfile.
    -es               Ex mode + silent batch mode -s-ex
                    Attention: Must be given in that order!
    -S ...            Source script.
    -c 'set nomore'   Suppress the more-prompt when the screen is filled
                    with messages or output to avoid blocking.
    

    【讨论】:

      【解决方案2】:

      来自: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 特殊字符是文字​​ &lt;CR&gt;,通过 &lt;C-v&gt;&lt;CR&gt; 获得。

      ^[ 特殊字符是文字​​ &lt;Esc&gt;,通过 &lt;C-v&gt;&lt;Esc&gt; 获得。

      【讨论】:

      • 如何在文本中搜索脚本中的/findthis?然后我想删除第一个搜索位置之前的所有文本。
      【解决方案3】:

      为什么要使用 vim 来自动执行此类任务,而有许多命令或 shell 语言非常出色且效率更高?您可以使用其他工具代替 vim 来自动化您的任务。

      您可以尝试使用 sed 来举例:

      [ ~]$ cat file
      Here is a file
      [ ~]$ echo "some text to add" >> file
      [ ~]$ echo "some more text to add" >> file
      [ ~]$ cat file
      Here is a file
      some text to add
      some more text to add
      [ ~]$ sed -i "s/text/TEXT/g" file
      [ ~]$ cat file
      Here is a file
      some TEXT to add
      some more TEXT to add
      

      编辑:在文件顶部插入一些文本有不同的方法。

      使用临时文件:

       echo "some text to add" > tmp.txt
       cat file.text >> tmp.txt
       mv tmp.text file.txt
      

      使用 sed :

      sed -i "1isome text to add" file.text # or
      sed -i "1s/^/some text to add\n/" file.txt
      

      使用子shell:

      echo -e "some text to add\n$(cat file.txt)" > file.txt
      

      【讨论】:

      • sed 是否支持 vi 支持的g 类型的功能?我不知道。我更习惯于 vi,所以我有宾至如归的感觉。无论如何,感谢您指出效率问题。
      • @Tempora 我编辑了我的帖子以添加一些关于在文件顶部插入一些文本的提示。我猜这相当于gg vim 命令。
      • 此外,我认为使用 vim 在大量文件上自动执行此类任务并不是一个好主意。
      【解决方案4】:

      我发现使用标准 Vim 脚本而不是普通模式脚本更自然。您可以使用以下方式调用它:

      vim -c "source vimscript" mytextfile
      

      您的 vim 脚本需要稍作更新才能使用这种方法。它看起来像:

      1
      i
      some text to add
      some more text to add
      .
      %s/text/TEXT/g
      wq
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-10
        • 1970-01-01
        • 1970-01-01
        • 2013-11-02
        • 2015-04-23
        • 2023-03-28
        • 2023-01-28
        • 1970-01-01
        相关资源
        最近更新 更多