我在与bash 命令行让工作变得更轻松、更快捷。
喜欢:
- ctrl+ L:清除屏幕
- ctrl+ a/ ctrl+ e:移动行首/行末
- ctrl+ r:只需写几个字符即可搜索命令的历史记录
- ctrl+ u/ ctrl+ y:剪切/粘贴该行。
还有很多很多我想要了解的并且学习这些肯定是有用的。
我想知道从哪里可以获取 Ubuntu 中这些快捷方式的列表?有没有列出这些快捷方式的手册?
笔记:
我想在一个地方获取快捷方式及其操作的列表。在很短的时间内学习很多快捷方式确实很有帮助。那么我们有没有办法得到这样的列表?不过谢谢你在这里给出的回答。
答案1
默认值位于 中man bash
,并附有每个命令的详细信息。如果您更改了键绑定,请参阅 BroSlow 的回答。
Commands for Moving
beginning-of-line (C-a)
Move to the start of the current line.
end-of-line (C-e)
Move to the end of the line.
forward-char (C-f)
Move forward a character.
backward-char (C-b)
Move back a character.
forward-word (M-f)
Move forward to the end of the next word. Words are composed of alphanumeric characters (letters and digits).
backward-word (M-b)
Move back to the start of the current or previous word. Words are composed of alphanumeric characters (letters and digits).
shell-forward-word
Move forward to the end of the next word. Words are delimited by non-quoted shell metacharacters.
shell-backward-word
Move back to the start of the current or previous word. Words are delimited by non-quoted shell metacharacters.
clear-screen (C-l)
Clear the screen leaving the current line at the top of the screen. With an argument, refresh the current line without clearing the screen.
...
reverse-search-history (C-r)
Search backward starting at the current line and moving `up' through the history as necessary. This is an incremental search.
...
unix-line-discard (C-u)
Kill backward from point to the beginning of the line. The killed text is saved on the kill-ring.
...
yank (C-y)
Yank the top of the kill ring into the buffer at point.
编辑
这些命令都位于手册的连续部分中,因此您可以从 浏览Commands for Moving
。或者,您可以使用以下方式将整个部分保存到文本文件中:
man bash | awk '/^ Commands for Moving$/{print_this=1} /^ Programmable Completion$/{print_this=0} print_this==1{sub(/^ /,""); print}' > bash_commands.txt
(注意:这将打印整个部分,包括没有默认键盘快捷键的命令。)
awk 代码解释
- 当(唯一)出现时
Commands for Moving
,将变量设置print_this
为 1。 - 在(唯一)出现时
Programmable Completion
(即以下部分),将变量设置为 0。 - 如果变量是 1,则删除前导空格(三个空格),并打印该行。
答案2
你可以通过调用 bash 内置命令列出当前 bash shell 中的所有快捷方式bind
带有-P
选项。
例如
bind -P | grep clear
clear-screen can be found on "\C-l".
要改变它们,你可以做类似的事情
bind '\C-p:clear-screen'
并将其放入初始化文件中以使其永久生效(请注意,您一次只能将一个组合键绑定到一个事物,因此它将失去之前的任何绑定)。
答案3
以下命令给出了一个很好的柱状输出,显示了用法和快捷方式。
bind -P | grep "can be found" | sort | awk '{printf "%-40s", $1} {for(i=6;i<=NF;i++){printf "%s ", $i}{printf"\n"}}'
这将给出一个输出,如下所示
abort "\C-g", "\C-x\C-g", "\e\C-g".
accept-line "\C-j", "\C-m".
backward-char "\C-b", "\eOD", "\e[D".
backward-delete-char "\C-h", "\C-?".
backward-kill-line "\C-x\C-?".
backward-kill-word "\e\C-h", "\e\C-?".
backward-word "\e\e[D", "\e[1;5D", "\e[5D", "\eb".
beginning-of-history "\e<".
beginning-of-line "\C-a", "\eOH", "\e[1~", "\e[H".
call-last-kbd-macro "\C-xe".
capitalize-word "\ec".
character-search-backward "\e\C-]".
character-search "\C-]".
clear-screen "\C-l".
complete "\C-i", "\e\e".
...
使用以下命令将此输出放入文本文件中
bind -P|grep "can be found"|sort | awk '{printf "%-40s", $1} {for(i=6;i<=NF;i++){printf "%s ", $i}{printf"\n"}}' > ~/shortcuts
该文件在您的 $HOME 目录中创建。
解释
获得所有快捷方式。
bind -P
删除所有未指定的快捷方式
grep "can be found"
对输出进行排序
sort
打印第一列(即函数)并对齐文本
awk '{printf "%-40s", $1}
这是上一个命令的一部分。它打印 6+ 列(即快捷方式)。
{for(i=6;i<=NF;i++){printf "%s ", $i}{printf"\n"}}'
将输出放入主目录中名为快捷方式
> shortcuts
您可以通过运行以下命令了解该命令的工作原理。
bind -P
bind -P | grep "can be found"
bind -P | grep "can be found" | sort
答案4
只要 bash 手册没有被修改以致于该命令不合适(这不太可能),以下命令将显示所有默认快捷键bash
。
man bash | grep -A294 'Commands for Moving'
输出如下:
Commands for Moving
beginning-of-line (C-a)
Move to the start of the current line.
end-of-line (C-e)
Move to the end of the line.
forward-char (C-f)
Move forward a character.
backward-char (C-b)
Move back a character.
forward-word (M-f)
Move forward to the end of the next word. Words are composed of alphanumeric characters (letters and digits).
backward-word (M-b)
Move back to the start of the current or previous word. Words are composed of alphanumeric characters (letters and
digits).
shell-forward-word
Move forward to the end of the next word. Words are delimited by non-quoted shell metacharacters.
shell-backward-word
Move back to the start of the current or previous word. Words are delimited by non-quoted shell metacharacters.
clear-screen (C-l)
Clear the screen leaving the current line at the top of the screen. With an argument, refresh the current line
without clearing the screen.
redraw-current-line
Refresh the current line.
Commands for Manipulating the History
accept-line (Newline, Return)
Accept the line regardless of where the cursor is. If this line is non-empty, add it to the history list according
to the state of the HISTCONTROL variable. If the line is a modified history line, then restore the history line to
its original state.
previous-history (C-p)
Fetch the previous command from the history list, moving back in the list.
next-history (C-n)
...
如果修改了 bash 手册,则可以轻松更改此命令以满足需要。