ZSH Gem #7: Editing variables with vared

Posted by | Comments (2) | Trackbacks (0)
Today I'm showing you how to edit variables by using vared. The vared shell 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 vared and 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. $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

vared PROMPT

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 $USERNAME
  • %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: [%n@%m|%~/] 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 .zshrc ;-).

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:

alias pmsyu=$pmsyu

Of course, you could also do that without using a variable, but it's just an example.

Read more about vared:

Trackbacks

No Trackbacks for this entry.

Comments

There have been 2 comments submitted yet. Add one as well!
Matt
Matt wrote on : (permalink)

Your alias command can be done more easily with !!, like this:
sudo pacman -Syu
alias “pm-syu=!!”

My prompt string:
F{def}%(?..%B%K{red}[?]%K{def}%b )%(1j.%b%K{yel}%F{bla}%jJ%F{def}%K{def} .)%F{yellow}%B%*%b %F{m}%m:%F{yellow}%~ %B%(!.#.>)%b %F{def}
Exit status indicator (if not 0), number of background jobs (if not 0), time, hostname, directory, prompt character.

Frank Hermann
Frank Hermann wrote on : (permalink)

Thank you for your comment!
I know that you can define an alias much easier than I did in my article. But I just needed an example for using the history flag with vared.
Your prompt string looks really confusing for me. Could you explain to me how it’s working? Because I just tried to use your prompt string but it’s seems like it doesn’t work on my machine. =)

Write a comment:

HTML-Tags will be converted to Entities.
Textile-formatting allowed
Standard emoticons like :-) and ;-) are converted to images.
Design and Code Copyright © 2010-2017 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms. tweetbackcheck