Sta*_*ann 149 command-line bash
当我必须输入长命令时,有没有办法加快 Linux CLI 导航?我现在只是使用箭头,并且 - 如果我有一个很长的命令,从命令的开始到它的中间需要一些时间。
例如,有没有办法在不使用箭头的情况下跳转到命令的中间?
Ada*_*tek 194
Readline库提供的一些有用的行编辑键绑定:
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)
小智 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 样式编辑。
小智 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 不同的东西。