web-dev-qa-db-ja.com

macOSのbashでターミナルキーバインドを使用する

MacOSのシェルのキーボードショートカットを習得しようとしていましたが、 ALT+B、 うまく行かなかった。

シェルでキーバインドをどのように発見、設定、使用しますか?チートシートがあれば役に立ちます。

51
Open the way

OS X端末で機能しない「メタ」キーシーケンスを処理する1つの方法は、特定の文字シーケンスを特定のキープレスに割り当てることです。米国以外のキーボードを使用している私たちにとって、これは他の回答のコメントで言及されている「メタとしてオプションを使用する」設定よりも多くの場合、より良い解決策です。 (多くの国際的なMacキーボードは、特定の重要な文字が使用できないため、オプション/ Altキーがないと、基本的に開発作業に使用できません。 # 英国のMacキーボードなど)

Word左とWord右をbashで機能させるために、ターミナルの設定の「キーボード」セクションを使用しました。特定のキーを押したときに特定のコードシーケンスを生成するように指示できます。私は私のように設定しました alt+\033b(実際には2文字です。Esc、次に小文字のb)を生成し、 alt+\033fを生成します(つまり、Esc f)。これにより、オプションキーを押したまま矢印キーを使用して、Wordを左右に動作させることができます。

私がまだ解決していないのは、 Esc 動作の鍵-理論的には「メタ」シーケンスにそれを使用できるはずですが、動作しないようです。 (つまり、タイプするだけ Esc+b 1ワード戻ります。)

USキーボードレイアウト、またはAppleが実際に必要なすべてのキーを提供するのに適していると思われるその他のキーボードを使用している場合、他の人が提案したように、「オプションをメタキーとして使用する」(ターミナルの設定のキーボードセクションでも)メタキーの任意の組み合わせにアクセスできるため、おそらくより良い選択です。 Alt+b 期待どおりに動作します。

12
Ian Griffiths

Mac OS XのターミナルはBASHです。BASHショートカットをいくつか示します。

Ctrl + A    Go to the beginning of the line you are currently typing on
Ctrl + E    Go to the end of the line you are currently typing on
Ctrl + L    Clears the Screen, similar to the clear command
Ctrl + U    Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H    Same as backspace
Ctrl + R    Let’s you search through previously used commands
Ctrl + C    Kill whatever you are running
Ctrl + D    Exit the current Shell
Ctrl + Z    Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W    Delete the Word before the cursor
Ctrl + K    Clear the line after the cursor
Ctrl + T    Swap the last two characters before the cursor
Esc + T  Swap the last two words before the cursor
Alt + F  Move cursor forward one Word on the current line
Alt + B  Move cursor backward one Word on the current line
Tab      Auto-complete files and folder names

あなたが探しているのは CtrlH。 (これはバックスペースキーを押すのと同じです)

1文字前に戻るエスケープ文字を探している場合は、\bを探しています。のように:

$ echo -e "one two\b\b\b\b three" # Will echo "one three"
81
Arko

私のUK Macでは、ALT +カーソルを左/右に動かすと、1ワード前/後ろに移動します。絶対に不可欠です。

10
jmoz

SS64を試してください。このWebサイトは、コマンドラインリファレンスに最適です。

このSS64ページから取得したOS X CLIキーボードショートカット。

Bash Keyboard Shortcuts

Moving the cursor:
  Ctrl + a   Go to the beginning of the line (Home)
  Ctrl + e   Go to the End of the line (End)
  Ctrl + p   Previous command (Up arrow)
  Ctrl + n   Next command (Down arrow)
   Alt + b   Back (left) one Word
   Alt + f   Forward (right) one Word
  Ctrl + f   Forward one character
  Ctrl + b   Backward one character
  Ctrl + xx  Toggle between the start of line and current cursor position

Editing:
 Ctrl + L   Clear the Screen, similar to the clear command
 Ctrl + u   Cut/delete the line before the cursor position.
  Alt + Del Delete the Word before the cursor.
  Alt + d   Delete the Word after the cursor.
 Ctrl + d   Delete character under the cursor
 Ctrl + h   Delete character before the cursor (backspace)
 Ctrl + w   Cut the Word before the cursor to the clipboard.
 Ctrl + k   Cut the Line after the cursor to the clipboard.
  Alt + t   Swap current Word with previous
 Ctrl + t   Swap the last two characters before the cursor (typo).
 Esc  + t   Swap the last two words before the cursor.
 Ctrl + y   Paste the last thing to be cut (yank)
  Alt + u   UPPER capitalize every character from the cursor to the end of the current Word.
  Alt + l   Lower the case of every character from the cursor to the end of the current Word.
  Alt + c   Capitalize the character under the cursor and move to the end of the Word.
  Alt + r   Cancel the changes and put back the line as it was in the history (revert).
 Ctrl + _   Undo

 TAB        Tab completion for file/directory names
For example, to move to a directory 'sample1'; Type cd sam ; then press TAB and ENTER. 
type just enough characters to uniquely identify the directory you wish to open.

History:
  Ctrl + r   Recall the last command including the specified character(s)
             searches the command history as you type.
             Equivalent to : vim ~/.bash_history. 
  Ctrl + p   Previous command in history (i.e. walk back through the command history)
  Ctrl + n   Next command in history (i.e. walk forward through the command history)
   Alt + .   Use the last Word of the previous command
  Ctrl + s   Go back to the next most recent command.
            (beware to not execute it from a terminal because this will also launch its XOFF).
  Ctrl + o   Execute the command found via Ctrl+r or Ctrl+s
  Ctrl + g   Escape from history searching mode

Process control:
 Ctrl + C   Interrupt/Kill whatever you are running (SIGINT)
 Ctrl + l   Clear the screen
 Ctrl + s   Stop output to the screen (for long running verbose commands)
 Ctrl + q   Allow output to the screen (if previously stopped using command above)
 Ctrl + D   Send an EOF marker, unless disabled by an option, this will close the current Shell (EXIT)
 Ctrl + Z   Send the signal SIGTSTP to the current task, which suspends it.
            To return to it later enter fg 'process name' (foreground).

Emacs mode vs Vi Mode    
All the above assume that bash is running in the default Emacs setting, if you prefer this can be switched to Vi shortcuts instead.

Set Vi Mode in bash:
$ set -o vi

Set Emacs Mode in bash:    
$ set -o emacs

注:Altキーのショートカットを使用するには>>ターミナルの設定を開く>>設定タブ>>キーボード>>「オプションをメタキーとして使用」にチェックマークを付けます

Terminal Preferences

6
Rahul Thakur

bash(1) manページの[〜#〜] readline [〜#〜]セクションが必要です。

man 1 bash
/^READLINE

マップする必要は本当にありません option OS Xターミナルでのメタキー option+left-arrow、 option+right-arrow 代わる Alt+F そして Alt+B それぞれ。その他の一般的に使用されるショートカットはすべて、他のプラットフォームに実装されているとおりに機能します。 esc の元の機能をエミュレートできます Alt キーが、それを使用して少し不快になることができます esc 修飾子として、あなたは保持することができません esc そして繰り返し押す B または F、あなたはそれを手放す必要があり、毎回それを再び押し下げます。

TL; DR

option+left-arrow = 1ワード戻る

option+right-arrow = 1ワード転送

1
Dan Robson

あなたは私のプロフィールに興味があるかもしれません: https://github.com/lingtalfi/mac-terminal-shortcuts

次の(直感的な)ショートカットを提供します。

ALT-left: move one Word backward
ALT-right: move one Word forward
CTRL-left: move to the beginning of the line
CTRL-right: move to the end of the line
ALT-backspace: kill one Word backward
ALT-del: kill one Word forward
ALT-up: set Word after cursor to uppercase
ALT-down: set Word after cursor to lowercase
CTRL-backspace: Same as ALT-backspace
CTRL-del: Same as ALT-del
home: move to the beginning of the line
end: move to the end of the line
0
ling