Repository: junegunn/fzf (language: go)

Issue:
v0.50.0 not showing process data correctly
### Checklist

- [X] I have read through the manual page (`man fzf`)
- [X] 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.50.0 (brew)

### OS

- [ ] Linux
- [X] macOS
- [ ] Windows
- [ ] Etc.

### Shell

- [ ] bash
- [X] zsh
- [ ] fish

### Problem / Steps to reproduce

I have my completion trigger set to `,,`. After doing a `brew upgrade`, this is now the output of `kill ,,` for me:

<img width="1290" alt="image" src="https://github.com/junegunn/fzf/assets/18129692/3fc12384-9cd6-4f74-8653-2370f8fe705a">

If I select one (or more), it does not have a process ID, the command will be entered as: `kill 2^[[0m` 



Discussion / hints:
Looks like your ps commmand is producing ANSI color codes when it shouldn't. You see colored output when you do `ps -ef | cat`? And what is the output of `command -v ps`?
> Looks like your ps commmand is producing ANSI color codes when it shouldn't. You see colored output when you do `ps -ef | cat`? And what is the output of `command -v ps`?

No color output, and `/bin/ps`



@blaineventurine sets an alias for `cat`[^1] and has a config file for `bat` with the `--color=always` flag. [^2]

```bash
alias cat='bat'
```

Additionally, there is no `--ansi` flag in the `FZF_DEFAULT_OPTS`[^3].

---

Minimal setup to reproduce the issue:

```sh
export FZF_DEFAULT_OPTS=""
export FZF_COMPLETION_TRIGGER=',,'
alias cat='bat --color=always --number'
source /Users/paria/Developer/fzf/shell/completion.zsh
```

<img src="https://github.com/junegunn/fzf/assets/92653266/f741e687-709a-4272-9426-f3ced540fddf" width="600">

---


It seems to have come from commit 152988c17bd6c35ce55652d123db8b65a32f98e4.

Prepending `command` to the `cat` command in the function below seems to fix the issue. However,
this does not explain why the issue only became apparent after commit 152988c17bd6c35ce55652d123db8b65a32f98e4. Anyone understands this?

https://github.com/junegunn/fzf/blob/f97d2754134607b24849fc4a2062dbfcaafddd6a/shell/completion.zsh#L183-L187

Another `cat` command in that file already has been prefixed with `command`[^4], lets just follow suit ?

https://github.com/junegunn/fzf/blob/f97d2754134607b24849fc4a2062dbfcaafddd6a/shell/completion.zsh#L231-L236


