vim - 如何读取文件到当前缓冲区的行范围

时间:2022-11-12 19:18:57

I want to read line n1->n2 from file foo.c into the current buffer.

我想从文件foo.c读取行n1-> n2到当前缓冲区。

I tried: 147,227r /path/to/foo/foo.c

我试过:147,227r /path/to/foo/foo.c

But I get: "E16: Invalid range", though I am certain that foo.c contains more than 1000 lines.

但我得到:“E16:无效范围”,但我确定foo.c包含超过1000行。

7 个解决方案

#1


80  

:r! sed -n 147,227p /path/to/foo/foo.c

#2


19  

You can do it in pure Vimscript, without having to use an external tool like sed:

您可以在纯Vimscript中完成,而无需使用像sed这样的外部工具:

:put =readfile('/path/to/foo/foo.c')[146:226]

Note that we must decrement one from the line numbers because arrays start from 0 while line numbers start from 1.

请注意,我们必须从行号中减少一个,因为数组从0开始,而行号从1开始。

Disadvantages: This solution is 7 characters longer than the accepted answer, and it will temporarily consume memory relative to the size of the other file.

缺点:此解决方案比接受的答案长7个字符,并且它将暂时消耗相对于其他文件大小的内存。

#3


18  

The {range} refers to the destination in the current file, not the range of lines in the source file.

{range}指的是当前文件中的目标,而不是源文件中的行范围。

After some experimentation, it seems

经过一些实验,似乎

:147,227r /path/to/foo/foo.c

means insert the contents of /path/to/foo/foo.c after line 227 in this file. i.e.: it ignores the 147.

表示在此文件的第227行之后插入/path/to/foo/foo.c的内容。即:它忽略了147。

#4


3  

Other solutions posted are great for specific line numbers. It's often the case that you want to read from top or bottom of another file. In that case, reading the output of head or tail is very fast. For example -

发布的其他解决方案非常适合特定的行号。通常情况下,您想要从另一个文件的顶部或底部读取。在这种情况下,读取头部或尾部的输出非常快。例如 -

:r !head -20 xyz.xml

Will read first 20 lines from xyz.xml into current buffer where the cursor is

将xyz.xml中的前20行读入光标所在的当前缓冲区

:r !tail -10 xyz.xml 

Will read last 10 lines from xyz.xml into current buffer where the cursor is

将从xyz.xml读取最后10行到光标所在的当前缓冲区

The head and tail commands are extremely fast, therefore even combining them can be much faster than other approaches for very large files.

head和tail命令非常快,因此即使组合它们也可以比非常大的文件的其他方法快得多。

:r !head -700030 xyz.xml| tail -30

Will read line numbers from 700000 to 700030 from file xyz.xml into current buffer

将从文件xyz.xml读取700000到700030的行号到当前缓冲区

This operation should complete instantly even for fairly large files.

即使对于相当大的文件,此操作也应立即完成。

#5


2  

You will need to:

你需要:

:r /path/to/foo/foo.c
:d 228,$
:d 1,146

Three steps, but it will get it done...

三个步骤,但它会完成它...

#6


2  

A range permits a command to be applied to a group of lines in the current buffer.

范围允许将命令应用于当前缓冲区中的一组行。

So, the range of read instruction means where to insert the content in the current file, but not the range of file that you want to read.

因此,读取指令的范围意味着在当前文件中插入内容的位置,而不是您要读取的文件范围。

#7


1  

I just had to do this in a code project of mine and did it this way:

我只需要在我的代码项目中执行此操作并以此方式执行此操作:

In buffer with /path/to/foo/foo.c open:

在带有/path/to/foo/foo.c的缓冲区中打开:

:147,227w export.txt

In buffer I'm working with:

在缓冲区我正在使用:

:r export.txt

Much easier in my book... It requires having both files open, but if I'm importing a set of lines, I usually have them both open anyway. This method is more general and easier to remember for me, especially if I'm trying to export/import a trickier set of lines using g/<search_criteria/:.w >> export.txt or some other more complicated way of selecting lines.

