<p>Is there a way to run a regexp-string replace on the current line in the bash?</p>

<p>I find myself rather often in the situation, where I have typed a long commandline and then realize, that I would like to change a word somewhere in the line.</p>

<p>My current approach is to finish the line, press <kbd>Ctrl</kbd>+<kbd>A</kbd> (to get to the start of the line), insert a # (to comment out the line), press enter and then use the <code>^oldword^newword</code> syntax (<code>^oldword^newword</code> executes the previous command after substituting oldword by newword).</p>

<p>But there has to be a better (faster) way to achieve this. (The mouse is not possible, since I am in an ssh-sessions most of the time).</p>

<p>Probably there is some emacs-like key-command for this, that I don't know about.</p>

<p>Edit: I have tried using vi-mode. Something strange happened. Although I am a loving vim-user, I had serious trouble using my beloved bash. All those finger-movements, that have been burned into my subconscious suddenly stopped working. I quickly returned to emacs-mode and considered, giving emacs a try as my favorite editor (although I guess, the same thing might happen again).</p>

[{'answer_id': 28228, 'author': 'Rob Wells', 'author_id': 2974, 'author_profile': 'https://Stackoverflow.com/users/2974', 'pm_score': 2, 'selected': False, 'text': '<p>G\'day,</p>\n\n<p>What about using vi mode instead? Just enter set -o vi</p>\n\n<p>Then you can go to the word you want to change and just do a cw or cW depending on what\'s in the word?</p>\n\n<p>Oops, forgot to add you enter a ESC k to o to the previous line in the command history.</p>\n\n<p>What do you  normally use for an editor?</p>\n\n<p>cheers,\nRob</p>\n\n<p>Edit: What I forgot to say in my original reply was that you need to think of the vi command line in bash using the commands you enter when you are in "ex" mode in vi, i.e. after you\'ve entered the colon.</p>\n\n<p>Worst thing is that you need to move around through your command history using the ancient vi commands of h (to the left) and l (to the right). You can use w (or W) to bounce across words though.</p>\n\n<p>Once you get used to it though, you have all sorts of commands available, e.g. entering ESC / my_command will look back through you r history, most recent first, to find the first occurrance of the command line containing the text my_command. Once it has found that, you can then use n to find the next occurrance, etc. And N to reverse the direction of the search.</p>\n\n<p>I\'d go have a read of the man page for bash to see what\'s available under vi mode. Once you get over the fact that up-arrow and down-arrow are replaced by ESC k, and then j, you\'ll see that vi mode offers more than emacs mode for command line editing in bash.</p>\n\n<p>IMHO natchurly! (-:</p>\n\n<p>Emacs? Eighty megs and constantly swapping!</p>\n\n<p>cheers,\nRob</p>\n'}, {'answer_id': 28389, 'author': 'jj33', 'author_id': 430, 'author_profile': 'https://Stackoverflow.com/users/430', 'pm_score': 2, 'selected': False, 'text': "<p>in ksh, in vi mode, if you hit 'v' while in command mode it will spawn a full vi session on the contents of your current command line.  You can then edit using the full range of vi commands (global search and replace in your case).  When :wq from vi, the edited command is executed.  I'm sure something similar exists for bash.  Since bash tends to extend its predecessors, there's probably something similar.</p>\n"}, {'answer_id': 113250, 'author': 'Zak Johnson', 'author_id': 17052, 'author_profile': 'https://Stackoverflow.com/users/17052', 'pm_score': 2, 'selected': True, 'text': '<p>Unfortunately, no, there\'s not really a better way.  If you\'re just tired of making the keystrokes, you can use macros to trim them down.  Add the following to your <code>~/.inputrc</code>:</p>\n\n<pre><code>"\\C-x6": "\\C-a#\\C-m^"\n"\\C-x7": "\\C-m\\C-P\\C-a\\C-d\\C-m"\n</code></pre>\n\n<p>Now, in a new bash instance (or after reloading <code>.inputrc</code> in your current shell by pressing <code>C-x C-r</code>), you can do the following:</p>\n\n<ol>\n<li>Type a bogus command (e.g., <code>ls abcxyz</code>).</li>\n<li>Press Ctrl-x, then 6.  The macro inserts a <code>#</code> at the beginning of the line, executes the commented line, and types your first <code>^</code>.</li>\n<li>Type your correction (e.g., <code>xyz^def</code>).</li>\n<li>Press Ctrl-x, then 7.  The macro completes your substitution, then goes up to the previous (commented) line, removes the comment character, and executes it again.</li>\n</ol>\n\n<p>It\'s not exactly elegant, but I think it\'s the best you\'re going to get with readline.</p>\n'}]
['https://Stackoverflow.com/questions/28224', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1870/']