Development Tip

OS X 터미널에서 커서를 단어 단위로 이동하는 방법이 있습니까?

yourdevel 2020. 9. 29. 18:45
반응형

OS X 터미널에서 커서를 단어 단위로 이동하는 방법이 있습니까?


나는 조합 알고 Ctrl+는 A현재 명령의 시작 부분으로 이동하고, Ctrl+ E끝으로 이동합니다.

하지만 Cocoa 애플리케이션의 Alt+ / 처럼 단어 단위로 점프하는 방법 이 있습니까?


상자 밖에서 매우 기괴한 Esc+ F사용하여 다음 단어 의 시작으로 이동 하고 Esc+ B를 사용하여 현재 단어의 시작으로 이동할 수 있습니다.


Mac OS X에서는 다음 키보드 단축키가 기본적으로 작동합니다. 터미널 환경 설정 (키보드 탭 아래)에서 옵션 키가 메타 처럼 작동하도록 해야합니다.

  • alt (⌥)+ F점프 F의 단어로 orward을
  • alt (⌥)+ B점프 B의 단어로 ackward을

간단한 텍스트 탐색을위한 기본 emacs 키 바인딩이 bash 셸에서 작동하는 것처럼 보입니다. 당신이 사용할 수있는

  • alt (⌥)+ D현재 커서 위치에서 시작하는 단어 삭제
  • ctrl+ A줄의 시작으로 이동
  • ctrl+ E줄 끝으로 이동
  • ctrl+ K커서 위치에서 시작하는 줄을 죽입니다.
  • ctrl+ Y킬 버퍼에서 텍스트 붙여 넣기
  • ctrl+ R기록에서 과거에 입력 한 명령을 역 검색하려면
  • ctrl+ S검색을 전달하려면 (zsh에서 작동하지만 bash에서는 작동하지 않음)
  • ctrl+ F문자로 앞으로 이동
  • ctrl+ B문자만큼 뒤로 이동
  • ctrl+ W커서 위치에서 단어를 뒤로 제거합니다.

방법은 다음과 같습니다.

기본적으로 터미널에는 단어별로 (왼쪽 및 오른쪽) 이동할 수있는 바로 가기가 있습니다.

  • esc+ B(왼쪽)
  • esc+ F(오른쪽)

alt+ 를 구성 하고 해당 시퀀스를 생성 할 수 있습니다 .

  • 터미널 환경 설정 열기 ( cmd+ ,);
  • 설정 탭에서 키보드를 선택하고있는 경우 두 번 클릭하고 ⌥ ←없는 경우 추가합니다.
  • 수정자를 원하는대로 설정하고 상자에 단축키 esc+를 입력 B하여 텍스트를 생성합니다 \033b(이 텍스트를 수동으로 입력 할 수 없음).
  • 오른쪽 단어에 대해 반복 ( esc+ F\033f)

또는 textmate에서이 블로그 게시물을 참조 할 수 있습니다.

http://blog.macromates.com/2006/word-movement-in-terminal/


iTerm2로 전환 하십시오 . 평범한 오래된 터미널보다 무료이며 훨씬 좋습니다. 또한 키보드 단축키와 같은 사용자 정의를위한 더 많은 옵션이 있습니다.

또한 cmd1-9를 사용 하여 탭간에 전환 할 수 있다는 점이 마음 에 듭니다. 그것을 시도하면 일반 터미널로 돌아 가지 않을 것입니다 :)

iterm2에서 사용자 정의 키보드 기본 설정을 구성하는 방법

  • iTerm2 설치
  • 시작하고 기본 설정 창으로 이동합니다.
  • 키보드 프로필 탭 선택
  • 프로필을 새로운 항목에 복사 한 다음 ^+ Right/ 와 같은 화살표 키 바로 가기를 삭제 Left하거나 백업에 관심이없는 경우 기본 프로필에서 삭제하면됩니다.
  • 다음으로 수정 된 프로필이 선택되었는지 확인합니다 (별표 표시).

그림 1.png

  • 이제 키보드 탭 (맨 위 행)을 선택합니다.

iTerm 2

  • 더하기 버튼을 클릭하여 새 키보드 단축키를 추가하십시오.
  • In the first box type CMD+Left arrow
  • In the second box choose "send escape code"
  • In the third box type the letter B

그림 2.png

  • Repeat with desired key combinations. escape+B moves one word to the left, escape+f moves one word to the right.
  • you may also wish to set up cmd+d to delete the word in front of the cursor with escape+d

I often hit the wrong button (cmd / control / alt) with an arrow key and so i have my arrow key combinations with those buttons all set to jump forward and back words, but please do what fits you best.


Actually there is a much better approach. Hold option ( alt on some keyboards) and press the arrow keys left or right to move by word. Simple as that.

option
option

Also ctrle will take you to the end of the line and ctrla will take you to the start.


I have Alt+/ working: open Preferences » Settings » Keyboard, set the entry for option cursor left to send string to shell: \033b, and set option cursor right to send string to shell: \033f. You can also use this for other Control key combinations.


Actually it depends on what shell you use, however most shells have similar bindings. The bindings you are referring to (e.g. Ctrl+A and Ctrl+E) are bindings you will find in many other programs and they are used for ages, BTW also work in most UI apps.

Here's a look of default bindings for Bash:

Most Important Bash Keyboard Shortcuts

