Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 9 down vote accepted
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.
share|improve this answer
I had to paste the command at the prompt because typing 10G brought me to the 10th line before I could type the rest, but it did indeed work. – eebbesen Feb 17 '14 at 16:57
@eebbesen, well that was the point. See my edit – Stéphane Chazelas Feb 17 '14 at 17:20
Thank you for clarifying @StephaneChazelas (as well as for your initial answer) -- it is now clear. – eebbesen Feb 17 '14 at 17:43
genius to the rescue again :) – Ramesh Sep 5 '14 at 15:02

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

share|improve this answer
That will work, but for large files I prefer to use less -- it is my understanding that vi causes the whole file to load at once. – eebbesen Feb 17 '14 at 16:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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