10

I want to use preexec() to modify certain commands before they are run but I need to be able to evaluate the current entered command. Is there a variable that contains the entire command before it is executed? I know !! is the last command but I need the current line before it's saved to history.

An example of what I want to do would probably help

ls -l /root please

And then I want preexec to see I wrote "please" at the end and replace it with

sudo ls -l /root

I think something like

preexec() {
    if [[ $CURRENT_LINE =~ please$ ]]; then
        $CURRENT_LINE="sudo ${CURRENT_LINE% please}"
    fi

Would work but I can't find a variable in zsh that gives me the correct $CURRENT_LINE

For bonus points I also want to be able to enter please on a line by itself and have it run sudo !! but I could probably do that with some form of alias.

I think it might be better to make a please function that I can pipe a command to but I don't think that'll work as well because the command will run and fail (before piping) before it is run again with sudo.

0

1 Answer 1

16

As far as I know that the preexec is not for the right place to modify the command to be executed though. We can not change the commands to be executed from inside of the preexec function…

Although the actual command to be executed are passed as $1, $2 and $3.

preexec

Executed just after a command has been read and is about to be executed. If the history mechanism is active (and the line was not discarded from the history buffer), the string that the user typed is passed as the first argument, otherwise it is an empty string. The actual command that will be executed (including expanded aliases) is passed in two different forms: the second argument is a single-line, size-limited version of the command (with things like function bodies elided); the third argument contains the full text that is being executed.

-- zshmisc(1) 9.3.1 Hook Functions

For example:

alias ls='ls -sF --color=auto'
preexec () { 
  print ">>>preexec<<<"
  print -l ${(qqq)@}
}

If I have above in ~/.zshrc then I will get follows:

% echo test preexec<Esc-Return>
ls<Return>
;# outputs below
>>>preexec<<<
"echo test preexec
ls"
"echo test preexec; ls -sF --color=auto"
"echo test preexec
ls -sF --color=auto"
test preexec
total 1692
...

You could add your own zle widget functions to the zsh line editor for manipulating the line editor buffer. (zshzle(1))

You could add the zle widget function to change the behavior for hitting Enter.

my-accept-line () {
  if [[ "$BUFFER" == *" please" ]]; then
    BUFFER="sudo ${BUFFER% please}"
  fi
  zle .accept-line
}
zle -N accept-line my-accept-line

The above snippets changes the functionality for accept-line from the built-in behavior to my-accept-line defined here.


Adding the abbreviations also could help which is described below:

Cloning vim's abbreviation feature

-- “examples:zleiab [ZshWiki]” - http://zshwiki.org/home/examples/zleiab

6
  • I like the idea of changing the accept-line widget but I ran into a problem adding the function definition to my zshrc. When I add it it says condition expected: "$BUFFER" I searched around but didn't see anything. any ideas? Jan 23, 2015 at 5:55
  • I suspect that the =~ operator caused the error, so I've updated to avoid using it. I hope that this should fix the error. Well, I would like to know zsh's version with echo $ZSH_VERSION.
    – hchbaw
    Jan 23, 2015 at 7:04
  • 1
    I like your idea to modify the accept-line widget. Just one nitpick: The pattern you are looking for should match the text you are removing. So you probably should use *" please" instead of just *please, because the latter one would also match if the last word just ends on bitch (as unlikely as it may be, at least while using english words…).
    – Adaephon
    Jan 23, 2015 at 7:34
  • @Adaephon, good catch! I've updated as you suggested. Thank you very much for your comment.
    – hchbaw
    Jan 23, 2015 at 7:50
  • Thanks guys, that worked exactly as I expected. In case it still helps, my zsh version is 5.0.5 on my laptop and 4.9.11 on my desktop. I haven't tried yet on my desktop but can next week Jan 23, 2015 at 16:49

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.