varedshell builtin invokes the ZSH line editor, to interactively edit variables. This is sometimes very useful when you have appended values to a variable but then recognize that you added something you didn't want to add. Instead of rebuilding all the contents of the variable you could just use
varedand clean up the mess.
Another use case for
vared would be to provide user interaction in shell scripts. Instead of just prompting the user to input something with the
read command you could also let him edit pre-defined values. Of course,
vared can also provide the user with an input prompt just like
read (use the flag
-p for a left-aligned prompt or
-r for a right-aligned prompt).
There are several situations, where
vared might come in handy, but I won't elaborate on all these. Instead let's come to a few (more or less) practical examples where I can show you how
vared basically works.
The first one is changing the prompt theme. The ZSH default prompt is quite boring (there aren't many things, Bash does better than ZSH, but that might be one of them). The current prompt is saved in the global shell parameter
$PROMPT defines the left prompt string (there is also a right prompt in
$RPROMPT which will be displayed on the right of the command line). You can start editing this variable by simply typing in
After you press <Enter>,
vared will automatically open an interactive editor for you to let you edit the string variable. This should look like the following line:
% vared PROMPT<Enter> %m%#
Now you can use the interactive editor to edit this line to create your own custom ZSH prompt (of course, this is much more useful when you already have a more or less complex prompt and you just want to edit parts of it). In the list below I've listed some important escape sequences for your prompt.
%n: expands to the value of
%m: the hostname
%~: the present working directory (
$PWD) where the home directory is represented as
%t: current time of day, in 12-hour, am/pm format
%T: current time of day, in 24-hour format
%*: current time of day in 24-hour format, with seconds
%w: the date in day-dd format
%W: the date in mm/dd/yy format
%D: the date in yy-mm-dd format
I replaced the default prompt theme with the following string:
And my right prompt displays the current date and time with
[%* on %W]
That's it, you now have a brandnew prompt! Just don't forget to put it in your
Now it's time to show you some more features of
vared. When the
-c flag is set,
vared will automatically create a new shell variable, if it doesn't already exist. The
-h flag let's you scroll through your history while you are using the line-editor.
Let's say you want to create a new alias for one of your last used commands:
sudo pacman -Syu
(this updates the package database on Arch Linux and upgrades the system)
Now you can use
vared to easily create a new alias name for this command. You just have to select the desired command from your history by pressing the <Up> and <Down> arrow keys while you are using the line-editor and hit <Enter> to set the variable.
% vared -c -h pmsyu<Enter> <Up>sudo pacman -Syu
After that you can use the shell variable $pmsyu to define an alias:
Of course, you could also do that without using a variable, but it's just an example.
Read more about vared: