less command with multiple files: How to navigate to next/previous

283

36

I just found out I can use less with multiple files. less status line tells me (END) - Next: file2.txt

But how do I navigate previous/next from less?

Jesper Rønn-Jensen

Posted 2009-11-16T09:54:16.117

Reputation: 5 450

Answers

313

We read in the manpage:

       :n     Examine  the next file (from the list of files given in the com‐
              mand line).  If a number N is specified, the N-th next  file  is
              examined.
       :p     Examine the previous file in the command line list.  If a number
              N is specified, the N-th previous file is examined.

Stephan202

Posted 2009-11-16T09:54:16.117

Reputation: 5 946

9:e [file] Examine a new file. -- i.e. open a new file while less is open – JellicleCat – 2014-06-30T15:30:28.817

66+1 stack exchange is faster than manual grep through man to find the right part when you're not sure how it's described. – Nathan – 2014-08-05T18:31:38.157

4If a number N is specified - how to specify this number (can't find answer in the manpage)? – Piotr Dobrogost – 2014-11-25T16:53:22.550

15@PiotrDobrogost: Good question. I had to fiddle a bit with it myself. Turns out the number precedes both the colon and the n or p. E.g., 3:n moves one to the third-next file. – Stephan202 – 2014-11-25T17:35:13.610

3@Nathan surely a simple search in the less man page (e.g. /next or /next file) is faster than doing a web search and scouring for this answer...? – ardnew – 2017-08-26T19:42:07.570

6@ardnew You are very unlikely to get any upvotes on that comment - anyone who agrees with you is unlikely to come across this question! – T.C. Proctor – 2017-09-21T15:29:15.090

@T.C.Proctor quiet you -- you're only validating Nathan by pointing out that I too came across this question – ardnew – 2017-09-21T18:39:49.120

@ardnew You are right (/next file gets faster to the point). However, current upvote score (of 39:1) is a quite accurate measure of 'very unlikely' qualification in @T.C. Proctor's comment. – tishma – 2018-02-21T10:52:54.230

Well, Google tends to be smarter than grep. While with somewhat short and simple manpages grepping gets you there in no time, with longer ones like bash... It took me long time to figure out how to navigate it efficiently. – EvgEnZh – 2019-01-17T22:02:14.757

36

Type :n and :p.

jtbandes

Posted 2009-11-16T09:54:16.117

Reputation: 8 350

28

Found out from :h (help window) that I can use :p (for previous) and :n (for next)

Jesper Rønn-Jensen

Posted 2009-11-16T09:54:16.117

Reputation: 5 450

11Teach a man to fish. I didn't know you could :anything, but now I know how to look using :h... – Mitch Kent – 2014-10-15T09:51:46.710

6Just to clarify, you only type h for help, without the colon (the colon is already there). However, you have to type :n or :p with an explicit colon to go to next/prev file. – wisbucky – 2017-09-27T21:24:50.487

18

Note: you actually have to type the : for these commands (even though there is a colon visible already).

:n jump to next file
:p jump to previous file
:x jump to first file

3:n jump 3 files ahead
3:p jump 3 files back
3:x jump to 3rd file

:f print current file name/info (helpful if you forget where you are)

wisbucky

Posted 2009-11-16T09:54:16.117

Reputation: 1 522

Piggy-backing off of this answer, because it took me closest to what I was looking for: If you want to "easily" jump to the LAST file in your list, first use :f to see how many files are in your list (for instance '(file 1 of 99)'), then just type 99,:x as described by @wisebucky and you'll immediately jump to it. Unfortunately, there doesn't seem to be a shortcut for jumping to the last file, like you can so easily jump to the first with :x alone. – J.M. Janzen – 2019-04-27T20:24:12.587

2

Not strictly an answer for that question but maybe someone can find this useful nevertheless.

If the number of files is reasonably small, one could use vim for that:

vim -O files*

In this way all the files are displayed at once by splitting the screen automatically.

(Use -o to split horizontally.)

Some basic vim survival commands for this use case:

  • Ctrl-W + arrow selects an adjacent split;
  • / search the buffer;
  • :qa exits vim (!!!).

cYrus

Posted 2009-11-16T09:54:16.117

Reputation: 18 102