ChatGPT解决这个技术问题 Extra ChatGPT

How to set keyboard shortcuts to jump to beginning/end of line? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions about general computing hardware and software on Stack Overflow. You can edit the question so it’s on-topic for Stack Overflow. Closed last year. Improve this question

I can see that Ctrl+left/right jumps to the beginning/end of line. How to change this to Cmd+left/right arrow?

If you want those shortcuts to work in VIM too, see this answer: stackoverflow.com/a/9611698/991068

a
artist.pradeep

Add in iTerm2 the following Profile Shortcut Keys

FOR ACTION SEND ⌘ ← "SEND HEX CODE" 0x01 ⌘ → "SEND HEX CODE" 0x05 ⌥ ← "SEND ESC SEQ" b ⌥ → "SEND ESC SEQ" f

Here is a visual for those who need it

https://i.imgur.com/djgBBQ3.png


Do you know what the code/sequence for Command+Delete is? (clear out the prompt)
I answered my first comment elsewhere, basically hex-code 0x15 gives Ctrl+U which clears the line. However I am finding 0x01 Ctrl+A to interfere with Tmux usage. Therefore I intend to map it instead to the Home key (same functionality as Fn+Left on a Mac keyboard).
The escape sequence for Home is [1~ and the escape sequence for End is [4~
Works like a charm, awesome!
The correct binding is ⌘← "SEND ESC SEQ" OH for Home and ⌘→ "SEND ESC SEQ" OF for End (those are uppercase 'o's not zeros). This simulates actually pressing the Home and End keys, and as such will work in bash, vim, etc.
s
sqren

To jump between words and start/end of lines in iTerm2 pick one of the two solutions below.

1. Simple solution (recommended)

Open Preferences Click "Profile" tab Select a profile in the list on the left (eg "Default") Click "Keys" tab Click "Key Mappings" tab (if it exists) Click the "Presets" dropdown and select "Natural Text Editing"

https://i.stack.imgur.com/NLIVi.gif

Note: if you have several profiles (f.e. Default and Hotkey Window) and want the same modification to be applied for all profiles, use these steps instead:

Click "Keys" tab Choose "Key Bindings"

2. Mapping keys manually (Advanced)

If you don't want to use the "Natural Text Editing" preset mentioned above, you can map the keys you need manually:

Open Preferences Click "Profile" tab Select a profile in the list on the left (eg "Default") Click "Keys" tab Click "Key Mappings" tab (if it exists) Click the [+] icon Add the shortcuts from the table below

SHORTCUT DESCRIPTION ACTION SEND ⌘ ← Jump to beginning of line "SEND HEX CODE" 0x01 ⌘ → Jump to end of line "SEND HEX CODE" 0x05 ⌘ ← Delete Delete line "SEND HEX CODE" 0x15 ⌥ ← Jump to beginning of word "SEND HEX CODE" 0x1b 0x62 ⌥ → Jump to beginning end of word "SEND HEX CODE" 0x1b 0x66 ⌥ ← DELETE Delete word "SEND HEX CODE" 0x1b 0x08 ⌘ z Undo "SEND HEX CODE" 0x1f

Note

If keyboard bindings already exists for the shortcuts above, they must be removed for the new ones to take effect.


This worked well for me in iTerm2 and was very easy to follow, thanks!
Just clarifying what do the original bindings for ⌥← do? I see it Says send Hex Codes: 0x1b 0x1b 0x5b 0x44. Would be good to know in case people did not want to remove this.
Does iTerm have a way to bundle these into a preset we can just load? Seems annoying to type in manually but this worked for me and was awesome. Thanks a lto!
This is assuming you are using emacs mode set -o emacs
I think this should be the right answer to the question
C
Community

I see there's a lot of good answers already, but this should provide the closest to native OSX functionality as possible in more than just your shell. I verified that this works in ZSH, Bash, node, python -i, iex and irb/pry sessions (using rb-readline gem for readline, but should work for all).

Open the iTerm preferences +, and navigate to the Profiles tab (the Keys tab can be used, but adding keybinding to your profile allows you to save your profile and sync it to multiple computers) and keys sub-tab and enter the following:

Delete all characters left of the cursor

⌘+←Delete Send Hex Codes: 0x15 More compatible, but functionality sometimes is to delete the entire line rather than just the characters to the left of the curser. I personally use this and then overwrite my zsh bindkey for ^U to delete only stuff to the left of the cursor (see below). or 0x18 0x7f Less compatible, doesn't work in node and won't work in zsh by default, see below to fix zsh (bash/irb/pry should be fine), performs desired functionality when it does work.

Delete all characters right of the cursor

⌘+fn+←Delete or ⌘+Delete→ Send Hex Codes: 0x0b

Delete one word to left of cursor

⌥+←Delete Send Hex Codes: 0x1b 0x08 Breaks in Elixir's IEX, seems to work fine everywhere else or 0x17 Works everywhere, but doesn't stop at normal word breaks in IRB and will instead delete until it sees a literal space.

Delete one word to right of cursor

⌥+fn←Delete or ⌥+Delete→ Send Hex Codes: 0x1b 0x64

Move cursor to the front of line

⌘+← Send Hex Codes: 0x01

Move cursor to the end of line

⌘+→ Send Hex Codes: 0x05

Move cursor one word left

⌥+← Send Hex Codes: 0x1b 0x62

Move cursor one word right

⌥+→ Send Hex Codes: 0x1b 0x66

Undo

⌘+z Send Hex Codes: 0x1f

Redo typically not bound in bash, zsh or readline, so we can set it to a unused hexcode which we can then fix in zsh

⇧+⌘+Z or ⌘+y Send Hex Codes: 0x18 0x1f

Now how to fix any that don't work

For zsh, you can setup binding for the not yet functional ⌘+←Delete and ⇧+⌘+Z/⌘+y by running:

# changes hex 0x15 to delete everything to the left of the cursor,
# rather than the whole line
$ echo 'bindkey "^U" backward-kill-line' >> ~/.zshrc

# binds hex 0x18 0x7f with deleting everything to the left of the cursor
$ echo 'bindkey "^X\\x7f" backward-kill-line' >> ~/.zshrc

# adds redo
$ echo 'bindkey "^X^_" redo' >> ~/.zshrc

# reload your .zshrc for changes to take effect
$ source ~/.zshrc

I'm unable to find a solution for adding redo in bash or readline, so if anyone know a solution for either of those, please comment below and I'll try to add them in.

For anyone looking for the lookup table on how to convert key sequences to hex, I find this table very helpful.


That is really useful, makes a lot of difference! I would like to point it out that some people can get confused with the $ in front of echo (just remove it?). Other than that, it's perfect. Thank you.
@Christophe So it looks like the key binding is there like it should be. Either you made a mistake adding the hex values in or keybinding for backward-word is being overwritten. Run bindkey | grep "\^\[b" and that'll tell you if something else is overwriting it after it's been set. It should only return #=> "^[b" backward-word. If it's being overwritten, you'll have to find where its being overwritten and remove it. If that's not possible, you can run echo 'bindkey "^[b" backward-word' >> ~/.zshrc it will add it to the last line of your .zshrc, then restart iTerm and it should work
@Christophe check the keys tab in the iTerm preferences to see if it's also mapping + to something. If so remove it. You can test if your b is working correctly by quickly pressing esc releasing, then pressing b. That should jump a word to the left. Alternatively, you can map + to hex 0x1b 0x42 or 0x1b 0x5b 0x31 0x3b 0x35 0x44. I verified that both of those work for me. I really think you have + being overwritten in iTerm though, either in your profile keys tab or keys tab.
Amazing! Just learned about Undo plus Redo bindings from here.
I did this manually in and I thought why can't I import a profile to do this? Then I created GabLeRoux/iterm2-macos-dynamic-profile based on these instructions. It works flawlessly, thanks 🍻
N
Nike Kov

In iTerm 3.0.12 you can switch to Natural Text Editing preset:

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

iTerm → Preferences → Profiles → Keys

Warning As it is a preset, it can override the keys you have binded before. So it's better to save your current key bindings before applying a preset.


Could you elaborate more your answer? As in, what that preset includes and might overwrite?
Thanks for the answer. Also chiming in it's probably best practice to apply this to your own custom profile that way you aren't screwing with the defaults and can toggle the profile as a natural text editing "mode"
Simplest and easiest solution by far. Should be #1 answer.
@CarlesAlcolea I think it will remove your custom presets and hotkeys so you should export the profile if you want to save them.
@CarlesAlcolea and @wordsforthewise Look at the warning
S
Stphane

Follow the tutorial you listed above for setting up your key preferences in iterm2.

Create a new shorcut key Choose "Send escape sequence" as the action Then, to set cmd-left, in the text below that: Enter [H for line start OR Enter [F for line end


F jumpes only one word within my iterm2
ITerm => Preferences => Keys
Love this! the other solutions did not work for me. I Spend hours frustrated that I could not use ctrl+a and ctrl+e in iterm2. But I did not want to give up fish for it. This totally works thank you!
Other solutions did not work for me either, but this one did.
F
Fabrizio

For quick reference of anyone who wants to go to the end of line or start of line in iTerm2, the above link http://hackaddict.blogspot.com/2007/07/skip-to-next-or-previous-word-in-iterm.html notes that in iTerm2:

Ctrl+A, jumps to the start of the line, while

Ctrl+E, jumps to the end of the line.


Why is this answer not at the top?
@GangadharJannu: Totally agree. It goes straight to the ans. Should be on the top.
agreed i knew the ctrl + A to get to start of line but couldn't find the end of line. this needs to be marked as the answer.
G
GabLeRoux

I used Travis answer and I created a dynamic profile you can import based on its instructions.

GabLeRoux/iterm2-macos-dynamic-profile

Instructions are in the readme and it's a lot faster to import this than it is to add them all manually. I made this an answer as per @gooli's request because this was hidden in a comment. Hope you enjoy this 🍻


Came back here a year later and so happy to find this!
N
Nerdwood

Just to help out anyone that is having the same issue but specifically using Zsh shell with iTerm 2. It turns out that Zsh doesn't read /etc/inputrc properly, and so fails to understand any key bindings you set up through the preferences!

To fix this, you need to add some key bindings to your .zshrc file, such as:

# key bindings
bindkey "\e[1~" beginning-of-line
bindkey "\e[4~" end-of-line

Note the backslashes in the example above before the "e", the linked article does not show them, so add them into your .zshrc file when adding bindings.


z
zx485

The only things that work for for moving to the beginning and end of line are

⌘← "SEND ESC SEQ" OH - to move to the beginning of line
⌘→ "SEND ESC SEQ" OF - to move to the end of line


h
halfelf

The old fashion emacs bindings can still work in iterm2 and os x terminal:

Preferences -> Profiles -> Keys (sub tab in profiles)

Set Left/Right option key acts as +Esc (similar in os x terminal)

This should enable alt-f and alt-b for moving words by words. (Still ctrl-a and ctrl-e always work as usual)

If set as meta those old bindings will work while some iterm2 bindings unavailable.


I
Islam.Ibrahim

As Explains in here, you can do it with a simple steps:

By default, word jumps (option + → or ←) and word deletions (option + backspace) do not work. To enable these, go to "iTerm → Preferences → Profiles → Keys → Load Preset... → Natural Text Editing → Boom! Head explodes"


J
Jose Alban

bind -p will show a list of bound escaped keys in your shell, that might help giving you more ideas / search terms.


bind -p only works in Bourne shells (bash, sh) for zsh, csh tcsh, use bindkey.
Z
Zitao Xiong

explained in https://aaronaddleman.com/articles/hexcodes-and-iterm/

you can use xxd -psd to get key hex code.


H
Halil Özgür

For me, the following combination worked:

Key Action Esc+ End result ⌘← Send Escape Sequence a Send ^[ a ⌘→ Send Escape Sequence e Send ^[ e