bash iTerm 2:如何设置键盘快捷键以跳转到行首/行尾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6205157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:35:20  来源:igfitidea点击:

iTerm 2: How to set keyboard shortcuts to jump to beginning/end of line?

macosbashitermiterm2

提问by chris

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

我可以看到Ctrl+ left/right跳转到行的开头/结尾。如何将其更改为Cmd+ left/ right arrow

In order to get Alt+left/right arrowworking correctly for word jumping follow this article.

为了让Alt+ left/right arrow正确地进行单词跳转,请遵循这篇文章

回答by Matthias

Add in iTerm2 the following Profile Shortcut Keys

在 iTerm2 中添加以下配置文件快捷键

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

这是为需要它的人提供的视觉效果

iTerm add key visual

iTerm 添加关键视觉

回答by sqren

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

到单词之间跳跃和开始/在iTerm2线端挑一个下面的两种溶液。

1. Simple solution (recommended)

1.简单的解决方案(推荐)

  1. Open Preferences
  2. Click "Profile" tab
  3. Select a profile in the list on the left (eg "Default") and click "Keys" tab
  4. Click the "Presets" downdown and select "Natural Text Editing"
  1. 打开首选项
  2. 单击“个人资料”选项卡
  3. 在左侧列表中选择一个配置文件(例如“默认”),然后单击“密钥”选项卡
  4. 单击“预设”向下并选择“自然文本编辑”

enter image description here

在此处输入图片说明



2. Mapping keys manually (Advanced)

2. 手动映射键(高级)

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

如果你不想使用上面提到的“自然文本编辑”预设,你可以手动映射你需要的键:

  1. Open Preferences
  2. Click “Keys” tab
  3. Click the [+]icon
  1. 打开首选项
  2. 单击“密钥”选项卡
  3. 点击[+]图标

You can now add the following keyboard shortcuts:

您现在可以添加以下键盘快捷键:

Move cursor one word left

将光标向左移动一个字

  • Keyboard shortcut: ?+
  • Action: Send Hex Code
  • Code: 0x1b 0x62
  • 键盘快捷键:?+
  • 操作:发送十六进制代码
  • 代码: 0x1b 0x62

enter image description here

在此处输入图片说明

Move cursor one word right

将光标向右移动一个字

  • Keyboard Combination: ?+
  • Action: Send Hex Code
  • Code: 0x1b 0x66
  • 键盘组合:?+
  • 操作:发送十六进制代码
  • 代码: 0x1b 0x66

Move cursor to beginning of line

将光标移动到行首

  • Keyboard Combination: ?+
  • Action: Send Hex Code
  • Code: 0x01
  • 键盘组合:?+
  • 操作:发送十六进制代码
  • 代码: 0x01

Move cursor to end of line

将光标移动到行尾

  • Keyboard Combination: ?+
  • Action: Send Hex Code
  • Code: 0x05
  • 键盘组合:?+
  • 操作:发送十六进制代码
  • 代码: 0x05

Delete word

删除单词

  • Keyboard Combination: ?+ ←Delete
  • Action: Send Hex Code
  • Code: 0x1b 0x08
  • 键盘组合:?+←Delete
  • 操作:发送十六进制代码
  • 代码: 0x1b 0x08

Delete line

删除行

  • Keyboard Combination: ?+ ←Delete
  • Action: Send Hex Code
  • Code: 0x15
  • 键盘组合:?+←Delete
  • 操作:发送十六进制代码
  • 代码: 0x15

Undo

撤消

  • Keyboard Combination: ?+ z
  • Action: Send Hex Code
  • Code: 0x1f
  • 键盘组合:?+z
  • 操作:发送十六进制代码
  • 代码: 0x1f

Don't forget to remove the previous bindings:

不要忘记删除以前的绑定

  • Open the “Profiles” tab
  • Click the sub-tab ”Keys”
  • Remove the mappings for key combinations ?+ and ?+
  • 打开“配置文件”选项卡
  • 单击子选项卡“密钥”
  • 删除组合键?+?+的映射

回答by Travis

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).

我看到已经有很多好的答案,但这应该提供最接近本机 OSX 功能的不仅仅是你的 shell。我验证了这在 ZSH、Bash、node、python -i、iex 和 irb/pry 会话中有效(使用 rb-readline gem 作为 readline,但应该适用于所有人)。

Open the iTerm preferences ?+,and navigate to the Profilestab (the Keystab can be used, but adding keybinding to your profile allows you to save your profileand sync it to multiple computers) and keyssub-tab and enter the following:

打开 iTerm 首选项?+,并导航到Profiles选项卡(Keys可以使用该选项卡,但将键绑定添加到您的配置文件允许您保存配置文件并将其同步到多台计算机)和keys子选项卡并输入以下内容:

Delete all characters left of the cursor

删除光标左边的所有字符

?+←DeleteSend Hex Codes:

0x15More 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 ^Uto delete only stuff to the left of the cursor (see below).

or

0x18 0x7fLess 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发送十六进制代码:

0x15更兼容,但功能有时是删除整行,而不仅仅是光标左侧的字符。我个人使用它,然后覆盖我的 zsh 绑定键^U以仅删除光标左侧的内容(见下文)。

或者

0x18 0x7f兼容性较差,在 node 中不起作用,默认情况下在 zsh 中不起作用,请参阅下文以修复 zsh(bash/irb/pry 应该没问题),在它起作用时执行所需的功能。

Delete all characters right of the cursor

删除光标右侧的所有字符

?+fn+←Deleteor ?+Delete→Send Hex Codes: 0x0b

?+ fn+←Delete?+Delete→发送十六进制代码:0x0b

Delete one word to left of cursor

删除光标左边的一个词

?+←DeleteSend Hex Codes:

