如何更快地浏览长命令?


136

当我必须输入长命令时,是否可以加快Linux CLI的导航?我现在只是使用箭头,并且-如果我的命令比较长,从命令的开始到命令的中间会花费一些时间。

有没有办法例如不使用箭头就跳到命令中间?

Answers:


174

Readline库提供了一些有用的行编辑键绑定:

  • Ctrl-A:转到行首
  • Ctrl-E:转到行尾
  • Alt-B:向后跳过一个字
  • Alt-F:向前跳过一个字
  • Ctrl-U:删除到行首
  • Ctrl-K:删除到行尾
  • Alt-D:删除到单词末尾

7
+1,因为即使由于某些原因ctrl箭头不起作用,此操作也有效。值得一提的是,对于screen用户而言,Ctrl-A变为Ctrl-
AA。– enzotib

3
要撤消的缺失(或通过删除其移动文本),使用Ctrl + Y
Lekensteyn

7
Ctrl +向右箭头,Ctrl +向左箭头值得一提。
mac

3
在Ubuntu上,使用Gnome和GnomeTerminal Alt-A打开菜单而不是移动光标。您如何Alt-A与Gnome一起使用?我的意思是,Gnome是默认设置,因此阅读此书的任何人都有可能在Gnome中运行终端。
杰森

1
如果要通过SSH OS X 连接到Ubuntu ,则可能必须使用“ Esc”而不是Ctrl,例如Esc-A,Esc-E等。对于iTerm和Terminal来说是这样。
Fred Clausen 2015年

77

这里还有更多捷径

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 (i.e. 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

感谢您提供的简单明了的清单。
neverMind9 '18

14

如果您是vi [m]和bash用户,则可能会发现,将readline(供bash使用)添加set editing-mode vi~/.inputrc/etc/inputrc文件中以使用vi样式编辑很有用。或者,您可以通过运行bash命令使bash使用vi风格的编辑set -o vi。将命令添加到您的~/.bashrc文件以使行为持久。

如果您是zsh用户,请添加bindkey -v.zshrc文件中以进行vi样式编辑。


8

我不知道一种不使用光标键专门跳到中间的方法。但是,我建议使用CTRL +光标键从空白移到空白(即,从一个单词跳到另一个单词)。


1

在您的.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 '"\C-a" : jump_mid'

或者,如果您想使用Ctrl-Something直接跳到中间,请将代码更改为:

jump_mid() {
    LEN=${#READLINE_LINE}
    POS=$(($LEN / 2))
    READLINE_POINT=$POS
}

并将其绑定到与Ctrl-a不同的东西。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.