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

前端 未结 7 1949
梦毁少年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:23

    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

    :r !tail -10 xyz.xml 
    

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

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

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

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

    This operation should complete instantly even for fairly large files.

提交回复
热议问题