less command g vs p option
In less
for navigation purposes according with this tutorial
indicates:
g Go to the first line in the file.
p Go to the beginning of the file.
I tested both, and of course the result is the same (of course using G
to go bottom) and testing each one.
But just at a first glance if g
and G
do the opposite to each other and they are enough to go to the first line (top) and last line (bottom) respectively – so why there is the p
option if it does the same as g
?
He, this is mischaracterizing what these commands actually do. p
is for "percentage".
Try typing 20p
and you’ll jump to 20% of the file length. Nifty!
20g
works too, but it goes to the twentieth line.
Simply typing g
or p
just implies 0g
or 0p
; because the zeroth line and the zeroth byte are both the file’s beginning, that works out as the same.
You can test this rather easily; I’m assuming you’re using zsh
:
#!/usr/bin/zsh
(for i in {1..1000}; echo $i) | less
will display 1000 numbered lines, and 33g
will jump to line 33, but 33.3p
will jump to line 333 🙂
It says in the less
help (less
a file, press h
) that you can go to the percent mark of the file with p
. For example, you can do 50p
in the less
prompt and it will go to the 50% point (halfway point) of the file. I think the reason why it goes to the start of the file is because you haven’t supplied a number (for where to go) before the p
, so it just goes to the start.
For example:
$ less example.txt
example.txt:
This is the start
This is the middle
This is the end
When I do 50p
:
This is the middle
This is the end
~
~
~
~
~
~
When I do p
:
This is the start
This is the middle
This is the end
~
~
~
~