删除 Vim 中的行并将其重定向到另一个文件
我想删除最后 10 行并将其移动到另一个文件。
我目前所做的是:
- 在可视模式下选择最后10行,
- 通过
:'<,'>w
将这些行写入其他文件, - 然后删除选定的行。
有没有更有效的方法可以做到这一点?
I want to delete say last 10 lines and move it to another file.
What I currently do is:
- select last 10 lines in visual mode,
- write these lines by
:'<,'>w
to other file, - and then delete selected lines.
Is there any more efficient way I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过执行以下操作删除一个步骤:
:!>; newfile.txt
这将使用外部命令将行写入给定的文件名,并同时删除它们,因为该命令没有输出任何内容。
如果您想追加到文件而不是覆盖它,请使用
>>
而不是>
。You can remove one step by doing this:
:!> newfile.txt
This will use an external command to write the lines to the given filename, and simultaneously delete them because nothing was output from the command.
If you want to append to a file instead of overwriting it, then use
>>
instead of>
.您可以改用
ex
命令。例如:1,10w file.name
写入前十行:$-9,$w file.name
写入后十行行(美元符号表示最后一行)With the code below in your
.vimrc
you can use the command:MoveTo file.name
In normal mode the steps you mentioned (
GV9k:w file.name gvd
) are the most efficient in my opinion.You can use
ex
commands instead. e.g.:1,10w file.name
Write the first ten lines:$-9,$w file.name
Write the last ten lines (the dollar sign denotes last line)With the code below in your
.vimrc
you can use the command:MoveTo file.name
In normal mode the steps you mentioned (
GV9k:w file.name gvd
) are the most efficient in my opinion.将一系列行写入文件并删除它们的简单方法
之后就是运行命令
,但是如果文件名不固定的话,频繁使用会很不方便。
下面是使用相同命令实现命令的函数
MoveToFile()
姓名。整个过程包含以下两个步骤:编写一系列行
使用
:write
命令(参见:help :w_f
、:help :w!
、:help :w_a
) , 然后删除该范围。1
现在您可以使用上述命令来涵盖所有常见用例。为了
例如,要将视觉上选定的行范围移动到文件,请使用映射
此映射会触发半完整命令调用
:MoveToFile
在可视模式下选择的行范围 (
'<,'>
)。您只需输入文件名并按 Enter。
如果您经常对缓冲区的最后十行执行此操作,请创建类似的
映射仅适用于这种情况:
1 指定行被删除到默认寄存器中
覆盖其以前的内容。删除行而不影响
寄存器将
MoveToFile()
中的最后一个命令更改为在本例中
:delete
使用黑洞寄存器(请参阅:help :d
和>:帮助
) 而不是默认的。"_
A straightforward way of writing a range of lines to file and deleting them
afterwards is to run the command
However, it is inconvenient for frequent use if the file name is not constant.
Below is the function
MoveToFile()
implementing the command with the samename. The whole thing wraps the following two steps: write a range of lines
using
:write
command (see:help :w_f
,:help :w!
,:help :w_a
), thendelete that range.1
Now you can use the above command to cover all frequent use cases. For
example, to move visually selected range of lines to file use the mapping
This mapping triggers semi-complete command calling
:MoveToFile
for therange of lines selected in Visual mode (
'<,'>
). You need only to typea file name and hit Enter.
If you frequently do this for the last ten lines of a buffer, create a similar
mapping just for this case:
1 Specified lines are deleted into the default register
overwriting its previous contents. To delete lines without affecting
registers change the last command in
MoveToFile()
toIn this case
:delete
uses the black hole register (see:help :d
and:help
) instead of the default one."_
当您使用可视模式选择行时,您只需按三个键即可删除刚刚写下的行:
d'>
(请参阅:help '>
)。While you use visual mode for selecting lines, you can delete just written down lines pressing only three keys:
d'>
(see:help '>
).