1

Because writing commands full names is tiring. Is there any way/implementation for git to guess the commands like vim?

For example in vim:

:w
:wri
" for
:write

What I would like to do with git:

git br
git bra
# for
git branch

While I can do that with aliases, I would prefer something more automatic (for all commands) like vim's functionality.

  • 4
    It's important to note that `git` can't do this because the `git` command doesn't run until you press ``. The feature you're asking about would have to be implemented in your shell. I think you're looking for https://stackoverflow.com/questions/12399002/how-to-configure-git-bash-command-line-completion – larsks Sep 25 '21 at 14:31
  • 2
    In addition to shell auto-completion, you will probably be interested in [git aliases](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases). For instance you can configure git so that `git b` is an alias of `git branch`. [Well apologies, I admit I did not read your last sentence the first time.] – Maëlan Sep 25 '21 at 14:32
  • 3
    `git config help.autocorrect 10` tells git to guess what you meant, and execute it if you don't object within 10 tenths of a second. [Auto-Correct Git Commands](https://andy-carter.com/blog/auto-correct-git-commands) – Raymond Chen Sep 25 '21 at 15:08
  • @larsks That's not true; the vim examples are all processed when pressing return as well, it just treats any unambiguous prefix as an alias for the full command. This isn't a question about tab completion (although that would be a workaround) but about parameter / command parsing. – IMSoP Sep 25 '21 at 21:05
  • @RaymondChen That sounds like it would be a great answer, rather than a comment. I'd upvote it. – IMSoP Sep 25 '21 at 21:06
  • @imsop It is true. When you enter commands in `vim`, *you're already running vim*. When you enter `git` commands at the shell prompt *git is not running*. You're running bash (or zsh or whatever). – larsks Sep 25 '21 at 21:09
  • Note that if you write a script named `git-b` and place it in your `$PATH` so that Git can run it, `git b` will *run your script*. So `git b` *is* a command, you just have to write it first. That's *why* Git doesn't normally do this kind of completion. However, with bash completion scripts, you can set things up so that you can type `git bra` and have bash do autocompletion for you: the autocomplete script has a list of Git commands it knows about, and assumes those are the commands. Which means when you write your own `git bling`, you have to add that to the autocomplete script. – torek Sep 25 '21 at 21:45
  • @larsks The question doesn't ask to abbreviate the word "git". If you type "git br", then by the time the "br" needs to be interpreted, *git is already running*. The shell doesn't know what the word "branch" means, it just runs "git" with an array or parameters. It would be perfectly plausible for git to see that "br" and decide that it was an abbreviation for "branch", just as it sees "branch" and decides that it should run a particular built-in function / sub-command. – IMSoP Sep 25 '21 at 22:29
  • No, `git` is not running at that point. I think you may misunderstand how the shell works. No command is executed until you press "return". – larsks Sep 25 '21 at 23:23
  • 1
    @larsks Why do you think anything needs to happen before you press return? Write "git flooble flarble", *and press return*; the shell finds the git executable in your configured path, and invokes it with an array containing the strings "flooble" and "flarble"; it is then *entirely up to the invoked program* what to do with those words. In git's case, that means looking up "flooble" in its list of configured aliases, built-in commands, or external shell scripts. If the authors of git wanted "flooble" to do something, they could; and if they wanted "b" to be equivalent to "branch", they could. – IMSoP Sep 26 '21 at 09:04
  • It is obvious to me that this feature does not exist. The most close thing to what I want is the `git config help.autocorrect 10` proposed by @Raymond Chen. I am aware of completion, but I would like to avoid pressing . – invinciblecache Sep 27 '21 at 10:04

0 Answers0