I'm editing my PS1 prompt and I don't want to keep sourcing my bashrc to update it (since my bashrc resets other things too). I also don't want to copy and paste it over and over. Is there a way to edit the variable interactively, like using nano
(my default editor)?
The PS1 is mostly an example, because I want to do the same thing with other vars. The reason I want to do it interactively is that the variable is already quite long, and I just need to edit a few characters in it. And I only need to change it in the current shell.
This can be done. Type
var=$var
and then expand and edit it. To expand, use Esc+Ctrle (the default shortcut, check output ofbind -p | grep shell-expand-line
to confirm).So:
will become:
Which you can then edit in an editor with Ctrlx Ctrle (
edit-and-execute-command
in readline terms). When you save and quit, the saved content will be executed by the shell.If you already have
PS1=...
in your history, you can just go back to that and Ctrlx Ctrle.From the bash manual:
Instead of sourcing the bashrc, source another file, which just contains the variable.
Create it:
Note: If the variable contains single quotes, you will need to use a command that can escape them, e.g.:
Open it in an editor, e.g.
nano /tmp/PS1
.nano
over and over, you could run it in another terminal/TTY, or use a graphical editor.Make your changes and save.
Source it:
Repeat steps 3 and 4 as needed.
Zsh has a builtin called
vared
that lets you edit a variable inline. I wrote my own version, originally posted on Super User:Note this has some subtle differences from the Zsh builtin, e.g:
vared $
)Then to edit the PS1, just run
vared PS1
.