3

I often find myself in a long command-line, where it can be a pain to navigate to a specific location in the middle by means of Left-Arrow, Right-Arrow, Alt+B, Alt+F etc.

I know that using tmux, I can move to a particular <keyword> by means of a search. Since this is a very common operation, is something similar implemented in contemporary terminals like gnome-terminal?

Bonus: it would be nice to get an answer for macOS's iTerm too.

8
  • not precisely what you're after, but bash allows you to press Ctrl-X Ctrl-E to edit the current command line in your preferred text editor (set with the $EDITOR or $VISUAL variables). Some other shells have a similar feature.
    – cas
    Jul 29, 2019 at 12:19
  • 1
    Are you editing the shell command line, or are you looking for some output that's scrolled by? Jul 29, 2019 at 12:22
  • set editing-mode vi in your ~/.inputrc (or just type set -o vi to test this) and enjoy vi navigation in your command line: Leave input mode with Esc and use commands like 42| to move to column 42 or f; to jump to the next semicolon and so on ...
    – Philippos
    Jul 29, 2019 at 12:34
  • @Gilles - I'm editing the shell command line.
    – Shuzheng
    Jul 29, 2019 at 13:06
  • @Philippos - Nice, what terminals does this apply to? I'm not to keen about what .inputrc is really for - will you elaborate? Just to be sure, it is the terminal that carries out the vi navigation, right? It's not the the shell.
    – Shuzheng
    Jul 29, 2019 at 13:09

2 Answers 2

4

Bash (and any other terminal application that uses the readline library) has search functionality. Command line edition is done by the shell, not by the terminal. (See What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'?).

The main search commands are Ctrl+S and Ctrl+R, which search forward and backward respectively. These are incremental searches: after pressing Ctrl+S or Ctrl+R, type the text you want to search and you'll be brought to the next/previous occurrence of what you've typed so far.

Press any key that doesn't insert a character and that isn't Backspace to end search mode. Note that the key will have its usual effect, in particular Enter runs the command immediately. Left/Right are usually the most convenient way.

If you want to cancel the search, press Ctrl+G and you'll be returned to the command you were editing. These commands search the shell command history as well as the current command line. If you accidentally drift to a previous command line, Ctrl+G returns you to what you were typing originally.

Bash also has commands to search a single character without entering a search mode: Ctrl+] forward, Ctrl+Alt+] or Alt+- Ctrl+] backward.

Zsh has similar commands (and quite a few more). Its commands to search a single character quickly aren't bound to a convenient key by default (Ctrl+X Ctrl+F forward and none backward) unless you're in vi mode, but you can bind a key to them with bindkey.

5
  • How can the shell move the cursor? The shell is just an application that gets input, whenever the user enters a command-line and presses ENTER? It's the terminal that is responsible for the I/O environment that we enter commands into e.g. Gnome terminal? That's how I understand your post wrt. TTYs.
    – Shuzheng
    Jul 29, 2019 at 13:53
  • @Shuzheng The shell sends the terminal an escape sequence. The terminal interprets that particular escape sequence as an instruction to move the cursor. All modern shells use the terminal in a mode where they receive input on each character, not just after a whole line. Jul 29, 2019 at 14:14
  • 1
    Ohh, so the shell can actually communicate with the terminal, not just sending it command output? When you refer to escape sequences, they are also used for coloring output (I've seen those before). In this case, the escape sequence instead tells the terminal "move the cursor to position X" instead of "color this output red"? The shell manipulates the terminal, after the terminal launches it, by means of /dev/tty (controlling terminal) or stty?
    – Shuzheng
    Jul 29, 2019 at 14:30
  • for some reason the commands to search a single character doesn't work for my Bash. Also, after pressing Ctrl+R and entering the text I want to search for (say, foobar), why can't I delete of said text to enter some new text to search for? After deleting all of foobar in search mode, the search fails?
    – Shuzheng
    Jul 31, 2019 at 6:40
  • @Shuzheng You can use Backspace in search mode. If you delete all characters, you're still in search mode. Press Ctrl+G to exit search mode. Jul 31, 2019 at 6:51
2

With bash you can use edit-and-execute-command (Ctrl+x, Ctrl+e) to edit the command in your favorite editor:

   edit-and-execute-command (C-x C-e)
          Invoke  an editor on the current command line, and execute the result as shell commands.  
          Bash attempts to invoke $VISUAL, $EDITOR, and emacs as the editor, in that order.

How to move to a specific location depends on your editor. If you are using vim you can then position the cursor to the exact word using the vim-easymotion plugin.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .