如何更快地导航长命令?
当我必须输入长命令时,有没有办法加速Linux CLI导航? 我现在只是使用箭头,而且 – 如果我有一个长命令,从命令的开始到它的中间需要一些时间。
有没有办法例如跳转到命令的中间而不使用箭头?
Readline库提供的一些有用的行编辑键绑定:
-
Ctrl-A
:转到行首 -
Ctrl-E
:转到行尾 -
Alt-B
:向后跳过一个单词 -
Alt-F
:向前跳过一个单词 -
Ctrl-U
:删除到行首 -
Ctrl-K
:删除到行尾 -
Alt-D
:删除到单词的结尾
这里有一些快捷方式
Ctrl + a – go to the start of the command line Ctrl + e – go to the end of the command line Ctrl + k – delete from cursor to the end of the command line Ctrl + u – delete from cursor to the start of the command line Ctrl + w – delete from cursor to start of word (ie delete backwards one word) Ctrl + y – paste word or text that was cut using one of the deletion shortcuts (such as the one above) after the cursor Ctrl + xx – move between start of command line and current cursor position (and back again) Alt + b – move backward one word (or go to start of word the cursor is currently on) Alt + f – move forward one word (or go to end of word the cursor is currently on) Alt + d – delete to end of word starting at cursor (whole word if cursor is at the beginning of word) Alt + c – capitalize to end of word starting at cursor (whole word if cursor is at the beginning of word) Alt + u – make uppercase from cursor to end of word Alt + l – make lowercase from cursor to end of word Alt + t – swap current word with previous Ctrl + f – move forward one character Ctrl + b – move backward one character Ctrl + d – delete character under the cursor Ctrl + h – delete character before the cursor Ctrl + t – swap character under cursor with the previous one
如果您是vi [m]和bash用户,您可能会发现通过将set editing-mode vi
添加到~/.inputrc
或/etc/inputrc
文件中来使readline(由bash使用)使用vi样式编辑很有用。 或者,您可以通过运行bash命令set -o vi
来使bash使用vi样式编辑。 将命令添加到~/.bashrc
文件以使行为持久。
如果您是zsh用户,请将bindkey -v
添加到bindkey -v
文件以进行vi样式编辑。
我不知道如何在不使用光标键的情况下专门跳转到中间。 但是,我建议使用CTRL +光标键从空白移动到空白(即从一个单词跳到另一个单词)。
在.bashrc中获取下面的代码段。 Ctrl-a跳转到开头并按Ctrl-a再次跳到中间。
jump_mid() { if [ "$READLINE_POINT" -eq "0" ]; then LEN=${#READLINE_LINE} POS=$(($LEN / 2)) READLINE_POINT=$POS else READLINE_POINT=0 fi } bind -x '"\Ca" : jump_mid'
或者,如果您想使用Ctrl-Something直接跳转到中间,请将代码更改为:
jump_mid() { LEN=${#READLINE_LINE} POS=$(($LEN / 2)) READLINE_POINT=$POS }
并将其绑定到与Ctrl-a不同的内容。