Please also note that you can customize them. You need to create a file, name as you wish, I named mine .bash_key_bindings and put it into my home directory. There you can set some general bash options and you can also set key bindings. To make sure they are applied, you need to modify a file named ".bashrc" that bash reads in upon start-up (you must create it, if it does not exist) and make the following call there:

bind -f ~/.bash_key_bindings

~ means home directory in bash, as stated above, you can name the file as you like and also place it where you like as long as you feed the right path+name to bind.

Let me show you some excerpts of my .bash_key_bindings file:

set meta-flag on
set input-meta on
set output-meta on
set convert-meta off
set show-all-if-ambiguous on
set bell-style none
set print-completions-horizontally off

These just set a couple of options (e.g. disable the bell; this can be all looked up on the bash webpage).

"A": self-insert
"B": self-insert
"C": self-insert
"D": self-insert
"E": self-insert
"F": self-insert
"G": self-insert
"H": self-insert
"I": self-insert
"J": self-insert

These make sure that the characters alone just do nothing but making sure the character is "typed" (they insert themselves on the shell).

"\C-dW": kill-word
"\C-dL": kill-line
"\C-dw": backward-kill-word
"\C-dl": backward-kill-line
"\C-da": kill-line

This is quite interesting. If I hit Ctrl+D alone (I selected d for delete), nothing happens. But if I then type a lower case w, the word to the left of the cursor is deleted. If I type an upper case, however, the word to the right of the cursor is killed. Same goes for l and L regarding the whole line starting from the cursor. If I type an "a", the whole line is actually deleted (everything before and after the cursor).

I placed jumping one word forward on Ctrl+F and one word backward on Ctrl+B

"\C-f": forward-word
"\C-b": backward-word

As you can see, you can make a shortcut, that leads to an action immediately, or you can make one, that just inits a character sequence and then you have to type one (or more) characters to cause an action to take place as shown in the example further above.

So if you are not happy with the default bindings, feel free to customize them as you like. Here's a link to the bash manual for more information.


If you check Use option as meta key in the keyboard tab of the preferences, then the default emacs style commands for forward- and backward-word and ⌥F (Alt+F) and ⌥B (Alt+B) respectively.

I'd recommend reading From Bash to Z-Shell. If you want to increase your bash/zsh prowess!


If you happen to be a Vim user, you could try bash's vim mode. Run this or put it in your ~/.bashrc file:

set -o vi

By default you're in insert mode; hit escape and you can move around just like you can in normal-mode Vim, so movement by word is w or b, and the usual movement keys also work.


As of Mac OS X Lion 10.7, Terminal maps Option-Left/Right Arrow to Esc-b/f by default, so this is now built-in for bash and other programs that use these emacs-compatible keybindings.


In Bash, these are bound to Esc-B and Esc-F. Bash has many, many more keyboard shortcuts; have a look at the output of bind -p to see what they are.


Use Natural Text Editing preset!

여기에 이미지 설명 입력

Essentially it binds, among other key sequences, Option + LeftArrow to ^[b sequence and Option + RightArrow to ^[f

This works in fish and bash, as well as in psql terminal.


Under iterm2's Preferences > Profile > Keys, you click the + below Key Mappings and record a new shortcut. For Action, select Send Escape Sequence and type b or f for backwards and forwards respectively.

When I tried to record one for (Ctrl+), I noticed in the Keyboard Shortcut field that the arrow never showed up. Turns out I had to disable the default mac's System Preferences > Keyboard > Shortcuts > Mission Control shorcuts first to get things to work, as they'll override iterm2's default shortcuts. Should be true for the standard terminal app, too.

키보드 시스템 환경 설정


Hold down the Option key and click where you'd like the cursor to move


As answered previously, you can add set -o vi in your ~/.bashrc to use vi/vim key bindings, or else you can add following part in .bashrc to move with Ctrl and arrow keys:

# bindings to move 1 word left/right with ctrl+left/right in terminal, just some apple stuff!
bind '"\e[5C": forward-word'
bind '"\e[5D": backward-word'
# bindings to move 1 word left/right with ctrl+left/right in iTerm2, just some apple stuff!
bind '"\e[1;5C": forward-word'
bind '"\e[1;5D": backward-word'

To start effect of these lines of code, either source ~/.bashrc or start a new terminal session.

P.S. If you are adding to ~/.bash_profile or ~/.profile then do not forget to replace all instances of .bashrc with your modified file.


New answer for iTerm2 Build 3.3.4 users:

Step 1: (macOS X) System Preferences > Keyboard > Shortcuts tab > Select Mission Control (left panel) > Uncheck shortcuts that labeled as "Move left a space" and "Move right a space"

Step 2: (iTerm2 Build 3.3.4) Preferences > Profiles > Select * Default (left panel) > Keys tab > Delete both "⌥->" and "⌥<-" entries > Set both "Left Option (⌥) Key:" and "Right Option (⌥) Key:" to Esc+

No messing around with shell profiles, no messing around with inferior masOS (default) Terminal, no awkwards Esc+F/B, rinse & repeat non-sense.

Done deal!!!

동료 프로그래머 여러분,이 팁을 즐기십시오!

참고 URL : https://stackoverflow.com/questions/81272/is-there-any-way-in-the-os-x-terminal-to-move-the-cursor-word-by-word

반응형