bash 在终端命令行上移动光标的最快方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/657130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest way(s) to move the cursor on a terminal command line?
提问by Frank
What is the best way to move around on a given very long command line in the terminal?
在终端中给定的非常长的命令行上移动的最佳方式是什么?
Say I used the arrow key or Ctrl-Rto get this long command line:
假设我使用箭头键或Ctrl-R来获取这个长命令行:
./cmd --option1 --option2 --option3 --option4 --option5 --option6 --option7 --option8 --option9 --option10 --option11 --option12 --option13 --option14 --option15 --option16 --option17 --option18 --option19 --option20 --option21 --option22 --option23 --option24 --option25 --option26 --option27 --option28 --option29 --option30 --option31 --option32 --option33 --option34 --option35 --option36 --option37 --option38 --option39 --option40 --option41 --option42 --option43 --option44 --option45 --option46 --option47 --option48 --option49 --option50
Now I need to move (starting from the beginning or the end of the line) the cursor to --option25
to modify something there.
现在我需要将光标移动(从行的开头或结尾开始)--option25
以修改那里的内容。
What is the fastest way to get there? What I usually do is Ctrl-Ato get to the beginning and then repeatedly Alt-Fto move forward, word by word (or Ctrl-Eto go the end and Alt-Bto then go backward). But on a long line that takes too much time. There must be a way to search and jump directly to the part I need to modify, e.g. option25
?
到达那里的最快方式是什么?我通常做的是Ctrl-A从头开始,然后重复Alt-F一个字一个字地向前移动(或者Ctrl-E走到最后然后Alt-B然后向后退)。但在一条需要太多时间的长线上。必须有一种方法可以搜索并直接跳转到我需要修改的部分,例如option25
?
采纳答案by Pianosaurus
Since this hasn't been closed yet, here are a few more options.
由于此功能尚未关闭,因此这里有更多选项。
- Use Ctrl+xfollowed by Ctrl+eto open the current line in the editor specified by
$FCEDIT
or$EDITOR
oremacs
(tried in that order). - If you ran the command earlier, hit Ctrl+rfor a reverse history search and type
option25
(in this case). The line will be displayed. Hit Tabto start editing at this point. - Use history expansion with the
s///
modifier. E.g.!-2:s/--option25/--newoption/
would rerun the second-to-last command, but replace option25. To modify the last./cmd
command, use the!string
syntax:!./cmd:s/--option25/--newoption/
Any delimiter may be used in place of / in the substitution. - If editing the previous line, you can use quick substitution:
^--option25^--newoption
- Character search. This was mentioned by Pax, and can be done in regular emacs-mode with Ctrl+]for forward search, and Ctrl+Alt+]for backward search.
- 使用Ctrl+x后跟Ctrl+e在由
$FCEDIT
或$EDITOR
或emacs
(按该顺序尝试)指定的编辑器中打开当前行。 - 如果您之前运行过该命令,请点击Ctrl+r进行反向历史搜索并输入
option25
(在本例中)。将显示该行。此时点击Tab开始编辑。 - 使用带有
s///
修饰符的历史扩展。例如,!-2:s/--option25/--newoption/
将重新运行倒数第二个命令,但替换 option25。要修改最后一个./cmd
命令,请使用以下!string
语法:!./cmd:s/--option25/--newoption/
在替换中可以使用任何分隔符代替 /。 - 如果编辑上一行,您可以使用快速替换:
^--option25^--newoption
- 字符搜索。这是由大同提到的,并且可以在常规的Emacs模式来完成Ctrl+]向前搜索,和Ctrl+ Alt+]为向后搜索。
I recommend the second option. Ctrl+ris really handy and fast, no mucking about with editors, and you see the results before the command is run (unlike the history expansions).
我推荐第二种选择。Ctrl+r非常方便和快速,无需编辑器,您可以在运行命令之前看到结果(与历史扩展不同)。
回答by wonder.mice
To be clear, you don't want a "fast way to move the cursor on a terminalcommand line". What you actually want is a fast way to navigate over command line in you shellprogram.
需要明确的是,您不需要“在终端命令行上快速移动光标的方法”。您真正想要的是一种在shell程序中快速浏览命令行的方法。
Bashis very common shell, for example. It uses Readlinelibrary to implement command line input. And so to say, it is very convenient to know Readline bindings since it is used not only in bash. For example, gdb also uses Readline to process input.
例如,Bash是非常常见的 shell。它使用Readline库来实现命令行输入。可以这么说,了解 Readline 绑定非常方便,因为它不仅在 bash 中使用。例如,gdb 也使用 Readline 来处理输入。
In Readline documentation you can find all navigation related bindings (and more): http://www.gnu.org/software/bash/manual/bash.html#Readline-Interaction
在 Readline 文档中,您可以找到所有与导航相关的绑定(以及更多):http: //www.gnu.org/software/bash/manual/bash.html#Readline-Interaction
Short copy-paste if the link above goes down:
如果上面的链接失效,请进行简短的复制粘贴:
Bare Essentials
基本必需品
- Ctrl-b Move back one character.
- Ctrl-f Move forward one character.
- [DEL] or [Backspace] Delete the character to the left of the cursor.
- Ctrl-d Delete the character underneath the cursor.
- Ctrl-_ or C-x C-u Undo the last editing command. You can undo all the way back to an empty line.
- Ctrl-b 向后移动一个字符。
- Ctrl-f 向前移动一个字符。
- [DEL] 或 [Backspace] 删除光标左侧的字符。
- Ctrl-d 删除光标下方的字符。
- Ctrl-_ 或 Cx Cu 撤消上一个编辑命令。您可以一直撤消回到空行。
Movement
移动
- Ctrl-a Move to the start of the line.
- Ctrl-e Move to the end of the line.
- Meta-f Move forward a word, where a word is composed of letters and digits.
- Meta-b Move backward a word.
- Ctrl-l Clear the screen, reprinting the current line at the top.
- Ctrl-a 移至行首。
- Ctrl-e 移至行尾。
- Meta-f 向前移动一个单词,其中一个单词由字母和数字组成。
- Meta-b 向后移动一个词。
- Ctrl-l 清屏,在顶部重新打印当前行。
Kill and yank
杀死并猛拉
- Ctrl-k Kill the text from the current cursor position to the end of the line.
- M-d Kill from the cursor to the end of the current word, or, if between words, to the end of the next word. Word boundaries are the same as those used by M-f.
- M-[DEL] Kill from the cursor the start of the current word, or, if between words, to the start of the previous word. Word boundaries are the same as those used by M-b.
- Ctrl-w Kill from the cursor to the previous whitespace. This is different than M- because the word boundaries differ.
- Ctrl-y Yank the most recently killed text back into the buffer at the cursor.
- M-y Rotate the kill-ring, and yank the new top. You can only do this if the prior command is C-y or M-y.
- Ctrl-k 删除从当前光标位置到行尾的文本。
- Md Kill 从光标到当前词的结尾,或者,如果在词之间,到下一个词的结尾。单词边界与 Mf 使用的相同。
- M-[DEL] 从光标处删除当前单词的开头,或者,如果在单词之间,则删除到前一个单词的开头。字边界与 Mb 使用的边界相同。
- Ctrl-w 从光标处杀到前一个空格。这与 M- 不同,因为单词边界不同。
- Ctrl-y 将最近杀死的文本拉回光标处的缓冲区。
- 我的旋转击杀环,并猛拉新的顶部。如果先前的命令是 Cy 或 My,则只能执行此操作。
M is Meta key. For Max OS X Terminal you can enable "Use option as meta key" in Settings/Keyboard for that. For Linux its more complicated.
M 是元键。对于 Max OS X 终端,您可以为此在设置/键盘中启用“使用选项作为元键”。对于Linux,它更复杂。
Update
更新
Also note, that Readline can operate in two modes:
另请注意,Readline 可以在两种模式下运行:
- emacs mode (which is the default)
- vimode
- emacs 模式(默认)
- vi模式
To switch Bash to use vi mode:
切换 Bash 以使用 vi 模式:
$ set -o vi
Personaly I prefer vi mode since I use vim for text editing.
我个人更喜欢 vi 模式,因为我使用 vim 进行文本编辑。
Bonus
奖金
In macOS Terminal app (and in iTerm too) you can Option-Click to move the cursor (cursor will move to clicked position). This even works inside vim
.
在 macOS 终端应用程序中(以及在 iTerm 中),您可以通过 Option-Click 移动光标(光标将移动到单击的位置)。这甚至可以在vim
.
回答by Connecting life with Android
Hold down the Option key and click where you'd like the cursor to move, and Terminal rushes the cursor that precise spot.
按住 Option 键并单击您希望光标移动的位置,终端将光标移到精确的位置。
回答by paxdiablo
I tend to prefer vi editing mode (since those keystrokes are embedded into my spinal cord now (the brain's not used at all), along with the CTRL-K, CTRL-X from WordStar 3.3 :-). You can use the command line set -o vi
to activate it (and set -o emacs
to revert).
我倾向于使用 vi 编辑模式(因为这些按键现在已嵌入我的脊髓中(大脑根本没有使用),以及 WordStar 3.3 中的 CTRL-K、CTRL-X :-)。您可以使用命令行set -o vi
来激活它(并set -o emacs
恢复)。
In Vi, it would be (ESC-K to get the line up first of course) "f5;;B
" (without the double quotes).
在 Vi 中,它会是(当然首先使用 ESC-K 来排列)“ f5;;B
”(没有双引号)。
Of course, you have to understand what's on the line to get away with this. Basically, it's
当然,您必须了解在线上的内容才能摆脱这种情况。基本上,它是
f5 to find the first occurrence of "5" (in --option5).
; to find the next one (in --option15).
; to find the next one (in --option25).
B to back up to the start of the word.
Let's see if the emacs aficionados can come up with a better solution, less than 5 keystrokes (although I don't want to start a religious war).
看看emacs爱好者能不能想出更好的解决方案,少于5次击键(虽然我不想挑起宗教War)。
Have you thought about whether you'd maybe like to put this horrendously long command into a script? :-)
你有没有想过你是否想把这个非常长的命令放到一个脚本中?:-)
Actually, I can go one better than that: "3f5B
" to find the third occurrence of "5" then back up to the start of the word.
实际上,我可以做得更好:“ 3f5B
”找到“ 5”的第三次出现,然后返回到单词的开头。
回答by Edson Medina
Use Meta-b / Meta-f to move backward/forward by a word respectively.
使用 Meta-b / Meta-f 分别向后/向前移动一个单词。
In OSX, Meta translates as ESC, which sucks.
在 OSX 中,Meta 翻译为 ESC,这很糟糕。
But alternatively, you can open terminal preferences -> settings -> profile -> keyboard and check "use option as meta key"
但或者,您可以打开终端首选项 -> 设置 -> 配置文件 -> 键盘并选中“使用选项作为元键”
回答by dbr
After running the command once, run fc
运行一次命令后,运行 fc
It will launch $EDITOR
with the previous command, then you can use your regular editor to modify the command. When you save and exit, the file will be executed.
它将$EDITOR
使用上一个命令启动,然后您可以使用常规编辑器修改命令。当您保存并退出时,该文件将被执行。
..but, as Pax said - the command line isn't particularly good for editing absurdly long lines - why not make the command into a script?
..但是,正如 Pax 所说 - 命令行不是特别适合编辑荒谬的长行 - 为什么不将命令变成脚本?
回答by dbr
If you want to move forward a certain number of words, hit M-<n>
(M-
is for Meta and its usually the escape key) then hit a number. This sends a repeat argument to readline, so you can repeat whatever command you want - if you want to go forward then hit M-<n> M-f
and the cursor will move forward <n>
number of words.
如果你想向前移动一定数量的单词,点击M-<n>
(M-
用于 Meta 并且它通常是转义键)然后点击一个数字。这会向 readline 发送一个重复参数,因此您可以重复您想要的任何命令 - 如果您想前进,然后点击M-<n> M-f
,光标将向前移动<n>
单词数。
E.g.
例如
$|echo "two three four five six seven"
$ M-4
(arg: 4) echo "two three four five six seven"
$ M-f
$ echo "two three four| five six seven"
So for your example from the cursor at the beginning of the line you would hit, M-26 M-f
and your cursor would be at --option25|
-or- from the end of the line M-26 M-b
would put your cursor at --|option25
因此,对于您从行首的光标开始的示例,您将点击,M-26 M-f
并且您的光标将位于--option25|
- 或 - 从该行的末尾M-26 M-b
将您的光标置于--|option25
回答by Alexandr
Incremental history searching
增量历史搜索
in terminal enter:
在终端输入:
gedit ~/.inputrc
then copy paste and save
然后复制粘贴并保存
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char
all you need to do to find a previous command is to enter say the first 2 or 3 letters and upward arrow will take you there quickly say i want:
查找上一个命令所需要做的就是输入前 2 或 3 个字母,向上箭头将带您快速到达那里说我想要:
for f in *.mid ; do timidity "$f"; done
all i need to do is enter
我需要做的就是输入
fo
and hit upward arrow command will soon appear
并点击向上箭头命令很快就会出现
回答by Frank
One option is to use M-x shell
in emacs
. That provides all editing facilities and keystrokes that emacs
has, so C-s
can be used to search the text option25
, for example.
一种选择是M-x shell
在emacs
. 这提供了所有的编辑工具和按键emacs
,因此C-s
可用于搜索文本option25
,例如。
(But I'd still prefer to be in the real terminal shell instead if someone can point me to good search and edit facilities.)
(但如果有人可以为我指出良好的搜索和编辑工具,我仍然更喜欢在真正的终端外壳中。)
回答by user7610
Use the mouse
使用鼠标
Sometimes, the easiest way to edit a commandline is using a mouse. Some previous answers give a command to open your current line in your $EDITOR. For me (zhs with grml config) that combination is Alt+e
. If you enable mouse in your editor, you can make use of it.
有时,编辑命令行的最简单方法是使用鼠标。以前的一些答案给出了在 $EDITOR 中打开当前行的命令。对我来说(使用 grml 配置的 zhs)该组合是Alt+e
. 如果您在编辑器中启用鼠标,则可以使用它。
To enable mouse in Vim, add this to your ~/.vimrc
要在 Vim 中启用鼠标,请将其添加到您的 ~/.vimrc
set mouse=a
set ttymouse=xterm2
If you then want to do a text selection in terminal (instead of passing the mouseclick to vim), hold Shift when you click; this is terminal specific, of course.
如果您想在终端中进行文本选择(而不是将鼠标单击传递给 vim),请在单击时按住 Shift;当然,这是特定于终端的。
Sysadmins should not be afraid of the mouse.
系统管理员不应该害怕鼠标。