0

Say for instance I have the following in my command line history:

systemctl status puppet.service

And I want to re-execute it with status changed to start:

systemctl start puppet.service

Is there a way I can %s/tus/art/g with keyboard shortcuts or something?

3
  • 2
    Would opening the current line up in an editor help you? Esc+v in vi-mode, and Ctrl+x+Ctrl+e in emacs-mode. It would use the editor from $VISUAL.
    – Kusalananda
    Feb 13, 2021 at 18:00
  • 2
    Is it worth the effort? With ctrl+← twice, ctrl+w you delete the word "status", then type "start". Feb 13, 2021 at 18:02
  • 2
    define an alias if you do this more often is your best choice ever Feb 13, 2021 at 18:06

1 Answer 1

2

You can retype the last command with this shortcut:

!!

Then you can do inplace substitution with :

:s/from/to

Put it all together become :

!!:s/from/to/

Previously digited command can be referenced with !-n and the substitution occurs as well:

!-1:s/from/to/

If you want to refer to a command with a string another shortcut that would be usefull:

!?cmd?:s/from/to

which in you case become :

!?systemctl?:s/tus/art/g
5
  • 1
    This does not work on what's typed on the current command line, but on lines available through the command line history. That means that you have to have executed the command at least once to be able to recall it and modify it.
    – Kusalananda
    Feb 13, 2021 at 20:11
  • Yes. My fault. I have missunderstood the question. Feb 13, 2021 at 21:34
  • Well, the fact that the answer was accepted leads me to believe that the user didn't really know what they were asking for.
    – Kusalananda
    Feb 13, 2021 at 21:35
  • 1
    @Kusalananda I changed the question title to make it more relevant to the answer.
    – leeand00
    Feb 15, 2021 at 12:01
  • 1
    @mondo-cane Also, ^status^start^ would do the same as !!:s/status/start/ (the manual calls this "quick substitution"). Note that this affects only the most recent command in the history.
    – Kusalananda
    Feb 15, 2021 at 12:05

You must log in to answer this question.

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