简体   繁体   中英

Textmate-like Keyboard Navigation for the Command Line?

How do I better navigate the command line? In TextMate, I can do:

  • CMD + ALT + ARROW to switch tabs

  • CMD + SHIFT + ARROW to highlight lines of text

  • ALT + SHIFT + ARROW to highlight words

  • ALT + ARROW to jump words, etc.

How do I do this kind of stuff in the terminal?

Here's a slightly more complete mapping from Mac text navigation to the default Readline keys: iTerm键映射

I set this up in the Keys pane of my iTerm2 preferences, but you should be able to make the same mappings in Terminal.app (Edit: Everything except the first two. Terminal.app only lets you pick from a few different keys)

In MacOS' Terminal app, you can change your keyboard bindings; go to Terminal Preferences (shortcut Cmd + , ) and navigate to the 'Keyboard' tab:

Key => Action
control cursor left => \033b
control cursor right => \033f

etc. You can do the same thing with other emacs-style bindings. As far as switching tabs, that's a System Preferences > Keyboard > Keyboard Shortcuts setting; click 'Application Shortcuts' on the left side, and then click + on the right to add a new shortcut. Use your desired keyboard combo and pair it with the exact menu name, which in Terminal for moving forward/backward through tabs is 'Select Next Tab' and 'Select Previous Tab' (via the Window menu in Terminal), respectively.

Does that help?

This is a pointer for anyone landing here looking for the equivalent tips for iTerm (as opposed to the default terminal).

To do the same in iTerm, go to menu "Bookmarks", "Manage Profiles", "Keyboard profiles", "Global".

Then add all of the shortcuts you see in the other answers here, but instead of sending text "\\033..." when creating a mapping, you simply select the action "send character sequence" and the character for the escape sequence!.

  • "\\033b" becomes b
  • "\\033f" becomes f
  • and so on.

Escape sequence example

Some things can be done by editing the readline bindings (see bind -p / man bash ) but for some of the mac keyboard keys you can (or need to?) set extras up in Terminal.app's preferences.

For word navigation try this:

Terminal preferences -> settings -> keyboard

add the following:

  • option cursor left \\033b (escape b) (backwards word)
  • option cursor right \\033f (escape f) (forward word)
  • option forward delete \\033d (escape d) (kill word)

Word boundaries behave a little differently, but it's better than single character or whole line navigation, and doesn't require trading your option key for a meta key and learning a bunch of new key combos.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM