vim — How to read range of lines from a file into current buffer

前端 未结 7 2139
梦毁少年i
梦毁少年i 2021-01-30 03:49

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

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

But I get: \"E16: Invalid range\", though I

7条回答
  •  天命终不由人
    2021-01-30 04:35

    You can do it in pure Vimscript, without having to use an external tool like 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.

    Disadvantages: This solution is 7 characters longer than the accepted answer. It will temporarily read the entire file into memory, which could be a concern if that file is huge.

提交回复
热议问题