Skip to content

commandline -r broken in Fish 3.4.0? #8807

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

Closed
christianheinig opened this issue Mar 17, 2022 · 12 comments
Closed

commandline -r broken in Fish 3.4.0? #8807

christianheinig opened this issue Mar 17, 2022 · 12 comments

Comments

@christianheinig
Copy link

I use Mac OS Montery 12.3 and iTerm2 and just upgraded Fish shell to 3.4.0.

One of my functions stopped working, because commandline -r "xyz" does not replace the command line anymore. -a does not seem to work either.

I've seen that the commandline command has been updated, but it seems that the old behavior should still work.

@faho
Copy link
Member

faho commented Mar 17, 2022

You're going to have to add some more context here.

Specifically, what triggers the commandline call? Is this in a binding?

For the record:

bind \eb 'commandline -r xyz'

and pressing alt+b works for me.

@christianheinig
Copy link
Author

Hi Fabian, thanks for your reply.

For me, commandline -r "xyz" works neither directly in a iTerm2 shell (iTerm2 version is 3.4.15, by the way), nor as a binding in fish_user_key_bindings.fish, nor in my function.

My function in ~/.config/fish/functions/commit.fish which prepares a git commit message that includes the JIRA issue key:

function commit

  # Get git branch name, extract JIRA issue key and store in variable _ticket, omitted here
  set _ticket ...

  commandline -r "git commit -m \" ($_ticket)\""
  # Move cursor before issue key (when removing this, it still doesn't work)
  set _ticket_length (string length $_ticket)
  commandline -C (expr (commandline -C) - $_ticket_length - 4)
end

@faho
Copy link
Member

faho commented Mar 17, 2022

For me, commandline -r "xyz" works neither directly in a iTerm2 shell

If you run commandline -r xyz interactively, that will run, replace the commandline, then the new prompt will be started and will clean the commandline. This is as expected.

The same thing goes for the function when you run that interactively. I.e. if you enter commit into the commandline and press the return key, that will change the commandline, then the new prompt will happen and the commandline will be cleared again.

Having that change the commandline and then have it magically appear later isn't something we can really support. If it did work in earlier versions that's by accident.

Now, what should be working is to do it as a binding:

nor as a binding in fish_user_key_bindings.fish

Note: fish_user_key_bindings hasn't been needed since fish 3.0. It will still be executed if it is there, but it's not necessary.

But just to confirm: If you run

bind \cg 'commandline -r xyz'

in an interactive session, and then you press ctrl+g in that interactive session, does it replace the commandline with "xyz"? (I switched it from alt+b because I remembered some mac terminals like sending bad things for alt bindings if some setting is checked)

@christianheinig
Copy link
Author

Your latest binding to Ctrl+G works!
And it still works when calling my function in the binding. I will have to get used to using a shortcut instead of typing "commit", but as long as I don't have to type the issue key by hand I'm happy.

Thank you! :-)

@EvanTheB
Copy link

EvanTheB commented Mar 28, 2022

EDIT: apologies, I had not tested it as thouroughly as I thought. this is not working.

I restored the original behaviour with a small function:

function ecommandline
    set -g ECOMMANDLINE $argv
end

(append to end of fish_prompt):

    commandline $ECOMMANDLINE
    set -e ECOMMANDLINE

@phillco
Copy link

phillco commented Apr 17, 2022

Interesting! I never knew that using commandline in an interactive session wasn't meant to be supported -- I had a couple of functions that used it, and always thought it was rather cool feature of fish (one example from Stack Overflow: substitute).

Like substitute, it was cool to be able to take a recent command, modify it, and then put the modified command onto the prompt (and even execute it), while having the modified version behave just like a normal command (appear in the command buffer, show up in up-history, etc.)

I guess the closest thing would be to eval the modified command, then append it to the history file / reload the config, but it won't be quite the same.

@viveksjain
Copy link

Thanks for the suggestion @phillco. In case anyone else would benefit from an example, my use case was to take the current commandline, execute it and pipe through fzf, then set the commandline with the output. Here's what I did:

function fzf_prompt
  set -l cmd (commandline)
  echo
  eval $cmd | command fzf | while read -l r; set result $result (string escape $r); end
  if [ -n "$result" ]
    echo "- cmd:" $cmd >> ~/.local/share/fish/fish_history
    echo "  when:" (date "+%s") >> ~/.local/share/fish/fish_history
    history merge
    commandline -- (string join " " $result)
  end
  commandline -f repaint
end

Then you can setup a keybinding in fish_user_key_bindings.fish with bind \ef fzf_prompt.

@ridiculousfish
Copy link
Member

This keeps coming up and it looks easy enough to fix, so I will just fix it. commandline foo now does what it did in 3.4.x.

@ridiculousfish ridiculousfish reopened this Jun 4, 2022
ridiculousfish added a commit that referenced this issue Jun 4, 2022
@phillco
Copy link

phillco commented Jun 5, 2022

Oh, man, awesome! Thanks! This is one of those features that makes fish special for me.

@adrian-gierakowski
Copy link

@ridiculousfish I guess this deserves a release?

@faho
Copy link
Member

faho commented Jun 6, 2022

I guess this deserves a release?

@adrian-gierakowski A release will be out in time. We tend to do one every few months, not for specific features.

@phillco
Copy link

phillco commented Jun 16, 2022

Just to close the loop for anyone subscribed, @ridiculousfish's fix shipped in 3.5.0: https://fishshell.com/docs/current/relnotes.html#fish-3-5-0-released-june-16-2022 :)

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

7 participants