On Linux, editing file_B in vim I want to add line 10-25 from file_A. Example: One has a whole set of HTML-Documents with quite the same header and yes, one could create a template and start from there, but still:

I am looking for a simple [and elegant] way to insert a range of lines from one file and add it to the file I am currently editing. In vim.

The solutions I found so far but are not exactly what I'm looking for, are

  • inside file_B the command :read file_A will add *the whole file_A* into file_B. Too much
  • there is copy&paste inside vim – but for this one must have opened file_A
  • inside file_A :10,25w! file_B will 'send' the given range of lines to file_B.
    This seems quite close to copy&paste
  • one can open multiple windows in vim
    But as I work on an 8inch screen, multiple windows make orientation hard

As I wasn't able to find a comfortable solution I wonder if I just misunderstood something or the ways I found so far are commonly used and taken as the standard way.

  • See this SO question, @Bernhard's answer was the accepted answer to that question over there too!
    – slm
    Apr 20 '13 at 12:41
  • @slm I was aware of this answer, but as I didn't put it into the list of solutions I found, thank you for the hint. I am still looking for something built within vim. But this seems out of reach. And I really wonder if this only achievable through outside commands, because as far as I got with vim now, I came to little restrictions up to here.
    – erch
    Apr 20 '13 at 15:03
  • @cellar.dweller You could make some kind of function out of it, if you really want something easier.
    – Bernhard
    Apr 21 '13 at 20:14

Best solution I can come up with, is to externally call sed and combine that with :read !

:r !sed -n -e '10,25p' fileB
  • Doesn't work here - but why? First, thank you for your help. What I get is 'E34: no previous command' [roughly translated] as if vim tries to repeat a command. As far I understand it, step by step: :r is short for read in vim. I have no knowledge about sed and how to use it. Of course, I replaced fileB with the actual file :)
    – erch
    Apr 21 '13 at 19:30
  • @cellar.dweller My answer was updated by some-one, I got the same error by that code. I back-rolled it. See if this works for you.
    – Bernhard
    Apr 21 '13 at 19:51
  • @Stephane Good to shorten answers, but your suggestion didn't work.
    – Bernhard
    Apr 21 '13 at 19:52
  • Now it works! To add a little myslef: starting with :10r etc. will place the content from line ten on [as long as this exists]. Works like a charm now. Great. Thanks a lot!
    – erch
    Apr 21 '13 at 20:09
  • 3
    Oops, sorry. Forgot about vim's expansion of !. Should have been :r!sed 10,25\!d. Note that you don't need the -e or the quotes, so you can write it :r!sed -n 10,25p for short as well. :r sed '10,$\!d;25q' to avoid reading the whole file. Apr 22 '13 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.