Is it possible to move cursor when editing long bash command with search, not only with arrows ? Something like regex searching inside currently typing command. As I understand bash by themselves can't do that, but may be there are other convenient solutions ?
2 Answers
The best way achieving what you're after (if you're still wondering after ten months) is spawning the bash default editor (which can be changed with export EDITOR=vim
, on bash) using the shortcut:
Ctrl+x+e
(Hold Ctrl, then press x followed by e).
From bash man:
edit-and-execute-command (C-xC-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.
You'll find yourself into a vim (or vi, or nano, or other..) session, where you can write your command using every shortcut your editor provides. The downside of this approach is that, obviously, you'll lose bash shortcuts, so no tab-tab for completion during the session.
Honestly, when a command is too large, I like editing it in a text file myself, running it as a script. This allows you to test and change the command until it works, Ctrl+x+e creates instead a temp file, which is trashed once the command run.
-
Yes, I've been told already about C-xC-e. But your answer helps others. One more moment - after exiting editor the command automatically executed. Is it possible to stay in command line after editing in EDITOR? – Dmitry Perfilyev Mar 27 '18 at 09:40
-
There are ways to run a script from within vi (see https://stackoverflow.com/questions/953398/how-to-execute-file-im-editing-in-vim) but frankly I think that’s not so productive, I also have the feeling tou wouldn’t be able to see the output. When I find myself needing to run a long bash line, and I want run that multiple times without leaving vi, I just use tmux with the shortcut ctrl-B, Shift 2 - this will split your current screen in two horizontal pane. Then you can modify your line on vi, say on the upper pane, and run it on the lower where you can check the output. – nnsense Mar 28 '18 at 10:45
Personally what I do is CTRL+ArrowL/R it will move the cursor word by word instead of letter by letter. You can also use these arrows : ↖ ↘ on your keyboard to go at the beginning (respectively : the end) of the current line. The idea behind that, is to be more efficient in the way you edit your command line.
But I don't think there is any command search available... Or you can write it in a script and search with your text editor

- 1,072
- 8
- 17
-
1
-
Depends on the keyboard you have, but yes I think we're talking about the same keys – ShellCode May 15 '17 at 09:40
-
some terminals use CTRL+ArrowL/R (my for example to switching between tabs ), so I use ESC+F/ESC+B to move cursor by word forward/backward – Dmitry Perfilyev May 15 '17 at 11:48