tags:

views:

942

answers:

3

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

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.

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

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).

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

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).

+3  A: 

G'day,

What about using vi mode instead? Just enter set -o vi

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?

Oops, forgot to add you enter a ESC k to o to the previous line in the command history.

What do you normally use for an editor?

cheers, Rob

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.

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.

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.

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.

IMHO natchurly! (-:

Emacs? Eighty megs and constantly swapping!

cheers, Rob

Rob Wells
+2  A: 

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.

jj33
+1  A: 

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 ~/.inputrc:

"\C-x6": "\C-a#\C-m^"
"\C-x7": "\C-m\C-P\C-a\C-d\C-m"

Now, in a new bash instance (or after reloading .inputrc in your current shell by pressing C-x C-r), you can do the following:

  1. Type a bogus command (e.g., ls abcxyz).
  2. Press Ctrl-x, then 6. The macro inserts a # at the beginning of the line, executes the commented line, and types your first ^.
  3. Type your correction (e.g., xyz^def).
  4. 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.

It's not exactly elegant, but I think it's the best you're going to get with readline.

Zak Johnson