terminal 光标移动原理和快捷键

日常跟终端打交道的过程中会遇到光标移动的场景,作为提升效率的快捷键值得学习。另外,除了终端支持这样快速移动其他场景也可能会支持,这里我不做过多的总结,实际场景尝试一下快捷键就知道是否可以了。目前可以确定是 bash、zsh、sh 等。

另外关于 terminal 光标移动底层原理可以简单介绍一下,光标移动等都来自一个底层库—— readline 实现的,有兴趣可以看一下 GNU 的 docs

按键符号

key name key symbol platform
ctrl key macos + windows
option key macos
command key macos
alt key windows
💡
Windows 的 Alt key 和 macOS 的 Option key 是对应关系

光标移动

key binding Command Comments
⌃ + A Ctlr + A moves the cursor to the start of a line
⌃ + E Ctlr + E moves the cursor to the end of the line
⌃ + B / -> Ctrl + B moves the cursor back one character at a time
⌃ + F / <- Ctrl + F moves the cursor forward one character at a time
⌥ + B Option + B moves the cursor back one word at a time
⌥ + F Option + F moves the cursor forward one word at a time

光标删除移动

key binding Command Comments
⌥ + W Option + W Kill the word behind point. Word boundaries are the same as those used by backward-word.
⌥ + D Option + D Kill from point 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 forward-word.
⌃ + W Ctlr + W Kill the word behind point, using white space as a word boundary. The killed text is saved on the kill-ring.

References

  1. linux命令行常用光标移动快捷键 - 简书
  2. Bash Reference Manual
  3. readline(3) - Linux manual page