Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Small zsh functions to redo commands



Hi,

inspired by the nice zsh editor zed I wrote two small functions that
allow to reissue some previous commands without retyping them.

The more useful is cpd, where you can select by arrow keys from
directories contained in the directory stack.
It works best if you enable the zsh options pushdsilent autopushd
pushdignoredups and histignorespace.

The second one, pcmd is of limited use only. It could be used if you
keep several .history files (e.g.) for different machines in the same
home directory or if you just want to select commands from the history
file by arrow keys. For a single .history file the editor command
history-search-backward (which is bound to the PrevScrn key at our
site) is more powerful. As pcmd is calling egrep -h it will not run on
HPUX as there the -h flag is missing. 

I bound these two functions to the Find and Select keys. To me this is
more or less a gadget but maybe somebody is missing such a functionality.

-- 
Wolfgang Friebel
    Deutsches Elektronen-Synchrotron DESY |  Phone:  +49 33762 77372  |
    Platanenallee 6                       |  Fax:    +49 33762 77216  |
    D-15738 Zeuthen  Germany              |  E-Mail: friebel@xxxxxx   |

#
# cpd Change to a Previous Directory (contained in the directory stack)
#
# Author W.Friebel friebel@xxxxxx 13/04/95
#
# Usage: cpd
#        displays the directories in the buffer stack
#	 chose the directory you want to cd to with the cursor keys
#	 optionally edit the directory string
#	 select the directory by pressing Enter (or ^X^W)
#	 or leave the menu with ^C
#
# it is recommended to bind cpd to a function key
# for VT100 style terminals the following binding could be used (Find key)
# bindkey -s "^[[1~" " cpd^M"
#
# The dirs or pushd function can be used in addition to preload frequently
# used directories into the directory stack

TRAPINT () {
  bindkey "^M" accept-line
  setopt +B
  trap - INT
  builtin let "$1 > 0" && return $[128+$1]
}

setopt -B
bindkey -s "^M" "^X^W"
bindkey "^[k" kill-region
# Select_Key definition: 
# to column 2; to begin of line; insert  cd ;
# to begin of line; mark position; to begin of buffer; delete up to mark
# to end of line; mark position; to end of buffer; to end of line
# delete up to mark; put cd command into input buffer and execute
bindkey -s "" "^[2^[|^A cd ^A^@^[<^[k^E^@^[>^E^[k^[a^X^K"

cpd_var=`builtin print "### Select (and Edit) directory, Enter to accept it, ^C to quit ###"
builtin print -l \`dirs \``

vared cpd_var
bindkey "^M" accept-line
setopt +B
eval $cpd_var






#!/bin/zsh -x
# pcmd Execute a Previous Command (contained in the .history* files)
#
# Author W.Friebel friebel@xxxxxx 13/04/95
#
# Usage: pcmd [string]
#        displays the previous commands containing string
#	 chose the command you want to execute with the cursor keys
#	 optionally edit the command
#	 select the command by pressing Enter (or ^X^W)
#	 or leave the menu with ^C
#
#	 If no string was given, it is assumed that pcmd is invoked by a
#	 function key. The argument is then taken from the command line
#	 and pcmd has to be bound by   bindkey -s "..." "^[q pcmd^M"
#
# for VT100 style terminals the following binding could be used (Select key)
# bindkey -s "^[[4~" "^[q pcmd^M"
#

TRAPINT () {
  bindkey "^M" accept-line
  setopt +B
  trap - INT
  builtin let "$1 > 0" && return $[128+$1]
}

if [[ $1 = "" ]]; then
  getln 1
fi

setopt -B
bindkey -s "^M" "^X^W"
bindkey "^[k" kill-region
# Select_Key definition: 
# to column 2; to begin of line; insert " ";
# to begin of line; mark position; to begin of buffer; delete up to mark
# to end of line; mark position; to end of buffer; to end of line
# delete up to mark; put cd command into input buffer and execute
bindkey -s "" "^[2^[|^A ^A^@^[<^[k^E^@^[>^E^[k^[a^X^K"

pcmd_var=`builtin print "### Select (and Edit) command, Enter to accept it, ^C to quit ###"
egrep -h "^$1" ~/.*history*(Ur)`
rc=$?

if [[ $rc = 0 ]]; then
  vared pcmd_var
  bindkey "^M" accept-line
  setopt +B
  eval $pcmd_var
else
  bindkey "^M" accept-line
  setopt +B
  builtin print "No matches for \"$1\" in .history files"
  return $[128+$rc]
fi



Messages sorted by: Reverse Date, Date, Thread, Author