Search code examples
fish

Fish shell function git_commit not updating command line or producing output


I have defined a git_commit function in my Fish shell configuration file to assist with Git commits. However, when I use the function, it does not produce any errors, but it also does not result in the expected behavior. Here's the function definition:

function git_commit
    # Extract the commit message from the git_commit command
    set -l commit_msg (string replace 'git_commit (.*)' '$1' (commandline))

    # Clear the current command line in fish shell
    commandline (commandline --cut-at-cursor)

    # Set the new command line with the fixed part and the commit message
    echo -n "git commit -m $commit_msg" | commandline -r

    # Bind the Enter key to move the cursor to the desired position
    bind \r "commandline -C (math (string length 'git commit -m \"\"') - 1)"
end

I'm invoking the function by running git_commit "fix typos". The expected behavior is that it should execute the git commit -m command with the provided commit message, and position the cursor at the end of the command. However, nothing happens when I run the command.

I'm using Fish shell version 3.3.1 on kubuntu.

i have created this function by read here:

tests

test 1

i have added echo "commit_msg= $commit_msg" inside the function and test this with no result as you could see here:

git_commit 'test'
commit_msg= 

test 2

i have added echo "commit_msg= $commit_msg"

git_commit "test"

now the cursor | has moves a bit back. cursor | move to: git_commit "tes|t" and commit_msg= is now not visible anymore.


I would appreciate any help or suggestions to fix this problem. Thank you!


Solution

  • Since it seems you want a second chance to rewrite the commit message, a function:

    function gitcommit
      command git commit --edit -m (string join " " $argv)
    end
    

    Using string join means you don't even need to quote the initial message:

    gitcommit this is my commit message
    

    It's recently been suggested to me that adding the -v option is helpful: it includes the git diff so you can be reminded about exactly what you're committing.


    another possibility is to use an abbreviation

    abbr --set-cursor --add -- gitcommit 'git commit -m "%'
    

    With that, you type gitcommit and when you hit space the abbreviation is replaced and you're now typing the commit message.

    • the set-cursor and % are intended to backspace the trailing space after the opening double quote.