0x1b 0x08Breaks in Elixir's IEX, seems to work fine everywhere else

or

0x17Works everywhere, but doesn't stop at normal word breaks in IRB and will instead delete until it sees a literal space.

?+←Delete发送十六进制代码:

0x1b 0x08Elixir 的 IEX 中断,似乎在其他任何地方都可以正常工作

或者

0x17适用于任何地方,但不会在 IRB 中的正常分词处停止,而是会删除,直到看到文字空格。

Delete one word to right of cursor

删除光标右侧的一个词

?+fn←Deleteor ?+Delete→Send Hex Codes: 0x1b 0x64

?+fn←Delete?+Delete→发送十六进制代码:0x1b 0x64

Move cursor to the front of line

将光标移动到行首

?+Send Hex Codes: 0x01

?+发送十六进制代码:0x01

Move cursor to the end of line

将光标移动到行尾

?+Send Hex Codes: 0x05

?+发送十六进制代码:0x05

Move cursor one word left

将光标向左移动一个字

?+Send Hex Codes: 0x1b 0x62

?+发送十六进制代码:0x1b 0x62

Move cursor one word right

将光标向右移动一个字

?+Send Hex Codes: 0x1b 0x66

?+发送十六进制代码:0x1b 0x66

Undo

撤消

?+zSend Hex Codes: 0x1f

?+z发送十六进制代码:0x1f

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

重做通常不会在 bash、zsh 或 readline 中绑定,因此我们可以将其设置为未使用的十六进制代码,然后我们可以在 zsh 中进行修复

?+?+Zor ?+ySend Hex Codes: 0x18 0x1f

?+ ?+Z?+y发送十六进制代码:0x18 0x1f

Now how to fix any that don't work

现在如何解决任何不起作用的问题

For zsh, you can setup binding for the not yet functional ?+←Deleteand ?+?+Z/?+yby running:

对于 zsh,您可以通过运行以下命令为尚未运行的?+←Delete?+ ?+ Z/ ?+设置绑定 y

# 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.

我找不到在 bash 或 readline 中添加重做的解决方案,所以如果有人知道其中任何一个的解决方案,请在下面发表评论,我会尝试添加它们。

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

对于寻找有关如何将键序列转换为十六进制的查找表的任何人,我发现此表非常有用。

回答by Jaime Bellmyer

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

按照上面列出的教程在iterm2 中设置您的关键首选项。

  1. Create a new shorcut key
  2. Choose "Send escape sequence" as the action
  3. Then, to set cmd-left, in the text below that:
    • Enter [Hfor line start
      OR
    • Enter [Ffor line end
  1. 创建一个新的快捷键
  2. 选择“发送转义序列”作为操作
  3. 然后,在下面的文本中设置cmd-left
    • 输入[H行开始
      OR
    • 输入[F行结束

回答by Nik Kov

In iTerm 3.0.12 you can switch to Natural Text Editingpreset:

在 iTerm 3.0.12 中,您可以切换到Natural Text Editing预设:

enter image description here

在此处输入图片说明

iTerm → Preferences → Profiles → Keys

iTerm → 首选项 → 配置文件 → 键

WarningAs 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.

警告由于它是一个预设,它可以覆盖您之前绑定的键。所以最好在应用预设之前保存您当前的键绑定。

回答by Ioannis Filippidis

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.htmlnotes that in iTerm2:

对于想要在 iTerm2 中转到行尾或行首的任何人的快速参考,上面的链接 http://hackaccident.blogspot.com/2007/07/skip-to-next-or-previous-word- in-iterm.html指出在 iTerm2 中:

  • Ctrl+A, jumps to the start of the line, while
  • Ctrl+E, jumps to the end of the line.
  • Ctrl+ A, 跳转到行首,而
  • Ctrl+ E, 跳转到行尾。

回答by GabLeRoux

I used Travis answerand I created a dynamic profileyou can import based on its instructions.

我使用了Travis answer并创建了一个动态配置文件,您可以根据其说明导入。

GabLeRoux/iterm2-macos-dynamic-profile

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

说明在自述文件中,导入它比手动添加它们要快得多。我根据@gooli 的要求回答了这个问题,因为这隐藏在评论中。希望你喜欢这个

回答by 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/inputrcproperly, and so fails to understand any key bindings you set up through the preferences!

只是为了帮助遇到相同问题但特别是在 iTerm 2 中使用 Zsh shell 的任何人。事实证明,Zsh无法/etc/inputrc正确读取,因此无法理解您通过首选项设置的任何键绑定!

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

要解决此问题,您需要在.zshrc文件中添加一些键绑定,例如:

# 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 .zshrcfile when adding bindings.

请注意上面示例中“e”之前的反斜杠,链接的文章没有显示它们,因此.zshrc在添加绑定时将它们添加到您的文件中。

回答by halfelf

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

旧的 emacs 绑定仍然可以在 iterm2 和 os x 终端中工作:

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

首选项 -> 配置文件 -> 密钥(配置文件中的子选项卡)

  • Set Left/Right option <kbd>?</kbd> key acts as +Esc(similar in os x terminal)
  • 设置Left/Right option <kbd>?</kbd> key acts as +Esc(在 os x 终端中类似)

This should enable alt-fand alt-bfor moving words by words. (Still ctrl-aand ctrl-ealways work as usual)

这应该能够alt-f并且alt-b逐字移动。(仍然ctrl-a而且ctrl-e总是像往常一样工作)

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

如果设置为meta那些旧绑定将工作,而某些 iterm2 绑定不可用。

回答by 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"

默认情况下,单词跳转(选项 + → 或 ←)和单词删除(选项 + 退格)不起作用。要启用这些,请转到“iTerm → 首选项 → 配置文件 → 键 → 加载预设... → 自然文本编辑 → 轰隆!头部爆炸”