ChatGPT解决这个技术问题 Extra ChatGPT

OS X Terminal shortcut: Jump to beginning/end of line

I know that I can jump to the beginning and end of a line inside the OS X terminal with Ctrl+A and Ctrl + E. But I'm so used to jumping with Cmd+Right arrow and Cmd+Left arrow from my editor, that I would love to use these shortcuts for the terminal too.

I haven't found a solution with Google, so maybe here someone can help.

Update

As in the comments, I tried the solution from How to move the cursor word by word in the OS X Terminal

Unfortunately, the Terminal settings seems to not include Cmd as a Modifier Key, as you can see here:

https://i.stack.imgur.com/nHqff.png

can't you adapt this ?
I've updated my question, see above
Hmmm, I don't think so. I tried it, it works with Option + right-arrow, but not with Cmd + right-arrow
You're right, my fault, I've got hard days on a PC
More useful shortcuts: stackoverflow.com/a/5542996

h
hygoh2k
fn + shift + leftArrow = goto beginning of line
fn + shift + rightArrow = goto end of line

these work for me


I didn't need to hold the shift key. fn + leftArraw or fn + rightArrow worked for me. Maybe it helps!
@BharatGupta fn+leftArrow works in some terminal applications (e.g. VI), but doesn't work in the terminal command line.
Using iTerm2, fn+left and fn+right work without shift. With shift, they don't work. ctrl+a and ctrl+e also work.
I'm a new Mac user and I'm disgusted by how hard everything is in Mac vs Windows! The hardware is good, but the software is horrible (i.e. not user friendly.)
If you are new on Mac think simple, coz when you use windows for long term, you are used to always use complicated path... with Mac you have to think very simple... and most of time you got it. It was like that for me when I switch for PC to Mac
e
elad silver

In the latest Mac OS You can use shift + home or shift + end


how do yo have a HOME / END key. I only have arrows on my keyboard
I use an external keyboard
@ddzzbbwwmm fn+left=home, fn+right=end, fn+up=pageup, fn+down=pagedown
A
Aaron Wallentine

I use a handy app called Karabiner to do this, and many other things. It's free and open source.

It's a keyboard remapper, with a lot of handy presets for many common remaps that people may want to do.

As you can see from the screenshot, this remap is included as a preset in Karabiner.

https://i.stack.imgur.com/5CESo.png

Hope this helps. Happy remapping!


Be aware, Karabiner doesn't work on macOS Sierra at the moment.
Good to know; I haven't updated. Hopefully Karabiner will be updated before too long! : )
Now it supports Sierra and even higher. Here's the link to some pre-defined rules from Karabiner website.
How save is it to use Karabiner?
@jaquinodev the Navigation in Terminal Apps shortcuts config seems to have been replaced with Keyboard shortcuts for Terminal apps which does not have the shortcuts needed for this question. Since I still have the configs laying around I uploaded it to GitHub (with instructions) here.
A
Abhishek saharn

As setup in the terminal using vi:

The Home button on a Macbook Pro keyboard: Fn + Left Arrow.

The End button on a Macbook Pro keyboard: Fn + Right Arrow.


This question was about the Mac OS terminal, not vi. vi is an application that can be run inside a terminal.
C
Community

Here I found a tweak for this, without any third party tool. This will make the following shortcut to work:

 fn + right: to go to the end of the line.
 fn + left: to go to the beginning of the line.

Open terminal preferences.(cmd + ,).

Go to your selected theme and then to the keyboard tab.

https://i.stack.imgur.com/oI7kM.png

And add a new entry as following.

That's all. Now close and check. Hope it helps.

EDIT: Refer to the comment by @Maurice Gilden below for more insights.


That will just enter "001" or "005". The backslash is missing and I could not find any way to manually add it. What you can do however is, press Ctrl+A for "\001" and Ctrl+E for "\005" while inside the text box.
This should be the absolute correct answer. All of the 3rd party apps are either broken or no longer maintained and fn+shift+leftArrow no longer works on Mojave.
This worked brilliantly. However, I selected "End" rather than "Page Down" for the fn + right arrow mapping.
Thank you brethren Abhishek, Maurice, Salyangoz and nedblorf, you the best. The only thing which left to figure out for me is to enable this functionality when working from terminal apps like the MidnightCommander (espetially when panels are switched off with the Ctrl+O).
On my windows keyboard, I now have pos 1 as 'go to begin of line' and end as 'go to end of line' which is what I want. I could enter backslash, but not one backslash but two so I followed Maurice.
E
Edwin Feliz

