Say I have written the following command but haven't yet pressed enter to execute it:

$ ls dir1 dir2 dir3

Is there a way to replace given characters without manually changing them in every location they are? For example, I'd like to press some shortcut, enter string to be replaced (say, dir) and then enter another string as its replacement (say 'directory`).


There's a replace-string autoloadable widget for that. Add to your ~/.zshrc:

autoload replace-string
zle -N replace-string
zle -N replace-string-again
bindkey '\eg' replace-string-again
bindkey '\er' replace-string

Then press Alt+r to invoke. Alt+g to repeat the last substitution. See info zsh replace-string for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.