Write lines to a file from less

While in vim I can write a range of lines from the file I’m viewing to another file. For example,

:1,10 w outfile.txt

will write lines 1 through 10 to outfile.txt.

Can I do the same while I’m viewing a file using less?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

10Gmm1G|mcat > outfile.txt

(typed within less) seems to work as long as the input doesn’t fit in one screen (in which case all the input ends up in outfile.txt for some reason).

  • 10G brings you to line 10
  • mm sets the m mark there
  • 1G (same as g in this case) brings you to line 1
  • |m pipe from the current line to mark m
  • cat > outfile.txt: that’s piped to that command.

Method 2

You could use view which is the same as vi -r


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x