Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to disable terminal beep when exiting search #3864

Closed
4 of 10 tasks
diminutivesloop opened this issue Jun 14, 2024 · 5 comments
Closed
4 of 10 tasks

Option to disable terminal beep when exiting search #3864

diminutivesloop opened this issue Jun 14, 2024 · 5 comments

Comments

@diminutivesloop
Copy link

Checklist

  • I have read through the manual page (man fzf)
  • I have searched through the existing issues
  • For bug reports, I have checked if the bug is reproducible in the latest version of fzf

Output of fzf --version

0.52.1 (brew)

OS

  • Linux
  • macOS
  • Windows
  • Etc.

Shell

  • bash
  • zsh
  • fish

Problem / Steps to reproduce

When using a terminal that has the audio beep enabled fzf emits a beep when exiting a search via escape or ctrl+c. I find this feedback unnecessary and distracting so I would appreciate an option to disable it.

@LangLangBart
Copy link
Contributor

to reproduce:

setopt beep
source <(fzf --zsh)
# press ctrl-r
# press escape
# hear the beep
man zshoptions | less --pattern 'Beep on error in ZLE'
#     Zle
#        BEEP (+B) <D>
#             Beep on error in ZLE.

Since this falls within the user's realm and how they configure their shell startup files, no action should be taken by fzf.
Adding unsetopt beep to your .zshrc should be sufficient.


Does the beep sound also appear when running fzf without utilizing any widgets?

look foo | fzf 
# press escape

@diminutivesloop
Copy link
Author

@LangLangBart So using setopt beep fixes the issue for me. I did try running look foo | fzf before setting that option and it didn't fire off a beep there either. Interestingly I still get the beep on command completion notifications via the bgnotify plugin. That's the behavior I wanted so it's not an issue but I would have expected setopt beep to disable beeps globally.

@LangLangBart
Copy link
Contributor

So using setopt beep fixes the issue for me.

I am confused; the beep option should enable beeps like this when encountering an error in a zle widget, such as in fzf-history-widget, which returns the non-zero exit status of fzf upon pressing ⎋ Escape or ⌃ Control + C.

fzf/shell/key-bindings.zsh

Lines 107 to 138 in e2401ac

# CTRL-R - Paste the selected command from history into the command line
fzf-history-widget() {
local selected num
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob 2> /dev/null
# Ensure the associative history array, which maps event numbers to the full
# history lines, is loaded, and that Perl is installed for multi-line output.
if zmodload -F zsh/parameter p:history 2>/dev/null && (( ${#commands[perl]} )); then
selected="$(printf '%1$s\t%2$s\000' "${(vk)history[@]}" |
perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++) { s/\n/\n\t/gm; print; }' |
FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m --read0") \
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
else
selected="$(fc -rl 1 | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' |
FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m") \
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
fi
local ret=$?
if [ -n "$selected" ]; then
if num=$(awk '{print $1; exit}' <<< "$selected" | grep -o '^[1-9][0-9]*'); then
zle vi-fetch-history -n $num
else # selected is a custom query, not from history
LBUFFER="$selected"
fi
fi
zle reset-prompt
return $ret
}
zle -N fzf-history-widget
bindkey -M emacs '^R' fzf-history-widget
bindkey -M vicmd '^R' fzf-history-widget
bindkey -M viins '^R' fzf-history-widget
fi

I did try running look foo | fzf before setting that option and it didn't fire off a beep there either. Interestingly I still get the beep on command completion notifications via the bgnotify plugin. That's the behavior I wanted so it's not an issue but I would have expected setopt beep to disable beeps globally.

Great, so there is no issue with fzf, just figuring out personal shell setup configuration.

@diminutivesloop
Copy link
Author

Sorry that was a mistake on my part. I meant to say unsetopt beep.

@LangLangBart
Copy link
Contributor

Interestingly I still get the beep on command completion notifications via the bgnotify plugin. That's the behavior I wanted so it's not an issue but I would have expected setopt beep to disable beeps globally.

I assume you mean the one from ohmyzsh1, in its basic form it does something like in the example below, but it doesn't use zle widgets, so unsetopt beep won't apply there.

Example: The two functions will run after a command has been read (preexec) and before each prompt (precmd). If the command took more than 10 seconds to complete, an audio signal is played.

# ensure 'EPOCHSECONDS' is loaded
zmodload -F zsh/datetime p:EPOCHSECONDS || return

get_time() {
  zbell_timestamp=$EPOCHSECONDS
}

ring_bell() {
  ((EPOCHSECONDS - zbell_timestamp >= 10)) && printf '\a'
}

# register the functions as hooks
preexec_functions+=(get_time)
precmd_functions+=(ring_bell)

The sound from printf '\a' is acceptable, but Apple provides some pleasant default sounds. You can use the command below to explore them with fzf and select a more preferred sound.

find /System/Library/Sounds/ /System/Library/PrivateFrameworks/ToneLibrary.framework \
  -type f \
  \( -name '*.aiff' \
  -o -name '*.au' \
  -o -name '*.caf' \
  -o -name '*.m4r' \
  -o -name '*.mp2' \
  -o -name '*.mp3' \
  -o -name '*.wav' \) |
  fzf \
    --preview 'afplay {}' \
    --preview-window '0'

I like this one; you might need to add &| to play the sound as a disowned background job, preventing any delay until the user can enter the next command.

afplay /System/Library/PrivateFrameworks/ToneLibrary.framework/Versions/A/Resources/AlertTones/Modern/Complete.m4r

# disowned background job
afplay /System/Library/PrivateFrameworks/ToneLibrary.framework/Versions/A/Resources/AlertTones/Modern/Complete.m4r &|

The maintainer would likely appreciate it if you close the issue if there are no issues left.

Footnotes

  1. bgnotify · ohmyzsh/ohmyzsh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants