4

I use ZSH via Oh-my-zsh, and one neat feature I see is that the shell expands my commands when they are in backticks.

For example, `pwd` + tab, replaces `pwd` with my current path:

https://infinit.io/_/DjmxTCK.gif

I am wondering if there's a relatively easy way to do the same for some commands without having to use the backticks. I'd like to apply that to some aliases (or functions).

To be clear, I am not talking about expanding arguments to a command, but the command itself.


This is a truly amazing feature, but is not exactly what I was looking for. Unless I did something wrong, this expands my alias to what the alias was defined to, but not to what the result is. For example here:

enter image description here

I would like to see the result of that command instead of the full command.

  • So, if I understand you correctly, you want to just type any command, press some key and replace the typed command with its output? – Adaephon Oct 28 '15 at 21:33
2

You can use a custom zle widget for this and call it by key binding, for example Alt+Tab (identical with Esc,Tab)

expand-to-output () {
    BUFFER='$('$BUFFER')'
    CURSOR=0
    zle redisplay
    zle expand-word
}
zle -N expand-to-output
bindkey "^[^I" expand-to-output

Now, if you type any command followed by Alt+Tab, the whole command will be replaced by its output, if the command could be run successfully. If the command cannot be successfully run, the typed command line will just be surrounded by $(...) but not expanded. If there were error messages, they should be printed above the edit buffer. This may not work reliably with multi-line prompts.

Explanation:

  • expand-to-output () {...} defines the (new) expand-to-output function.
  • BUFFER contains the current command line
  • BUFFER='$('$BUFFER')' places $( before and ) after the command line. $(...) is equivalent to `...` but it can be nested.
  • CURSOR is the position of the cursor on the command line
  • CURSOR=0 places the cursor on the beginning of the (edited) command line, that is on $. Without moving the cursor there, it could happen that zsh tries to expand a word inside of $(...) instead of the whole process substitution.
  • zle redisplay redisplays the edit buffer. This prevents display errors should the expanded command print something to stderr. (Note: there can be some issues with multi-line prompts)
  • zle expand-word calls the expand-word widget, which is usually often called as part of Tab-completion.
  • zle -N expand-to-output registers the function as widget of the same name.
  • bindkey "^[^I" expand-to-output binds the expand-to-output widget to Alt+Tab.
  • This is a truly amazing feature, but is not exactly what I was looking for. Unless I did something wrong, this expands my alias to what the alias was defined to, but not to what the result is. For example here: infinit.io/_/yHyTVZM.gif I would like to see the result of that command instead of the full command – midu Oct 22 '15 at 13:47

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.