# Magic enter command for (fish/zsh) shell

DevFeed: [Magic enter command for (fish/zsh) shell](<https://devfeed.tech/articles/magic-enter-command-for-fish-zsh-shell-25285.md>)

Original publisher: [Read original article](<https://kau.sh/blog/magic-enter-shell/>)

Author: Kaushik Gopal

Published: 2022-07-23T19:50:31Z

Content type: tutorial

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [friendly interactive shell](<https://devfeed.tech/topics/fish.md>), [Zsh](<https://devfeed.tech/topics/zsh.md>), [Shell](<https://devfeed.tech/topics/shell.md>), [Git](<https://devfeed.tech/topics/git.md>), [Command-line interface](<https://devfeed.tech/topics/cli.md>)

Tags: [command-line](<https://devfeed.tech/tags/command-line.md>), [commands](<https://devfeed.tech/tags/commands.md>), [git](<https://devfeed.tech/tags/git.md>), [issue](<https://devfeed.tech/tags/issue.md>), [submodules](<https://devfeed.tech/tags/submodules.md>)

## AI overview

A tutorial for fish and zsh users that binds the Enter key to run git-number in Git repositories with changes, or ls -lh elsewhere when the command line is empty.

## Source excerpt

Two of the most common commands I run when I cd into a directory are: git status (which i do through git-number) 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? Your browser doesn't support HTML5 video. Download the video instead. Solution for fish users ## If you're using fish, I got you covered: # filename: 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 (fish -c "git rev-parse --is-inside-work-tree >&2" 2>| grep true) # Special variable indicating git. set -l in_root_folder (fish -c "git rev-parse --show-toplevel >&2" 2>| grep (pwd)) set -l repo_has_changes (git status -s --ignore-submodules=dirty) if test -n "$is_git_repository" if test -n "$in_root_folder" if test -n "$repo_has_changes" set cmd git-number end 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 Solution for zsh users ## If you're using zsh, I adapted the native solution like so: # filename: magic-enter.plugin.zsh # Default commands : ${MAGIC_ENTER_GIT_COMMAND:="git-number"} # run when in a git repository : ${MAGIC_ENTER_OTHER_COMMAND:="ll"} # run anywhere else magic-enter() { # Only run MAGIC_ENTER commands when in PS1 and command line is empty # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#User_002dDefined-Widgets if [[ -n "$BUFFER" || "$CONTEXT" != start ]]; then return fi is_git_repository=$(git rev-parse --is-inside-work-tree) in_repo_root_folder=$(git rev-parse --show-toplevel) repo_has_chan