MoonPoint Support Logo


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals

Advanced Search
Sun Mon Tue Wed Thu Fri Sat

Sun, Jan 28, 2018 10:15 pm

Reading specific lines from a file into another file within Vi

If you need to read lines from another file into the file you are currently editing within the Vi text editor, you can do so by utilizing the editor's ability to execute an external command, e.g., a Linux/Unix shell command, by entering an exclamation mark followed by the external command. E.g., if I wanted to read the first 55 lines from the file ~/temp/lincoln.txt, I could e nter Vi's colon mode by hitting Esc followed by the colon character. I could then type r, which is the command used to read from another file. But, instead of immediately typing the file name, e.g. :r ~/temp/lincoln.txt, I could use the head utility to read the first fifty-five lines of the file by typing head -55 followed by the path to the file and the file name. E.g.:

:r !head -55 ~/temp/lincoln.txt

If, instead, I wanted to read the last 14 lines of the file, I could use the tail command.

:r !tail -14 ~/temp/lincoln.txt

If I wanted to include specific lines from the other file into the one I'm currently editing, e.g., lines 10 through 15, I could use the sed command as shown below.

:r !sed -n 10,15p ~/temp/lincoln.txt

[ More Info ]

[/software/editors/vi] permanent link

Once You Know, You Newegg AliExpress by

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo