I have some long log files. I can view the last lines with tail -n 50 file.txt
, but sometimes I need to edit those last lines.
How do I jump straight to the end of a file when viewing it with nano
?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityOpen the file with nano file.txt
.
Now type Ctrl + _ and then Ctrl + V
Many editors support the +NNN
option on the command line to jump directly to line NNN. Luckily for you, nano
appears to jump to the end if the line number given is past the end of file, so you could use something like:
nano +999999 file
That also works in joe
, but not in, e.g. less
or VIM, they complain about going past EOF. (at least the ones on my system. less +G file
and vi +$ file
work in those.)
Of course something like $EDITOR +$(wc -l file) file
would probably work in most editors, but that's a bit silly and involves reading the file twice.
nano +-1 file
is a superior version of this answer, thanks to unix.stackexchange.com/users/476361/lesha-lyushen (scroll to bottom of page) - his answer deserves more upvotes!
Dec 28, 2022 at 20:16
From the built-in Nano help (^G
):
M-\ (^Home) Go to the first line of the file
M-/ (^End) Go to the last line of the file
So, press Alt+\ to go to the first line or press Alt+/ to go to the last line.
/
is near the bottom of the keyboard and \
is near the top.If you want a command, you could write a function in your .bashrc
or .bash_aliases
to use the line count from wc
:
function nano-end {
# if the file exists, jump to the end
# otherwise, just open an empty nano
[ -f "$1" ] && nano +$(wc -l "$1") || nano
}
Now just type nano-end filename
to open the file to its last line!
In order to go directly to the end of the file in nano, just type: Alt + /. Also, if you wanna jump to a first line: Alt + \
Ctrl+End is working, if you have a recent (compiled) version of nano
editor.
If you don't know, how to do it, you may read Compiling Nano editor with options
Note, that in newer systems, e.g. based on Ubuntu 18.04, there already is such a version.
From the changelog:
... makes ^Home and ^End go to the start and end of the file (on terminals that support those keystrokes) ...
I do not have PuTTY installed, here we have to rely on other claims these key combinations don't work in Windows 10 + PuTTY + SSH + nano > 2.8.1.
On the other hand, I have Cygwin, and Windows 10 + Cygwin + SSH + nano > 2.8.1 works Ok.
OP wants me to add an answer for jumping to the last line in vim
.
ESC + ShiftG will get you to the beginning of the last line.
ESC + ShiftGA will get you to the end of last line and insert mode will be activated.
C-o G
. Not that you would want to do that if you were using vim right...
If anyone still has trouble with this, press Ctrl_, enter a large number (such as 9999999) then Enter.
This command will go to line 9999999; if the file has fewer than 9999999 line then it will go to the last line.
vim
:-Pvim
in a portable way G:
to open command line, than-
,P
and enter. That's what I got: "E464: Ambiguous use of user-defined command". All the best!