Updated for Mac OS X Mountain Lion 10.8.2.

In order to get page up and page down to work properly under Mac OSX Terminal, as well as the home and end of line (EoL), you need to go to Preferences and then Keyboard and configure the right escape key sequences.

(Note for laptop users: you don’t have the page up/down and home/EoL keys, but you can simulate them by holding the “fn” key and pressing on an arrow key. So these configurations also apply to you.)

In the Terminal Preferences, go to the Keyboard pane and watch the the keys named ‘page up’, ‘page down’, ‘home’ and ’end’. You will see they already exist but are configured to scroll the buffer rather than sending commands to the server you’re connected to.

In order to change this, double click on each of the relevant lines and select Action: “Send string to shell:”

You then need to enter the correct string to send to the shell. This is a multiple key sequence starting with the escape character (which is represented with it’s octal code \033). Here are strings you can use for each key:

As of OSX 10.6.8, the correct fix is:

Key Recommended Escape Sequence Other possible sequences you can try if the recommended doesn't work for you
Home \033OH \033[1~ \033[H \001
End \033OF \033[4~ \033[F \005
Page Up \033[5~
Page Down \033[6~

You can either type these strings on your keyboard (start by presseing Esc) or you can just copy paste the strings from above.

It is worth noting that some of these are already bound to Shift-Page Up, Shift-Page Down, Shift-Home and Shift-End (which are obtained with Shift-Fn-Up/Down/Left/Right on a laptop keyboard). So if you can change your habits slightly you don’t actually need to reconfigure anything.

Also note that some people (including Apple) recommend to use \033[H for Home but this doesn’t actually work in vi or vim. This is why I recommend using \033OH which has worked in any shell or editor for me so far (Mac terminal, Debian terminal, vim, etc.)

If the recommended sequences don’t work for you: please try the alternative suggested here. Also please let us know with which shell or editor you are trying to make them work and which escape sequences work best for you. This way we can order them in order of popularity and possibly give specific advice for specific environments to future visitors of this page. Thank you for your feedback.


Comments from long ago:

Comment from: Jorge Lobo

I have a new macbook (jan2009) and could not find the place to insert escape sequences in the preferences/keyboard options…

I use space to page down, the mac two key page up and down scrolls the window, not the terminal text

2009-03-16 22-57

Comment from: Brian

This worked for me, but was already set up on my 10.5 installation. It was already set up for Shift-page up/page down.

2009-04-18 17-55

Comment from: theman

Try this in the Terminal -> Preferences -> Keyboard widget:

CTRL-A – this is to go to the beginning of a line in Bash (set this to the ‘Home’ key)
CTRL-E – this is to go to the end of the line in Bash (set this to the ‘End’ key)

The first combo will produce: \005
The second combo will produce: \001

Don’t worry, it works in 10.5.x. This also works in the C Shell (‘csh’), so if you are used to using it this will work there as well.

2009-05-12 04-26

Comment from: Mirek Rusin

Works perfectly, thanks a lot!

2009-05-19 12-21

Comment from: Pedro

Great! Works perfect for me!

2009-07-23 17-25

Comment from: Paddy

Wow ….this worked for me too on Mac OSX 10.5.9

2009-07-23 18-35

Comment from: Richard P. Spillane

Alternatively, if this has problems, or is bash specific (doesn’t work in tcsh) you can try this alternative solution:

Terminal->Preferences->[Settings]->[Keyboard]:

Find end, home, pg up, and pg down (the list is alphabetically sorted).

First double click ’end’ and a prompt comes up that lets you remap the key, for ‘Action’ select ‘send string to shell:’ and then click on the entry field.

Press ‘Esc’ which emits ‘\033’ then type ‘[4~’ after it, so the text field should now show: “\033[4~”. Click ‘OK’.

Now you should be able to remap the other keys if you know the key codes, by pressing ‘Esc’ to emit \033. The codes for each of the keys are:

end \033[4~
home \033[1~
page down \033[6~
page up \033[5~

You will want to add ‘Shift-pgdwn/pgup’ to ask the buffer to scroll up and down. Do this by clicking ‘+’ and adding ‘pg down’ with the ‘Shift’ modifier and then select ‘scroll to next page in buffer’, and do the opposite for pg up.

I believe this is the optimal fix.

2009-07-24 07-46

Comment from: Jaap Blom

Thanks a bunch these are indeed the optimal settings!

2009-08-27 15-29

Comment from: mihai

I’ve found that the home and end keys don’t work with bash, but they work with mc (through ssh). To fix them for bash, I used this inputrc fix: http://linuxart.com/log/archives/2005/10/13/super-useful-inputrc/

Cheers!

2009-09-24 00-24

Comment from: Alex

I tried doing this but it didn’t work (OSX 10.5.8). Instead of going to the end of the line, I get a “BEEP” and a tilde (~) appears in the term window.

Would love to get this working if anyone knows off the top of their head what I’m doing wrong.

2009-11-06 17-54

Comment from: Warren Houghteling

You’re my hero! Not having these keys work as I’m used to in a terminal window was driving me crazy.

2009-12-08 19-47

Comment from: Davis Chord

Thanks for the heads-up on this!

2010-01-22 20-19

Comment from: norman

Try
Key Escape Sequence
Home \033OH
End \033OF

The O’s are captital as in OOPs, they are not zero’s. This is straight from termcap for xterm and works for both the bash shell and vi/vim

2010-01-27 14-48

Comment from: Adub

Great! Thanks for this. I had to copy and paste the text but it worked flawlessly after that.

Thanks again!

2010-02-02 18-40

Comment from: C

Thanks!!! The optimal setting above was my solution since i come from *nix and work alot with vi. This WAS driving me crazy. No more, yes!

2010-02-17 21-17

Comment from: the donco

norman’s comment with the OH/OF sequences finally worked for me on:
Mac OSX 10.6.2
17" Macbook Pro (built-in keyboard)

Also works via Synergy client (on Mac), connected to [Ubuntu] Linux (running server) w/Microsoft Natural Keyboard Pro

Everything works about right….except in terminal, where the command[/alt] and ctrl keys are flipped – I flipped them in the mac preferences so that ctrl+x/c/v would work without a bizarre reach, and now I have to “alt+c” running commands to kill them. Because I do most my terminal work on Linux, this is [barely] acceptable, but any insight on how to have the best of both worlds would be appreciated – i.e.: ctrl+c/copy in email and ctrl+c/cancel in terminal.

Regards,
-the donco

LiveCloud.com - Blog. Get Social. Take Control.
LiveCloud is a Social Strata Venture

2010-02-24 20-36

Comment from: Martin Kovachev

Thank you SOOOOOOOOOOOOOOOOOOOOO much!!!!! :))

I was getting crazy scrolling char by char until i get to the end of the console line…

2010-03-21 18-27

Comment from: carnator

Thank you Francois! however in OSX (10.6.2) the end/home keys did not work the same way for the terminal and vi.

@norman [Visitor] your bindings worked perfectly!

2010-03-26 00-27

Comment from: James

Fn+Shift+Up = Page Up

Fn+Shift+Down = Page Down

2010-06-02 19-27

Comment from: Neil

Norman (visitor)’s mappings also worked for me on a macbook pro with OS 10.6.3.

What I really want is for command-left / command-right to do that, like they do in every other mac app.

2010-06-16 13-25

Comment from: Doug

You are the man!!! Thanks worked like a charm! Mac OS X 10.6.4

2010-09-21 16-22

Comment from: Keith

Thanks!

Now… how the hell can I get these keys to work in textedit. AND Home and End keys do not work in my browser address bars. Is there a modifier? This is driving me nuts. Works fine on windows if I want to jump to the end or beginning of a url in the browser address bar.

2010-12-17 21-06

Comment from: Marius

If you find that this does not work on your command-line, keep in mind that your shell may just not know what to do with those command, but all those key sequences work pretty well in VIM editor.

For you to tell your shell how to interpret them, you either need to create a matching custom termcap entry, or use an existing one with a lot of matching capability.

For instance, Home and End does not work for me on my terminal when using vt220, xterm, or color-xterm… but when I do a “export TERM=linux” it works.

PgUP and DOWN still don’t have a function for me on my command-line, but that’s ok, since I don’t know what it is supposed to do anyway.

2011-01-26 20-57

Comment from: Colin

Actually it should be

Fn + Up Arrow = Page Up
Fn + Down Arrow = Page Down

2011-02-04 00-49

Comment from: Paul

Thanks mate! worked perfectly!

2011-03-05 06-07

Comment from: Tim

Thanks alot mate. I’ve been looking for this desperately. As a first-time Mac user, I didn’t know the Fn + direction keys were the Home, PageUp, End, PageDown keys.

2011-03-13 10-46

Comment from: Michael

Good stuff mate, worked a treat. Vim over SSH is now useable. :D/

2011-03-29 22-07

Comment from: Shez

holding function and shift and pressing down also works.

2011-04-11 10-00

Comment from: Mike Schroder

Thanks!~

This is still being found, and was very helpful.

2011-04-19 03-48

Comment from: Kate

Thank you! The Control A & E short-cut did the trick!

2011-04-21 23-56

Comment from: Seth

thanks, that worked. I live in the terminal and the home/end keys were killing me.

2011-11-03 00-28

Comment from: steve

Thanks……worked very well on Lion for me… it was driving me nuts, Ubuntu seems to include a lot of VIM functionality….

2011-11-28 04-55

Comment from: kyle renfrow

In order to get all four keys working I had to use a combination of the answer and comments:

for home and end: select ‘send to shell’ and press ctrl + a, and ctrl + e respectively.

for page up/down: select ‘send to shell’ and press the escape button followed by [5~ and [6~ respectively.

home \001
end \005
page up \033[5~
page down \033[6~

2011-12-01 05-45

Comment from: Rohaiza

I try all the above suggestion, but does not worked to my macbook pro (parallel)..any hints? TQVM

2012-01-08 18-56

Comment from: Kyriacos

Thanks for posting! Home and End keys do not work, but at least PageUp and PageDown are working great. (Mac OS X 10.7.2)

2012-01-10 16-31

Comment from: Tim

As of OSX 10.6.8, the correct fix is:

Terminal->Preferences->Settings->Keyboard->+

Key: home or end
Modifier: none
Action: send string to shell

Then for the sequence PRESS ESCAPE (or CTRL-[ ) to get a “\033” and then type “[H” or “[F” for home and end so the full thing is “\033[H” or “\033[F”.

It’s worth noting that these are already bound to Shift-Home and Shift-End (which are obtained with Shift-Fn-Left and Shift-Fn-Right). So if you can change your habits slightly you don’t actually need to reconfigure anything.

Francois: it would be nice if you updated your post to reflect this, since it is the first result in Google and I doubt many people are going to read down the comments this far.

2012-09-25 15-24

Comment from: François Planque

I updated for Mountain Lion and I also listed all possible escape sequences that were recommended here. I highlighted the ones that seem to work everywhere.

2012-09-26 13-41

Comment from: nodmonkey

You may also want to include a note for laptop users who don’t have Home/End keys and want to use CTRL-leftarrow and CTRL-rightarrow to achieve the same cursor movement.

The OS keyboard shortcuts have CTRL-leftarrow and CTRL-rightarrow mapped to “move left a space” and “move right a space” respectively under System Preferences > Keyboard > Keyboard Shortcuts > Mission Control. When you only have one space setup (the default), it is not easy to tell this is what the keys do.

These shortcuts overrule the Terminal keyboard settings, so you need to change or disable them if you want to use those key combinations to navigate in Terminal.

2012-10-12 11-56

Comment from: Raj

Thanks a lot..Worked for me :)

2012-10-30 19-16

Comment from: Michael

Another success. Thanks!

2012-12-17 06-42

Comment from: Tennessee Leeuwenburg

2011 model macbook air. Success. Thank you very much!

2013-01-10 05-05

Comment from: San

Thanks you for this. I love that you wrote this up with the alternatives and explanations and it was only a google search away!

Thanks.

2013-02-06 20-26

Comment from: Troy

Thanks heaps for this, worked a treat.

2013-04-17 16-36

Comment from: Tiago Tix Carvalho

You sir, deserve a cake! Thank you!

2013-05-23 18-33

Comment from: Can Kavaklıoğlu

Thanks a lot!!! Finally home/end!!!

2013-08-02 10-52

Comment from: KBK

Very nice, worked with emacs -nw

2013-08-16 02-35

Comment from: Pierre

Really thanks!!
Worked on mavericks connecting to another server and using vi… finally !!! =)

2013-12-30 16-10

Comment from: ztwo79

thanks . It’s really work fine for me

2014-09-02 17-01

Comment from: Angelux

Thank you very much, didn’t know I could use fn key to get home/end functionality on a laptop.

2016-01-11 00-03

Comment from: salvis

Thank you!

iTerm2 has replaced Terminal and allows me to actually configure these keystrokes. Thanks to iTerm2 I can use Midnight Commander (mc) on the Mac!

2018-03-16 15-45

Comment from: Cassiano

Mac OS X 10.11.6

Home: \033[H
End: \033[F

Page Up and Page Down: was working with fn + left arrow key, fn + right arrow key

2018-03-26 19-45