96

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?

payloc91
  • 2,329
  • 4
    Try out vim :-P – Hunter.S.Thompson Jan 31 '18 at 09:55
  • @Hunter.S.Thompson That does not work! You are using a user defined command! But now try out vim in a portable way G – Volker Siegel Jan 31 '18 at 10:17
  • 2
    @VolkerSiegel I can't tell whether you're being sarcastic, but it's not a user-defined command. It's an emoticon. We used to use these things before someone let Unicode smoke weed. – Maya Jan 31 '18 at 16:19
  • Sarcastic, of course! I tried your command: : to open command line, than -, P and enter. That's what I got: "E464: Ambiguous use of user-defined command". All the best! – Volker Siegel Jan 31 '18 at 16:26

8 Answers8

126

Open the file with nano file.txt.

Now type Ctrl + _ and then Ctrl + V

galoget
  • 349
payloc91
  • 2,329
  • 55
    You can also do Ctrl+W, Ctrl+V - depending on what is more comfortable for your hands :) – psmears Jan 31 '18 at 18:00
  • @psmears that depends on the version. v2.4 has your shortcuts. The current version v4.0 has the shortcuts Marko mentions. Very annoying. – Mausy5043 Apr 07 '19 at 14:46
  • 1
    @Mausy5043: Versions up to at least 2.4 have both sets of shortcuts. Version 4.0 seems to have removed Ctrl+W, Ctrl+V, which is annoying; it does have Ctrl+W, Ctrl+T, Ctrl+Y which is at least just on the letter keys, but is longer :-( – psmears Apr 07 '19 at 17:03
  • Those shortcuts do not work very well in Windows / Putty – Eric Grange May 16 '19 at 08:36
  • I am using nano editor with Italian keyboard. Maybe this is the reason, however in my environment the shortcut is Ctrl + - – Kar.ma Dec 13 '19 at 15:10
  • On ubuntu gnome terminal this just decreases the text size.... – Thufir Feb 20 '20 at 20:41
36

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.

ilkkachu
  • 138,973
  • 1
    Or with nano already open, use Alt/ + G then enter 99999 – Eric Grange May 16 '19 at 08:36
  • 1
    nano +-1 file is a superior version of this answer, thanks to https://unix.stackexchange.com/users/476361/lesha-lyushen (scroll to bottom of page) - his answer deserves more upvotes! – pre-kidney Dec 28 '22 at 20:16
29

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.

  • This would be the equivalent of gg (start) or G (end) in vim.
  • This also states that Ctrl+Home or Ctrl+End should work, but that's never worked for me they seem to work natively on console/desktop but not via SSH using PuTTY.
  • The way I remember this is that / 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!

ghaberek
  • 409
  • "The way I remember this is that / is near the bottom of the keyboard and \ is near the top" >> This is very good – Scransom May 08 '20 at 02:26
  • 2
    As of now, ctrl+w ctrl+v conflicts with WSL 2's default keybindings. alt+/ works without a hitch. – Culdesac Sep 01 '20 at 21:20
8

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 + \

6

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:

2017 April 12 - GNU nano 2.8.1

... makes ^Home and ^End go to the start and end of the file (on terminals that support those keystrokes) ...

PuTTY vs Cygwin

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.

2

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.

  • If you want to do this in insert mode, do C-o G. Not that you would want to do that if you were using vim right... – Maya Jan 31 '18 at 16:21
2

No need to use +999999, use instead -1, it works perfectly

nano +-1 file
0

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.

Stephen Kitt
  • 434,908