6

So Gilles mentioned in the chat room that:

less '+>' /path/to/file

can be used with less in order to view a file in reverse. I've never seen it before.

I'm curious - what's going on with the +> operator in the title? Is this less specific syntax? Can it be used otherwise? Are there similar operators I should probably know about?

mikeserv
  • 58,310
  • 1
    this command is effing dangerous in the hands of the sloppy. I ran it without the quotes around +> and ended up clobbering a file – iruvar Apr 07 '14 at 17:11
  • 2
    Well, warnings we give based on our own mistakes are sorely bought, but theyre definitely worth a vote. – mikeserv Apr 07 '14 at 17:13
  • 2
    I would say "to view the file from the end" rather than "in reverse" -
    `</path/to/file tac | less`
    
    

    would be "in reverse" to my ears. Also

    `less +G /path/to/file` 
    
    

    should be promoted over '+>" since it has the same effect with fewer keystrokes and less danger of file clobbering for neophytes.

    – D McKeon Apr 07 '14 at 22:08

2 Answers2

4

A long long time ago, several programs understood the command line argument +N (e.g. +42) to mean “start at line N” (not “skip N lines”, but “skip N-1 lines”, because the first line is line 1). The oldest trace I can find is in tail in Unix V7, but I don't know for sure that this is where the usage started. An important program with the same option is more. Other programs that inherited this syntax from more include less, vi, and most other text editors today.

Over time, this syntax was progressively generalized. By 2.8BSD in 1980, more also supported +/PATTERN to start at the first line containing the specified pattern. By 2.10BSD in 1985, vi supported +COMMAND to run an arbitrary command on startup, generalizing +/PATTERN (not all versions of vi support this, and for example POSIX only specifies -c COMMAND, but it's supported by nvi, Elvis and Vim). The commands are ex commands, i.e. what you can type after :.

Less also supports the generalized +COMMAND (I don't know since when, but it was in the 20th century). In the case of +, it's as if you'd typed the characters as input after less starts, except in the special cases +N which is equivalent to +g and +/PATTERN where you can omit the final Return.

less '+>' /path/to/file is an example of that usage: execute the command > (go to the end of the file) after startup. You can also write less +G since G and > are synonyms. You can combine commands, for example less $'+>?foo\r' /path/to/file to go to the last occurrence of foo.

Another way to make Less execute commands on startup is to stuff them in the LESS environment variable: LESS='+>' less /path/to/file. This is useful if less is invoked by another program such as man (example: Reading and searching long man pages).

  • Actually less $'+>?foo\r' /path/to/file does not go to the last occurrence of foo if foo is present on the last screen because (according to man less) The search starts at the line immediately before the top line displayed. Btw, what's the purpose of $ here? – Piotr Dobrogost Jan 29 '15 at 08:36
  • @PiotrDobrogost This seems to be an error in the manual: ?foo searches backward from the last line on the screen. Experimentally, it only fails to go to the last occurrence if the last occurrence is on the last line. I don't know how to fix this. The purpose of using $'…' rather than '…' is to make the shell interpret backslash escapes: the shell sees \r and converts it to a carriage return character in the argument passed to less. – Gilles 'SO- stop being evil' Jan 29 '15 at 11:28
  • From what I see it also fails if the last occurrence is on the next to the last line. I have version 458 of less. – Piotr Dobrogost Jan 29 '15 at 14:00
3

It's less specific (you can guess this with the quotes around the operator to avoid the shell interpreting it). The + argument tells less to apply this command after opening the file, in this case > which is the keystroke for going to the end of the file. You can try this out by opening some (long) file with less and hitting >. +> is just a shortcut for this. A fairly more common use-case you'll find in the wild is calling less +## /path/to/file where ## is a line number you want to be displayed by less.

At least +## also works for vi and emacs.

Andreas Wiese
  • 10,400
  • So '+' passes keystrokes to less? I mean i knew was escaped for a reason - as in - i knew it wasnt shell syntax, but often similar types of programs share common operators and such. – mikeserv Apr 07 '14 at 17:03
  • 2
    Actually a command. Have a look at man less | less '+/\+cmd'. ;) – Andreas Wiese Apr 07 '14 at 17:06
  • man answers are my favorite answers – mikeserv Apr 07 '14 at 17:24
  • My sarcasm sense tickles. I was proud of this since the exact command line also helps demonstrates what's going on. When thinking about it further, commands and keystrokes in less are rather equal. – Andreas Wiese Apr 07 '14 at 17:31
  • 1
    No sarcasm - i must have said it in at least one other comment somewhere - look at the timing on my comment there and your first upvote. man answers are my favorite answers. And if you look at my own answers a lot of them are nothing but codeblocks at all. – mikeserv Apr 07 '14 at 17:32
  • Oh, that's an opinion I often miss here. ;) You might additionaly consider accepting the answer if it helped you (it's not just reputation points: it might help others with a similar question to find the answer more easily). – Andreas Wiese Apr 07 '14 at 17:36
  • I am considering it - but the question's only 30 minutes old or so. Its a shame when we miss out on alternate information - if not necessarily better - just because the discussion dies out. If theres any to be had here id rather not squash it so soon. – mikeserv Apr 07 '14 at 17:39
  • That's, of course, perfectly reasonable and fine with me. – Andreas Wiese Apr 07 '14 at 17:40
  • Shouldn't it rather be less '+G' /the/file ? G also goes to the end of file (and there is less risk of clobbering the file you want to read!) ? (I can not test right now... no "less" at hand) – Olivier Dulac May 13 '14 at 15:12
  • Yes, this also works (and you could even omit the quotes). Would probably be the better way. – Andreas Wiese May 13 '14 at 16:18