384

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?

7 Answers7

418

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
  • 7,184
  • 13
    :e [file] Examine a new file. -- i.e. open a new file while less is open – Jellicle Jun 30 '14 at 15:30
  • 87
    +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 Aug 05 '14 at 18:31
  • 5
    If a number N is specified - how to specify this number (can't find answer in the manpage)? – Piotr Dobrogost Nov 25 '14 at 16:53
  • 17
    @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 Nov 25 '14 at 17:35
  • 4
    @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 Aug 26 '17 at 19:42
  • 8
    @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 Sep 21 '17 at 15:29
  • @T.C.Proctor quiet you -- you're only validating Nathan by pointing out that I too came across this question – ardnew Sep 21 '17 at 18:39
  • @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 Feb 21 '18 at 10:52
  • 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 Jan 17 '19 at 22:02
50

Type :n and :p.

jtbandes
  • 8,870
37

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

  • 15
    Teach a man to fish. I didn't know you could :anything, but now I know how to look using :h... – Mitch Kent Oct 15 '14 at 09:51
  • 8
    Just 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 Sep 27 '17 at 21:24
  • 1
    Except the :h is very misleading. It implies you must type :2 to get to the next file. :n * Examine the (N-th) next file from the command line. – Marc Nov 18 '23 at 10:34
31

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
  • 3,076
  • 2
    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 Apr 27 '19 at 20:24
7

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
  • 21,787
3

I find the :n binding cumbersome, so I often use the more command instead. (The name less is a wordplay on more, after all.)

more just uses the space bar to advance to the next file, and prints a header before each one. Example:

$ more *.txt
::::::::::::::
a.txt
::::::::::::::
Example file A
::::::::::::::
b.txt
::::::::::::::
Example file B
--More--(Next file: c.txt)

For one line or really short files, I usually use grep . *.txt, because it will prefix each file with the file name:

$ grep . *.txt
a.txt:Example file A
b.txt:Example file B
c.txt:Example file C
remcycles
  • 131
  • Does grep has a color option? – alper Jun 23 '23 at 18:48
  • Yes. I use the following Bash alias with Gnu grep 3.11 and the filename and contents are automatically highlighted with different colors: alias grep='grep --colour=auto'. – remcycles Jun 26 '23 at 19:21
0

Get help about navigation

While the less is opened press h key to open summary of less command and locate section CHANGING FILES:

                        CHANGING FILES

:e [file] Examine a new file. ^X^V Same as :e. :n * Examine the (N-th) next file from the command line. :p * Examine the (N-th) previous file from the command line. :x * Examine the first (or N-th) file from the command line. :d Delete the current file from the command line list. = ^G :f Print current file name.

Commands marked with * may be preceded by a number, N.

For more detailed and verbose help run man less

How to open multiple files in less

# Name each file name as params
less file1.txt file2.txt file3.txt

Group of files sing wild character

less file.txt less .txt

Examples of navigation

While the less is opened

  • Where I am

    • = or :f or Ctrl+G will print you the status of what file is opened, order of the file in the list of total number of opened files with name of next file, etc, aka file.txt (file 1 of 7) (END) - Next: file1.txt
  • Change by N-th file

    • by single file
      • :n to change to next file
      • :p to change to previous file
    • by N-th next file
      • 2:n to change by 2 next files
      • 2:p to change by 2 previous files
  • Change to the N-th file

    • to N-th file
      • 4:x to change to 4th file in the list
    • to the first file
      • :x to change to first file in the list, ie without any number, or
      • 1:x to change to first file in the list with the index number 1
    • to the last file
      • there is no direct command to navigate to the last file
      • use the "Change to the N-th file" command by finding total amount of oepened files from the status = aka (file 4 of 7) so the 7th file is the last one, then use:
      • 7:x to change to next 7th file, ie the last file in our list of 7 files
  • Refresh the file

    • Note: most refreshes works only when there are new lines in the refreshed file.
    • :e without file name do the file re-examination
      • Since re-examination is not working for me I user a workaround to get file refreshed by navigating in the list of files as the file is refreshed each time it is displayed:
      • use :n and :n to move next and previous or number:x to N-th file navigation
    • Shift+R Repaint the screen, discarding any buffered input.
    • Shift+F Scroll forward, and keep trying to read when the end of file is reached.
    • > or Shift+G go to the end of the file with refresh
  • Open new file

    • :efilename
    • Examine: file
      • where file can be filename aka file4.txt or file name pattern aka *.txt or any path to the file(s) aka ~/file*.
      • you can use TAB key to complete the path/filename
      • press TAB to loop between similar file names aka fileTABTAB loops files starting with file1.txt file2.txt file3.txt ...
      • use double quotes for filenames containing one or more spaces
      • percent sign (%) and pound sign (#) used as filename has special meaning.
    • Note: :e may be disabled while running less in "secure" mode, ie the environment variable LESSSECURE is set to 1 or less is compiled in "secure" mode.
  • Close file

    • :d delete, ie close current file from the list of opened files
ino
  • 201