vim 如何插入文本、保存和退出 bash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22575125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vim how to insert text, save and exit from bash
提问by Tem Pora
I have multiple files to edit. I have to make certain changes, like copying some lines based on patterns and then I have to insert some text at different places. I am planning to use vim to automate my task. I have written this script
我有多个文件要编辑。我必须进行某些更改,例如根据模式复制一些行,然后我必须在不同的位置插入一些文本。我打算使用 vim 来自动化我的任务。我写了这个脚本
gg
i
some text to add
some more text to add
<esc>
:%s/text/TEXT/g
:wq
But it just opens up the vim editor and inserts even the commands in the file and then I have to manually remove the following text
但它只是打开 vim 编辑器,甚至在文件中插入命令,然后我必须手动删除以下文本
<esc>
:%s/text/TEXT/g
:wq
and save the file.
并保存文件。
I am invoking the following command:
我正在调用以下命令:
vi -s vimscript mytextfile
I have used vim scripting earlier to do other things than inserting text like searching and replacing or copy-pasting patterns etc.
我之前使用 vim 脚本来做其他事情,而不是插入文本,比如搜索和替换或复制粘贴模式等。
回答by Ingo Karkat
Alternatives
备择方案
Unless you really need special Vim capabilities, you're probably better off using non-interactive toolslike sed
, awk
, or Perl / Python / Ruby / your favorite scripting language here.
除非你真的需要特殊的Vim的功能,你可能关闭使用更好的非交互式工具一样sed
,awk
或Perl / Python的/ Ruby的/你最喜欢的脚本语言在这里。
That said, you can use Vim non-interactively:
也就是说,您可以非交互式地使用 Vim:
Silent Batch Mode
静默批处理模式
For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute
command), use Ex-mode.
对于非常简单的文本处理(即使用 Vim 就像增强的 'sed' 或 'awk',也许只是受益于:substitute
命令中增强的正则表达式),使用Ex-mode。
REM Windows
call vim -N -u NONE -n -es -S "commands.ex" "filespec"
Note: silent batch mode (:help -s-ex
) messes up the Windows console, so you may have to do a cls
to clean up after the Vim run.
注意:静默批处理模式 ( :help -s-ex
) 会弄乱 Windows 控制台,因此您可能需要cls
在 Vim 运行后进行清理。
# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"
Attention: Vim will hang waiting for input if the "commands.ex"
file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the -
argument.
注意:如果"commands.ex"
文件不存在,Vim 将挂起等待输入;最好事先检查它的存在!或者,Vim 可以从标准输入读取命令。如果使用-
参数,您还可以使用从 stdin 读取的文本填充新缓冲区,并从 stderr 读取命令。
Full Automation
全自动化
For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:
对于涉及多个窗口的更高级处理和 Vim 的真正自动化(您可以与用户交互或让 Vim 运行以让用户接管),请使用:
vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"
Here's a summary of the used arguments:
以下是所用参数的摘要:
-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.
回答by romainl
From :help -s
:
来自: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}
Think of it as a macro. This is how your vimscript
file should look:
把它想象成一个宏。这是您的vimscript
文件的外观:
Osome text to add^Msome more text to add^[:%s/text/TEXT^M:wq^M
The ^M
special character is a literal <CR>
and is obtained with <C-v><CR>
.
该^M
特殊字符是文本<CR>
,并与获得<C-v><CR>
。
The ^[
special character is a literal <Esc>
and is obtained with <C-v><Esc>
.
该^[
特殊字符是文本<Esc>
,并与获得<C-v><Esc>
。
回答by Idriss Neumann
Why using vim to automate that kind of task whereas there are many commands or shell languages which are excellent and more efficient to do it ? You could use other tools instead of vim to automate your task.
为什么使用 vim 来自动执行此类任务,而有许多出色且更高效的命令或 shell 语言呢?您可以使用其他工具代替 vim 来自动化您的任务。
You could try to use sed for exemple :
您可以尝试使用 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
EDIT : There are different ways to insert some text at the top of a file.
编辑:在文件顶部插入一些文本有不同的方法。
Using a temporary file :
使用临时文件:
echo "some text to add" > tmp.txt
cat file.text >> tmp.txt
mv tmp.text file.txt
Using sed :
使用 sed :
sed -i "1isome text to add" file.text # or
sed -i "1s/^/some text to add\n/" file.txt
Using subshell :
使用子外壳:
echo -e "some text to add\n$(cat file.txt)" > file.txt
回答by R Sahu
I find it more natural to use a standard Vim script, not a normal mode script. You can invoke it using:
我发现使用标准 Vim 脚本更自然,而不是普通模式脚本。您可以使用以下方法调用它:
vim -c "source vimscript" mytextfile
Your vim script will need a little update to work using this approach. It will look something like:
你的 vim 脚本需要一点更新才能使用这种方法工作。它看起来像:
1
i
some text to add
some more text to add
.
%s/text/TEXT/g
wq