I want bash shell that does the following
我希望bash shell执行以下操作
- open a file using vim
- 使用vim打开文件。
- write something into file
- 把东西写进文件
-
save and exit the file
保存并退出文件
bashpromt# cat mybash.sh echo "about to open a file" vim file.txt #I need to use vim application to open a file #now write something into file.txt #close the that file. echo "done."
IS that possible? I found something like vim script , but not sure how to use it. Or something like "HERE" doc can be used for this ?
这有可能吗?我发现了类似vim脚本的东西,但不知道如何使用它。或者像"这里"医生可以用来做这个?
EDIT: I need to verify that vim editor is working fine over our file system.So I need to write script that invokes vim and does some command and close it. My requirement doesn't fit into doing stuffs like "echo "something" > file.txt" . I got to open the file using vim.
编辑:我需要验证vim编辑器在我们的文件系统上运行良好。因此我需要编写调用vim的脚本并执行一些命令并关闭它。我的要求不适合做诸如“echo”之类的东西。txt”。我用vim打开文件。
5 个解决方案
#1
10
Vim has several options:
Vim有几个选项:
-
-c
=> pass ex commands. Example:vim myfile.txt -c 'wq'
to force the last line of a file to be newline terminated (unlessbinary
is set in some way by a script) - -c =>通过ex命令。例如:vim myfile。txt -c 'wq'迫使文件的最后一行以换行结尾(除非二进制代码以某种方式设置)
-
-s
=> play a scriptout that was recorded with-W
. For example, if your file containsZZ
, thenvim myfile.txt -s the_file_containing_ZZ
will do the same as previously. - -s =>播放记录为-W的scriptout。例如,如果您的文件包含ZZ,那么vim myfile。txt -s the_file_containing_ZZ将执行与前面相同的操作。
Also note that, invoked as ex
, vim will start in ex mode ; you can try ex my_file.txt <<< wq
还要注意,作为ex调用,vim将以ex模式启动;您可以尝试ex my_file。txt < < < wq
#2
16
ex
is the commandline version for vi
, and much easier to use in scripts.
ex是vi的命令行版本,在脚本中更容易使用。
ex $yourfile <<EOEX
:%s/$string_to_replace/$string_to_replace_it_with/g
:x
EOEX
#3
4
You asked how to write "something" into a text file via vim and no answer has necessarily covered that yet.
您曾问过如何通过vim将“某物”写入文本文件,但还没有得到任何答案。
To insert text:
插入文本:
ex $yourfile <<EOEX
:i
my text to insert
.
:x
EOEX
:i
enters insert mode. All following lines are inserted text until .
is seen appearing by itself on its own line.
我进入插入模式。下面的所有行都被插入文本直到。它自己出现在自己的线上。
Here is how to search and insert as well. You can do something such as:
下面是如何搜索和插入。你可以做一些事情,比如:
ex $yourfile <<EOEX
:/my search query\zs
:a
my text to insert
.
:x
EOEX
This will find the first selection that matches regex specified by :/
, place the cursor at the location specified by \zs
, and enter insert mode after the cursor.
这将找到匹配regex的第一个选择:/,将光标置于\zs指定的位置,并在游标之后进入插入模式。
You can move \zs
to achieve different results. For example:
你可以移动\zs来获得不同的结果。例如:
ex $yourfile <<EOEX
:/start of match \zs end of match
:a
my text to insert
.
:x
EOEX
This will change the first occurrence of "start of match end of match" to "start of match my text to insert end of match".
这将把“开始匹配结束”的第一次出现更改为“开始匹配我的文本以插入匹配结束”。
If you want to allow any amount of whitespace in your searches between keywords, use \_s*
. For example, searching for a function that returns 0: :/\_s*return\_s*0}
如果您希望在关键字之间的搜索中允许任何数量的空格,请使用\_s*。例如,搜索返回0:/\_s*return\_s*0}的函数
#4
1
If you are wanting to see the work being done inside vim or gvim you can use --remote-send
如果您希望看到在vim或gvim中完成的工作,可以使用——远程发送
gvim --servername SHELL_DRIVER
bashpromt# cat mybash.sh
#!/bin/bash
echo "about to open $1"
gvim --servername SHELL_DRIVER $1 #I need to use vim application to open a file
#now write something into file.txt and close it
gvim --servername SHELL_DRIVER --remote-send '<ESC>i something to the file<ESC>:wq<CR>'
echo "done."
This will be slow but will do what you want it to.
First we open a gvim in which we can open all of our files (for efficiency)
With the first gvim line we open the file in the previously opened gvim.
On the second gvim line we send a command to the previously opened instance of gvim (with the desired file still open).
The command is as follows:<ESC>
- get out of any mode that gvim might have been ini something to the file
- go into insert mode and type " something to the file"<ESC>
- exit insert mode:wq
- write the file and quit vim
这将是缓慢的,但将做你想要的。首先,我们打开一个gvim,在其中,我们可以使用前面打开的gvim中打开的第一行gvim来打开所有文件(为了提高效率)。在第二个gvim行上,我们向先前打开的gvim实例发送一个命令(需要的文件仍然打开)。命令如下:
#5
1
Recently I have answered a similar question, "automated edit of several files". May be solution that I describe there will satisfy your needs.
最近我也回答了一个类似的问题,“几个文件的自动编辑”。也许我描述的解决方案会满足你的需要。
#1
10
Vim has several options:
Vim有几个选项:
-
-c
=> pass ex commands. Example:vim myfile.txt -c 'wq'
to force the last line of a file to be newline terminated (unlessbinary
is set in some way by a script) - -c =>通过ex命令。例如:vim myfile。txt -c 'wq'迫使文件的最后一行以换行结尾(除非二进制代码以某种方式设置)
-
-s
=> play a scriptout that was recorded with-W
. For example, if your file containsZZ
, thenvim myfile.txt -s the_file_containing_ZZ
will do the same as previously. - -s =>播放记录为-W的scriptout。例如,如果您的文件包含ZZ,那么vim myfile。txt -s the_file_containing_ZZ将执行与前面相同的操作。
Also note that, invoked as ex
, vim will start in ex mode ; you can try ex my_file.txt <<< wq
还要注意,作为ex调用,vim将以ex模式启动;您可以尝试ex my_file。txt < < < wq
#2
16
ex
is the commandline version for vi
, and much easier to use in scripts.
ex是vi的命令行版本,在脚本中更容易使用。
ex $yourfile <<EOEX
:%s/$string_to_replace/$string_to_replace_it_with/g
:x
EOEX
#3
4
You asked how to write "something" into a text file via vim and no answer has necessarily covered that yet.
您曾问过如何通过vim将“某物”写入文本文件,但还没有得到任何答案。
To insert text:
插入文本:
ex $yourfile <<EOEX
:i
my text to insert
.
:x
EOEX
:i
enters insert mode. All following lines are inserted text until .
is seen appearing by itself on its own line.
我进入插入模式。下面的所有行都被插入文本直到。它自己出现在自己的线上。
Here is how to search and insert as well. You can do something such as:
下面是如何搜索和插入。你可以做一些事情,比如:
ex $yourfile <<EOEX
:/my search query\zs
:a
my text to insert
.
:x
EOEX
This will find the first selection that matches regex specified by :/
, place the cursor at the location specified by \zs
, and enter insert mode after the cursor.
这将找到匹配regex的第一个选择:/,将光标置于\zs指定的位置,并在游标之后进入插入模式。
You can move \zs
to achieve different results. For example:
你可以移动\zs来获得不同的结果。例如:
ex $yourfile <<EOEX
:/start of match \zs end of match
:a
my text to insert
.
:x
EOEX
This will change the first occurrence of "start of match end of match" to "start of match my text to insert end of match".
这将把“开始匹配结束”的第一次出现更改为“开始匹配我的文本以插入匹配结束”。
If you want to allow any amount of whitespace in your searches between keywords, use \_s*
. For example, searching for a function that returns 0: :/\_s*return\_s*0}
如果您希望在关键字之间的搜索中允许任何数量的空格,请使用\_s*。例如,搜索返回0:/\_s*return\_s*0}的函数
#4
1
If you are wanting to see the work being done inside vim or gvim you can use --remote-send
如果您希望看到在vim或gvim中完成的工作,可以使用——远程发送
gvim --servername SHELL_DRIVER
bashpromt# cat mybash.sh
#!/bin/bash
echo "about to open $1"
gvim --servername SHELL_DRIVER $1 #I need to use vim application to open a file
#now write something into file.txt and close it
gvim --servername SHELL_DRIVER --remote-send '<ESC>i something to the file<ESC>:wq<CR>'
echo "done."
This will be slow but will do what you want it to.
First we open a gvim in which we can open all of our files (for efficiency)
With the first gvim line we open the file in the previously opened gvim.
On the second gvim line we send a command to the previously opened instance of gvim (with the desired file still open).
The command is as follows:<ESC>
- get out of any mode that gvim might have been ini something to the file
- go into insert mode and type " something to the file"<ESC>
- exit insert mode:wq
- write the file and quit vim
这将是缓慢的,但将做你想要的。首先,我们打开一个gvim,在其中,我们可以使用前面打开的gvim中打开的第一行gvim来打开所有文件(为了提高效率)。在第二个gvim行上,我们向先前打开的gvim实例发送一个命令(需要的文件仍然打开)。命令如下:
#5
1
Recently I have answered a similar question, "automated edit of several files". May be solution that I describe there will satisfy your needs.
最近我也回答了一个类似的问题,“几个文件的自动编辑”。也许我描述的解决方案会满足你的需要。