[^1]: [dotfiles/.config/zsh/aliases.zsh · blaineventurine/dotfiles](https://github.com/blaineventurine/dotfiles/blob/96f794cd1c826b481edc50fa09631d945d18223c/.config/zsh/aliases.zsh#L7)
[^2]: [dotfiles/.config/bat/config · blaineventurine/dotfiles](https://github.com/blaineventurine/dotfiles/blob/96f794cd1c826b481edc50fa09631d945d18223c/.config/bat/config#L1)
[^3]: [dotfiles/.config/zsh/.zshenv · blaineventurine/dotfiles](https://github.com/blaineventurine/dotfiles/blob/96f794cd1c826b481edc50fa09631d945d18223c/.config/zsh/.zshenv#L33)
[^4]: [Pull Request #701 · junegunn/fzf](https://github.com/junegunn/fzf/pull/701)

> @blaineventurine sets an alias for `cat`[1](#user-content-fn-1-abf4aaeb09d0e94e16174b8e0265d8c6)

Thank you for doing what I should have done and giving a fully reproducible config.

> Prepending `command` to the `cat` command in the function below seems to fix the issue.

Confirmed.

But I have to mention that aliasing cat to bat with `--color=always` is a bad idea and should be discouraged. It will break existing scripts that expect the normal cat behavior. `alias cat='bat --number'` should be enough. You'll get pretty output when you `cat something`, and it will not break scripts as bat will not print color codes when the standard output is not a TTY device.

```sh
alias cat='bat --number'

# Colors
cat README.md

# No colors
cat README.md | command cat
```
Commit 152988c17bd6c35ce55652d123db8b65a32f98e4 wrapped the `emulate` part in an if statement. With that change, the alias expansion no longer worked as expected.

Moving the interactive check after the `emulate` command seems to have solved the issue and prevents us from prepending every single command with `command`, as was described in #1944.

The movement of the interactive check to the top came with #3449, but it was just a simple one-line check, compared with the commit mentioned prior which wrapped everything in an if statement.


> [!NOTE]
> I am not confident that the explanation is correct; it was derived only through testing. The documentation about `emulate` did not provide clear instructions, and searching through the Zsh community posts did not offer much further assistance, other than the frequently mentioned advice of placing `emulate` at the top of the script.

@LangLangBart Thanks for the investigation. Should we do it like this then?

```diff
diff --git a/shell/completion.zsh b/shell/completion.zsh
index 7067b06..8cd9737 100644
--- a/shell/completion.zsh
+++ b/shell/completion.zsh
@@ -9,9 +9,6 @@
 # - $FZF_COMPLETION_TRIGGER (default: '**')
 # - $FZF_COMPLETION_OPTS    (default: empty)
 
-if [[ -o interactive ]]; then
-
-
 # Both branches of the following `if` do the same thing -- define
 # __fzf_completion_options such that `eval $__fzf_completion_options` sets
 # all options to the same values they currently have. We'll do just that at
@@ -76,6 +73,8 @@ fi
 # `finally` in lesser languages. We use it to *always* restore user options.
 {
 
+if [[ -o interactive ]]; then
+
 # To use custom commands instead of find, override _fzf_compgen_{path,dir}
 #
 #   _fzf_compgen_path() {
@@ -346,10 +345,10 @@ fzf-completion() {
 zle     -N   fzf-completion
 bindkey '^I' fzf-completion
 
+fi
+
 } always {
   # Restore the original options.
   eval $__fzf_completion_options
   'unset' '__fzf_completion_options'
 }
-
-fi
```
> @LangLangBart Thanks for the investigation. Should we do it like this then?

Yes, `key-bindings.zsh` may require similar modifications, and maybe in one file a hint ?

```bash
# The 'emulate' command should not be placed inside the interactive check,
# as doing so fails to disable alias expansion.
if [[ -o interactive ]]; then
```


@LangLangBart Sounds good. Can you open a pull request?

Fix patch:
diff --git a/shell/completion.zsh b/shell/completion.zsh
index 7067b06fc74..3de9fe49c55 100644
--- a/shell/completion.zsh
+++ b/shell/completion.zsh
@@ -9,8 +9,6 @@
 # - $FZF_COMPLETION_TRIGGER (default: '**')
 # - $FZF_COMPLETION_OPTS    (default: empty)
 
-if [[ -o interactive ]]; then
-
 
 # Both branches of the following `if` do the same thing -- define
 # __fzf_completion_options such that `eval $__fzf_completion_options` sets
@@ -75,6 +73,9 @@ fi
 # This brace is the start of try-always block. The `always` part is like
 # `finally` in lesser languages. We use it to *always* restore user options.
 {
+# The 'emulate' command should not be placed inside the interactive if check;
+# placing it there fails to disable alias expansion. See #3731.
+if [[ -o interactive ]]; then
 
 # To use custom commands instead of find, override _fzf_compgen_{path,dir}
 #
@@ -345,11 +346,10 @@ fzf-completion() {
 
 zle     -N   fzf-completion
 bindkey '^I' fzf-completion
+fi
 
 } always {
   # Restore the original options.
   eval $__fzf_completion_options
   'unset' '__fzf_completion_options'
 }
-
-fi
diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh
index a75f4beb2df..07baa203b24 100644
--- a/shell/key-bindings.zsh
+++ b/shell/key-bindings.zsh
@@ -11,8 +11,6 @@
 # - $FZF_ALT_C_COMMAND
 # - $FZF_ALT_C_OPTS
 
-if [[ -o interactive ]]; then
-
 
 # Key bindings
 # ------------
@@ -38,6 +36,7 @@ fi
 'builtin' 'emulate' 'zsh' && 'builtin' 'setopt' 'no_aliases'
 
 {
+if [[ -o interactive ]]; then
 
 # CTRL-T - Paste the selected file path(s) into the command line
 __fsel() {
@@ -114,10 +113,9 @@ 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
 
 } always {
   eval $__fzf_key_bindings_options
   'unset' '__fzf_key_bindings_options'
 }
-
-fi


Test patch: