2

I'm new to zsh and am trying to bind a key sequence to a function with the following in my .zshrc:

say_hello(){ echo "hello" } zle -N say_hello bindkey '^Y' say_hello 

Pressing Ctrl-Y will call the function and I'll see "hello" printed to the terminal but after I need to press Enter again before I'm given another zsh prompt. Calling the function by just typing in say_hello at the zsh prompt and pressing Enter does what I want - I see hello printed and then I'm given another zsh prompt. How can I get this behavior when binding the function to a key sequence?

Above is a simple example, really the function I'm trying to write is below:

my_cd() { if [[ "$#" -ne 0 ]]; then cd $(autojump $@) return fi dir_to_cd_to=$(fasd_cd -dl | fzf --height 40% --reverse --inline-info) # above isn't so important - dir_to_cd_to could be obtained in any way cd "$dir_to_cd_to" } zle -N my_cd bindkey -v '^Y' 'my_cd' 
5
  • Is your intention to run a command on ^Y or just to insert text in your commandline? Commented Jun 5, 2020 at 2:18
  • @MartinTournoij no the function I really want to run involves changing the current directory, I'm just using the echo command in the example here to illustrate the behavior I don't understand. Commented Jun 5, 2020 at 14:15
  • Strictly speaking, zsh is already waiting for your input; you don't need to hit Enter (causing the prompt to be redisplayed) before typing your next command. The output simply wrote "hello" and a newline to the terminal in the same place where your input would have been echoed by the terminal. Commented Jun 5, 2020 at 14:59
  • 1
    You might be interested in print -z instead, which will write directly to the input buffer as if you had typed text instead. That might be an alternative to having zle call a function "out of band", so to speak. Commented Jun 5, 2020 at 15:07
  • @chepner thanks. So I added an example of what I really want - a function that changes the working directory, that is run with a keybinding. Can you recommend how to make this work so that I will be given my zsh prompt after the function finishes? Commented Jun 6, 2020 at 1:17

1 Answer 1

2

To display messages in a zle widget, you're supposed to use zle -M rather than echo. echo will output your message at whatever the current cursor position is which isn't especially helpful. If you really want to use echo, calling zle reset-prompt afterwards will redraw a fresh prompt. If you don't want a potential mess in your terminal, consider starting with \r to move the cursor to the beginning of the line and ending with $termcap[ce] to clear to the end of the line.

Sign up to request clarification or add additional context in comments.

2 Comments

good to know, but I'm just using echo here to show the behavior I don't understand - what I really want to do in the function is more than just printing something
woops missed the zle reset-prompt part of this answer - that was the key part that did what I was looking for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.