Bash replacement reg-exp

Is there a way to start the regexp-string replacement in the current line in bash?

I quite often find myself in a situation where I typed a long command line, and then I understand that I would like to change the word somewhere in the line.

My current approach is to end the line, press Ctrl + A (to go to the beginning of the line), insert # (to comment on the line), press enter, and then use ^oldword^newword ( ^oldword^newword executes the previous command after replacing the old word new word).

But there must be a better (faster) way for this. (A mouse is not possible since I am most often in ssh sessions).

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

Edit: I tried using vi-mode. Something strange has happened. Although I am a loving vim user, I had serious problems using my favorite bash. All these finger movements that were burned in my subconscious mind suddenly stopped working. I quickly returned to emacs-mode and thought about letting emacs try as my favorite editor (although I think the same thing may happen again).

+4
source share
3 answers

Unfortunately, no, actually not the best way. If you're just tired of keystrokes, you can use macros to crop them. Add the following to your ~/.inputrc :

 "\C-x6": "\Ca#\Cm^" "\C-x7": "\Cm\CP\Ca\Cd\Cm" 

Now, in a new bash instance (or after rebooting .inputrc in the current shell by pressing Cx Cr ), you can do the following:

  • Enter the fogus command (for example, ls abcxyz ).
  • Press Ctrl-x, then 6. The macro inserts a # at the beginning of the line, executes the commented line, and types your first ^ .
  • Enter your correction (e.g. xyz^def ).
  • Press Ctrl-x, then 7. The macro will complete your substitution, then go to the previous (commented) line, delete the comment symbol, and execute it again.

It is not entirely elegant, but I think it is the best you will get with readline.

+1
source

G'day

How about using vi mode? Just type set -o vi

Then you can go to the word you want to change and just make cw or cW depending on what is in the word?

Oh, I forgot to add that you enter ESC k to o in the previous line in the command history.

What do you usually use for the editor?

amuses Rob

Edit: I forgot to say in my original answer that you need to think about the vi command line in bash using the commands you enter when you are in ex mode in vi, i.e. after you entered the colon.

The worst part is that you need to navigate through the history of commands using the ancient commands vi h (left) and l (right). You can use w (or W) to bounce through words.

Once you get used to it, you will get all kinds of commands, for example. entering ESC / my_command will look back through your history, first of all, first to find the first appearance of the command line containing the text my_command. Once it detects this, you can use n to search for the next event, etc. And N to change the direction of the search.

I would try reading the bash man page to find out what is available in vi mode. Once you get to the point where the up arrow and down arrow will be replaced by ESC k and then j, you will see that vi mode offers more than emacs mode for editing command line in bash.

IMHO natchurly! (-:

Emacs Eighty megabytes and a permanent replacement!

amuses Rob

+3
source

in ksh, in vi mode, if you press 'v' while in command mode, it will trigger a full vi session in the contents of your current command line. Then you can edit using the full set of vi commands (global search and replace in your case). When: wq from vi, the edited command is executed. I am sure something like this exists for bash. Since bash tends to expand on its predecessors, something like this is possible.

+3
source

Source: https://habr.com/ru/post/1276438/


All Articles