删除 Vim 中的行并将其重定向到另一个文件

发布于 2024-12-09 06:58:15 字数 179 浏览 0 评论 0原文

我想删除最后 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乖不如嘢 2024-12-16 06:58:15

您可以通过执行以下操作删除一个步骤:

  • 直观地选择行
  • :!>; newfile.txt

这将使用外部命令将行写入给定的文件名,并同时删除它们,因为该命令没有输出任何内容。

如果您想追加到文件而不是覆盖它,请使用 >> 而不是 >

You can remove one step by doing this:

  • visually select the lines
  • :!> 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 >.

折戟 2024-12-16 06:58:15

您可以改用 ex 命令。例如

:1,10w file.name   写入十行

:$-9,$w file.name 写入十行行(美元符号表示最后一行)


With the code below in your .vimrc you can use the command :MoveTo file.name

function! MoveLastLines(f)
  exe '$-9,$w ' . a:f    "write last ten lines to the passed filename
  $-9,$d                 "delete last ten lines
endfunction

command! -nargs=1 -range MoveTo :call MoveLastLines(<f-args>)


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

function! MoveLastLines(f)
  exe '$-9,$w ' . a:f    "write last ten lines to the passed filename
  $-9,$d                 "delete last ten lines
endfunction

command! -nargs=1 -range MoveTo :call MoveLastLines(<f-args>)


In normal mode the steps you mentioned (GV9k:w file.name gvd) are the most efficient in my opinion.

谁人与我共长歌 2024-12-16 06:58:15

将一系列行写入文件并删除它们的简单方法
之后就是运行命令

:$-9,$w path | '[,']d

,但是如果文件名不固定的话,频繁使用会很不方便。

下面是使用相同命令实现命令的函数 MoveToFile()
姓名。整个过程包含以下两个步骤:编写一系列行
使用 :write 命令(参见 :help :w_f:help :w!:help :w_a) , 然后
删除该范围。1

command! -nargs=* -complete=file -range=% -bang -bar MoveToFile
\   :<line1>,<line2>call MoveToFile(<q-args>, <bang>0)
function! MoveToFile(fname, overwrite) range
    let r = a:firstline . ',' . a:lastline
    exe r 'w' . ' !'[a:overwrite] . fnameescape(a:fname)
    exe r 'd'
endfunction

现在您可以使用上述命令来涵盖所有常见用例。为了
例如,要将视觉上选定的行范围移动到文件,请使用映射

:vnoremap <leader>m :MoveToFile

此映射会触发半完整命令调用 :MoveToFile
在可视模式下选择的行范围 ('<,'>)。您只需输入
文件名并按 Enter

如果您经常对缓冲区的最后十行执行此操作,请创建类似的
映射仅适用于这种情况:

:nnoremap <leader>m :$-9,$MoveToFile

1 指定行被删除到默认寄存器中
覆盖其以前的内容。删除行而不影响
寄存器将 MoveToFile() 中的最后一个命令更改为

exe r 'd_'

在本例中 :delete 使用黑洞寄存器(请参阅 :help :d>:帮助
"_
) 而不是默认的。

A straightforward way of writing a range of lines to file and deleting them
afterwards is to run the command

:$-9,$w path | '[,']d

However, it is inconvenient for frequent use if the file name is not constant.

Below is the function MoveToFile() implementing the command with the same
name. 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), then
delete that range.1

command! -nargs=* -complete=file -range=% -bang -bar MoveToFile
\   :<line1>,<line2>call MoveToFile(<q-args>, <bang>0)
function! MoveToFile(fname, overwrite) range
    let r = a:firstline . ',' . a:lastline
    exe r 'w' . ' !'[a:overwrite] . fnameescape(a:fname)
    exe r 'd'
endfunction

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

:vnoremap <leader>m :MoveToFile

This mapping triggers semi-complete command calling :MoveToFile for the
range of lines selected in Visual mode ('<,'>). You need only to type
a 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:

:nnoremap <leader>m :$-9,$MoveToFile

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() to

exe r 'd_'

In this case :delete uses the black hole register (see :help :d and :help
"_
) instead of the default one.

吖咩 2024-12-16 06:58:15

当您使用可视模式选择行时,您只需按三个键即可删除刚刚写下的行:d'>(请参阅:help '>)。

While you use visual mode for selecting lines, you can delete just written down lines pressing only three keys: d'> (see :help '>).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文