在我的书中更容易......它需要打开两个文件,但如果我导入一组行,我通常都会打开它们。这种方法对我来说更通用,更容易记住,特别是如果我试图使用g / > export.txt或其他一些更复杂的选择行的方式导出/导入一组棘手的行。

#1


80  

:r! sed -n 147,227p /path/to/foo/foo.c

#2


19  

You can do it in pure Vimscript, without having to use an external tool like sed:

您可以在纯Vimscript中完成,而无需使用像sed这样的外部工具:

:put =readfile('/path/to/foo/foo.c')[146:226]

Note that we must decrement one from the line numbers because arrays start from 0 while line numbers start from 1.

请注意,我们必须从行号中减少一个,因为数组从0开始,而行号从1开始。

Disadvantages: This solution is 7 characters longer than the accepted answer, and it will temporarily consume memory relative to the size of the other file.

缺点:此解决方案比接受的答案长7个字符,并且它将暂时消耗相对于其他文件大小的内存。

#3


18  

The {range} refers to the destination in the current file, not the range of lines in the source file.

{range}指的是当前文件中的目标,而不是源文件中的行范围。

After some experimentation, it seems

经过一些实验,似乎

:147,227r /path/to/foo/foo.c

means insert the contents of /path/to/foo/foo.c after line 227 in this file. i.e.: it ignores the 147.

表示在此文件的第227行之后插入/path/to/foo/foo.c的内容。即:它忽略了147。

#4


3  

Other solutions posted are great for specific line numbers. It's often the case that you want to read from top or bottom of another file. In that case, reading the output of head or tail is very fast. For example -

发布的其他解决方案非常适合特定的行号。通常情况下,您想要从另一个文件的顶部或底部读取。在这种情况下,读取头部或尾部的输出非常快。例如 -

:r !head -20 xyz.xml

Will read first 20 lines from xyz.xml into current buffer where the cursor is

将xyz.xml中的前20行读入光标所在的当前缓冲区

:r !tail -10 xyz.xml 

Will read last 10 lines from xyz.xml into current buffer where the cursor is

将从xyz.xml读取最后10行到光标所在的当前缓冲区

The head and tail commands are extremely fast, therefore even combining them can be much faster than other approaches for very large files.

head和tail命令非常快,因此即使组合它们也可以比非常大的文件的其他方法快得多。

:r !head -700030 xyz.xml| tail -30

Will read line numbers from 700000 to 700030 from file xyz.xml into current buffer

将从文件xyz.xml读取700000到700030的行号到当前缓冲区

This operation should complete instantly even for fairly large files.

即使对于相当大的文件,此操作也应立即完成。

#5


2  

You will need to:

你需要:

:r /path/to/foo/foo.c
:d 228,$
:d 1,146

Three steps, but it will get it done...

三个步骤,但它会完成它...

#6


2  

A range permits a command to be applied to a group of lines in the current buffer.

范围允许将命令应用于当前缓冲区中的一组行。

So, the range of read instruction means where to insert the content in the current file, but not the range of file that you want to read.

因此,读取指令的范围意味着在当前文件中插入内容的位置,而不是您要读取的文件范围。

#7


1  

I just had to do this in a code project of mine and did it this way:

我只需要在我的代码项目中执行此操作并以此方式执行此操作:

In buffer with /path/to/foo/foo.c open:

在带有/path/to/foo/foo.c的缓冲区中打开:

:147,227w export.txt

In buffer I'm working with:

在缓冲区我正在使用:

:r export.txt

Much easier in my book... It requires having both files open, but if I'm importing a set of lines, I usually have them both open anyway. This method is more general and easier to remember for me, especially if I'm trying to export/import a trickier set of lines using g/<search_criteria/:.w >> export.txt or some other more complicated way of selecting lines.

在我的书中更容易......它需要打开两个文件,但如果我导入一组行,我通常都会打开它们。这种方法对我来说更通用,更容易记住,特别是如果我试图使用g / > export.txt或其他一些更复杂的选择行的方式导出/导入一组棘手的行。