如何更快地导航长命令?

Sta*_*ann 149 command-line bash

当我必须输入长命令时,有没有办法加快 Linux CLI 导航?我现在只是使用箭头,并且 - 如果我有一个很长的命令,从命令的开始到它的中间需要一些时间。

例如,有没有办法在不使用箭头的情况下跳转到命令的中间?

Ada*_*tek 194

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

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

  • +1 因为即使由于某些原因,ctrl-arrows 不起作用,这也有效。值得注意的是,对于 `screen` 用户,Ctrl-A 变成了 Ctrl-A A。 (7认同)
  • Ctrl+向右箭头,Ctrl+向左箭头也值得一提。 (7认同)
  • 要撤消删除(或通过删除来移动文本),请使用 Ctrl + Y。 (3认同)
  • 在 Ubuntu 上使用 Gnome 和 GnomeTerminal `Alt-A` 打开菜单而不是移动光标。你如何在 Gnome 中使用“Alt-A”?我的意思是,Gnome 是默认设置,因此很可能任何阅读本文的人都会在 Gnome 中运行终端。 (3认同)

kar*_*cio 83

这里还有一些捷径

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
Run Code Online (Sandbox Code Playgroud)

  • 感谢您提供这份简单、无废话的清单。 (2认同)

小智 15

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

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


Kor*_*nuk 8

我不知道有什么方法可以在不使用光标键的情况下专门跳到中间。但是,我可以推荐使用Ctrl+ 光标键从空白移动到空白(即从一个单词跳转到另一个单词)。


小智 5

在 .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'
Run Code Online (Sandbox Code Playgroud)

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

jump_mid() {
    LEN=${#READLINE_LINE}
    POS=$(($LEN / 2))
    READLINE_POINT=$POS
}
Run Code Online (Sandbox Code Playgroud)

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