Control + A for goto the beginning and Control + E to goto the end


Stupid but at least it works. Thank you!
Great, that works but how can I change the key mapping?
A
Abhishek saharn

For iterm2:

Go to Profiles / Open Profiles / Keys

Set Cmd + left_arrow_key to Send Hex Code 001

Set Cmd + right_arrow_key to Send Hex Code 005

This allows you to use Cmd + arrow_keys to move from beginning to end of the line.


as per @bhekman's comment: "Using iTerm2, fn+left and fn+right work without shift. With shift, they don't work. ctrl+a and ctrl+e also work."
I don't see cmd as a modifier key option, same as in the original question
@electrovir just hold it down and press the arrow keys
See "Unfortunately, the Terminal settings seems to not include Cmd as a Modifier Key, as you can see here" in the original question. I have the same problem. My comment was about creating the key setting, not executing it.
@electrovir it sounds like you are in the wrong place. there is no modifier. open iterm. go to preferences. go to profiles. press the "+" button to the left of "Load Preset". There are two fields: "Keyboard Shortcut" and "Action" for keyboard shortcut hold command press the left arrow key. then for action -> "send hex code -> 001 "
n
noufalcep

I am not sure if this will work for you (I still use OS 10.8), but these work for my terminal:

home = move cursor to the start of the line
shift+end = move cursor to the end of the line

alt+leftArrow = move one "word" to the left
alt+rightArrow = move one "word" to the right

Hope this helps!


In 10.12 Sierra, it looks like it's shift + home to go to the beginning. the others listed here work as listed.
T
Tejas Niturkar

For latest mac os, Below shortcuts works for me.

Jump to beginning of the line == shift + fn + RightArrow

Jump to ending of the line == shift + fn + LeftArrow


A
Akshay Paliwal

fn + leftArraw or fn + rightArrow worked for me!


J
Jacob Marshall

You could download Better Touch Tools. It's an app that allows you to make custom key-bindings and shortcuts over your entire system or individual apps. Using it, you could make a shortcut in the terminal that emulates ctrl-a/ctrl-e whenever you press cmd-left/cmd-right, respectively. I definitely recommend it! I've been using it for years and I have over 50 shortcuts spread across several different apps.


Unfortunately, your link is broken
Thanks for the heads up! I fixed it.
Hey @Jacob what would be the 'triggered predefined action' to go beginning or end of the terminal command?
Oer, you should use the "Trigger Other Keyboard Shortcut" option, and enter Ctrl-A to go to the beginning of the terminal command, and Ctrl-E to go to the end.
This software seems to be outdated. It is not maintained anymore.
A
Abdur Rahman

Text Navigation Shortcuts

Jump to beginning of a line – Command+Left Arrow

Jump to end of a line – Command+Right Arrow

Jump to beginning of current word – Option+Left Arrow

Jump to end of current word – Option+Right Arrow

Jump to beginning of all text – Command+Up Arrow

Jump to end of all text – Command+Down Arrow

Text Selection Shortcuts

Select text to beginning of a line – Shift+Command+Left Arrow

Select text to end of a line – Shift+Command+Right Arrow

Select text to beginning of current word – Shift+Option+Left Arrow

Select text to end of current word – Shift+Option+Right Arrow

Select text to beginning of all text – Shift+Command+Up Arrow

Select text to end of all text – Shift+Command+Down Arrow


A
Ajith

in iterm2

fn + leftArraw or fn + rightArrow

this worked for me


The question was about the Mac OS X terminal
P
Prakash P

This worked for me Option + left-arrow or Option + right-arrow for moving the cursor to the start or end of the line.

Mac Os Version: Catalina


Worked for me using Mac OS Version: Monterey, thanks!
K
Koushik Das

As per the official website of Apple

control + A - Move to the beginning of the line

control + E - move to the end of the line

terminal shortcuts by Apple's official site


c
cnstntn.kndrtv

i have the same issue on MacOS 12 (Monterey) - want to go to beginning and end of the line with fn + arrow keys. When you are trying to write '\' in that text field, it becomes '\\' (with escape). You need to paste this text (copy from some text editor, o from terminal) \001 (home) and \005 (end) in field.

https://i.stack.imgur.com/s0gpC.png