3

Magic enter command for (fish) shell

 1 year ago
source link: https://kau.sh/blog/magic-enter-shell-fish/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Magic enter command for (fish) shell

Kaushik Gopal

Two of the most common commands I run when I cd into a directory are:

  1. git status (which i do through git-number)
  2. ls -lh (aliased automatically as ll through fish)
  • If this directory is not a git repo, then I usually am thinking of command 2.
  • If however it is a git repository then
    • I’d like to know if any files have been changed in this directory and
    • which ones specifically (which is command 1).

Wouldn’t it be nice if I could just hit the ↩ (enter key) and have this automatically happen?

If you’re using fish, I got you covered:

# fish/conf.d/magic-enter.fish
function magic-enter-cmd --description "Issue git status or ls on hitting enter in a dir"
    set -l cmd ll
    set -l is_git_repository (git rev-parse --is-inside-work-tree ^/dev/null) # Special variable indicating git.
    # set -l repo_has_changes (git status --porcelain --untracked-files=no)
    set -l repo_has_changes (git status --porcelain)

    if test -n "$is_git_repository"
        if test -n "$repo_has_changes"
            set cmd git status # or git-number which i highly recommend
        end
    end

    echo $cmd
end

function magic-enter
    set -l cmd (commandline)
    if test -z "$cmd"
        commandline -r (magic-enter-cmd)
        commandline -f suppress-autosuggestion
    end
    commandline -f execute
end

bind \r magic-enter

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK