# 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>), [Command-line interface](<https://devfeed.tech/topics/cli.md>)

Tags: [automatically](<https://devfeed.tech/tags/automatically.md>), [command](<https://devfeed.tech/tags/command.md>), [commands](<https://devfeed.tech/tags/commands.md>), [directory](<https://devfeed.tech/tags/directory.md>), [files](<https://devfeed.tech/tags/files.md>), [fish](<https://devfeed.tech/tags/fish.md>), [git](<https://devfeed.tech/tags/git.md>), [key](<https://devfeed.tech/tags/key.md>), [repo](<https://devfeed.tech/tags/repo.md>), [repository](<https://devfeed.tech/tags/repository.md>), [root](<https://devfeed.tech/tags/root.md>), [status](<https://devfeed.tech/tags/status.md>), [zsh](<https://devfeed.tech/tags/zsh.md>)

## AI overview

The article configures Enter on an empty command line to run `git-number` for a Git repository with changes, or `ll` otherwise. The fish function checks repository status and whether the current directory is the repository root before choosing the command; it then executes the selected command. The zsh version exposes `MAGIC_ENTER_GIT_COMMAND` and `MAGIC_ENTER_OTHER_COMMAND` settings, defaulting to `git-number` and `ll`, and only acts at an empty prompt. The excerpt notes revisions to limit status checks to the repository root and concern about slow porcelain status output, but cuts off before the full zsh logic.

## 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