Mac os zsh git autocomplete

How to Enable Git Tab Autocomplete on Your Mac

The git version control system is a popular choice among developers when working with source code. Git is automatically installed on every Mac by default, but you might want to enable the git tab autocomplete feature to help you automatically complete commands and branch names.

This feature is a must-have if you work with long branch names. For example, if you typed git checkout ma and then pressed the Tab key, git tab autocomplete would automatically fill in the rest of the branch name, like this: git checkout main .

Follow the instructions in this tutorial to enable git tab autocomplete on your Mac.

Enable Git Tab Autocomplete for Zsh

New Macs use the Zsh shell by default. If you’re using Zsh, add the following line to the

/.zshrc file and restart your Terminal application:

Alternatively, you can run the following two commands in your Terminal application to add the necessary line to the .zshrc file and restart your shell.

Git tab autocomplete is now enabled on your Mac.

Enable Git Tab Autocomplete for Bash

If your Mac is set to use the Bash shell, follow these instructions to enable git tab autocomplete on your Mac:

Download the necessary script to your Mac by using the following curl command:

Add the following line to the

Make the Bash script executable by running the following command:

Restart your Terminal application or run the following command:

Git tab autocomplete is now enabled on your Mac.

Subscribe to our email newsletter

Sign up and get Macinstruct’s tutorials delivered to your inbox. No spam, promise!

Источник

Mac os zsh git autocomplete

zsh-autocomplete adds real-time type-ahead autocompletion to Zsh. Find as you type, then press Tab to insert the top completion, Shift Tab to insert the bottom one, or ↓ / PgDn to select another completion.

Besides live autocompletion, zsh-autocomplete comes with many other useful completion features.

Optimized completion config

Zsh’s completion system is powerful, but hard to configure. So, zsh-autocomplete does it for you, while providing a manageable list of configuration settings for changing the defaults.

Live history search

Press Ctrl R or Ctrl S to do live, multi-line history search.

Press ↑ (or Alt ↑ or PgUp ) to open a menu with the last 16 history items. If the command line is not empty, then the contents of the command line are used to perform a fuzzy history search.

Press Ctrl Space in the completion menu or the history menu to insert more than one item.

Recent dirs completion

Works out of the box with zero configuration, but also supports zsh-z , zoxide , z.lua , rupa/z.sh , autojump and fasd .

On the command line:

Key(s) Action Widget
Tab Accept top completion complete-word
Shift Tab Accept bottom completion complete-word
Ctrl Space Show additional completions list-expand
Cursor down (if able) or completion menu down-line-or-select
PgDn / Alt ↓ Completion menu (always) menu-select
Cursor up (if able) or history menu up-line-or-search
PgUp / Alt ↑ History menu (always) history-search
Ctrl R Live history search, newest to oldest history-incremental-search-backward
Ctrl S Live history search, oldest to newest history-incremental-search-forward

In the completion menu:

Key(s) Action
↑ / ↓ / ← / → Change selection
Alt ↑ Backward one group
Alt ↓ Forward one group
PgUp / PgDn Page up/down
Ctrl R Full text search or previous search match
Ctrl S Full text search or next search match
Ctrl Space Multi-select
Tab Accept selection
Shift Tab Accept bottom completion
Enter Accept command line
most other keys Accept selection, then perform usual action

In the history menu:

Key(s) Action
↑ / ↓ Change selection
Ctrl Space Multi-select
Tab Accept selection
Enter Accept command line
most other keys Accept selection, then perform usual action
  • Tested to work with Zsh 5.7 or newer.
  • Should theoretically work with Zsh 5.4 or newer, but I’m unable to test that.

If you use Znap, simply add the following to your .zshrc file:

Then restart your shell.

For configuration options, see the included .zshrc file.

To uninstall, remove znap source marlonrichert/zsh-autocomplete from your .zshrc file, then run

Then restart your shell.

To uninstall, simply undo the installation steps above in reverse order:

  1. Restore the lines you deleted in step 3.
  2. Delete the line you added in step 2.
  3. Delete the repo you created in step 1. Finally, restart your shell.

Other Frameworks/Plugin Managers

To install with another Zsh framework or plugin manager, please refer to your framework’s/plugin manager’s documentation for instructions.

This project is licensed under the MIT License. See the LICENSE file for details.

About

🤖 Real-time type-ahead completion for Zsh. Asynchronous find-as-you-type autocompletion.

Источник

Git completion not working in zsh on OS X Yosemite with Homebrew

I cannot get git completion to work on my freshly installed OS X Yosemite in the zsh shell. I’ve installed both git and zsh using homebrew:

When installing git via homebrew, it should setup tab-completion for you, but when I git , it just tries to tab-complete the directories, and not the git-commands. I.e. it’s not giving any errors — it’s just not working — like it’s not set up.

This is what I know so far

Apparently zsh will look for ways to autocomplete by looking for _* files in one of the directories in $fpath . This is mine:

Looking at the first path we see:

So it seems to be setup to work out of the box — but it doesn’t.

This is what I’ve also tried

git-completion.bash

  1. Downloading git-completion.bash
  2. Running it using source git-completion.bash

This one kind of works (i.e. it enables auto-completion), but it prints a warning:

git-completion.zsh

So because of the above warning, I obviously also tried to download git-completion.zsh and followed the guide in the top of the file, but it basically tells you to source the .bash file first, which is of cause still giving an error.

Search StackOverflow and Google

I’ve tried just about everything I could find by searching here and on Google, but nothing seems to work for me.

4 Answers 4

I just stumbled upon the answer!

In my case I was missing a few important pieces in my .zshrc file. But first a little background:

What I’m trying to do is setup the «zsh Completion System». It comes with a lot of commands all named something with comp* . I tried to run these a few times but in many cases zsh would just tell me it didn’t know them. So aparently you have to autoload them, among other things.

This is what I did:

I added the following lines to my .zshrc file:

Then I opened a new terminal and ran:

Then I opened a new terminal and now git worked as expected 🙂

If you are setting up a custom $fpath in your .zshrc file I would recommend adding these lines after you’ve modified the $fpath (though I don’t know if it makes a difference).

Источник

Git tab completion not working in zsh on mac

No matter what I try and do I can’t seem to make git tab/auto completion work in my zsh shell. I’ve downloaded the bash-completion script and the zsh-completion one and followed the instructions, but I can’t make it work.

I’ve reinstalled oh-my-zsh but that didn’t seem to help or make any difference.

Can anyone who’s got it working describe to me their setup so I can try an emulate it to get it working for me?

To be specific, what I’ve done so far is:

/.zsh/_git
Added zstyle ‘:completion:*:*:git:*’ script

/.zsh $fpath) is somewhere in your config (possibly provided by Oh My Zsh)?

7 Answers 7

Actually, ZSH does know how to do git completion out of the box, but you need to turn on the completion feature itself (which from the steps you described I guess you haven’t done)

Adding this to your .zshrc should be enough:

After you put the line .zshrc file, don’t forget to restart the shell for ZSH to pick up the new config (alternatively, you can execute the line in your current session, that’ll enable autocompletion for that one session)

Here’s a one-liner to both add the line and apply to current session:

PS Another answer here suggests installing the hub tool instead: although the tool is handy, it’s merely a 3rd party (github community) wrapper around git. Hence, it has nothing to do with the topic of «Git completion in ZSH»

Источник

Moving to zsh, part 5: Completions

In this series, I will document my experiences moving bash settings, configurations, and scripts over to zsh .

I am preparing a book on this topic, reworked and expanded with more detail and topics. Like my other books, I plan to update and add to it after release as well, keeping it relevant and useful. You can pre-order it on the Apple Books Store now.

As I have mentioned in the earlier posts, I am aware that there are many solutions out there that give you a pre-configured ‘shortcut’ into lots of zsh goodness. But I am interested in learning this the ‘hard way’ without shortcuts. Call me old-fashioned. (“Uphill! In the snow! Both ways!”)

What are Completions?

Man shells use the tab key (⇥) for completion. When you press that key, the shell tries to guess what you are typing and will complete it, or if the beginning of what you typed is ambiguous, suggest from a list of possible completions.

For example when you want to cd to your Documents folder, you can save typing:

When you hit the tab key, the system will complete the path to the Documents folder.

When the completion is ambiguous, the shell will list possible completions:

At this point, you can add a character or two to get to a unique completion, and hit the tab key again. In zsh you can also hit the tab key repeatedly to cycle through the suggested completions. In this example, the first tab keystroke will show the list, the second will complete

/Desktop/ , the third completes

/Documents , and so on.

You can use tab completion commands as well:

Not having to type path and file names saves time and avoids errors, especially with complex paths with spaces and other special characters:

Using tab completion is a huge productivity boost when using a shell.

Turning It On

In the default configuration, tab completion in zsh is very basic. It will complete commands and paths, but not much else. But you can enable a very powerful, and useful completion system.

zsh comes with a tool you can use to setup this completion system. When you run the compinstall command it will lead you through a complex and hard to understand list of menus which explains the options and will generate the code necessary to set this configuration up and add it to your .zshrc file or another configuration file of your choice.

Since the commands to configure the completion are quite arcane and hard to understand, this is a good way to get something to start out with. I will explain some of these options and commands in detail.

Whether you use compinstall or not, to turn on the more powerful completion system, you need to add at least this command to your zsh configuration file:

This will initialize the zsh completion system. The details of this system are documented here.

If you want to configure the system, the configuration commands (usually zstyle commands) should be added to the zsh configuration file before you enable the system. (This only matters for a few configurations, but as a general rule it is safer.)

All of these completion rules need to be loaded and prepared. zsh ’s completion system creates a cache in the file

/.zcompdump . The first time you run compinit it might take a noticeable time, but subsequent runs should use this cache and be much faster.

Sometimes, especially when building and debugging your own completion files, you may need to delete this file to force a rebuild:

Case Insensitive Completion

Since the macOS file systems are usually case-insensitive, I prefer my tab-completion to be case-insensitive as well. For bash you configure that in the

/.inputrc . In zsh you modify the completion systems behavior with this (monstrous) command:

I have seen many varieties for this configuration in different websites, but this is what compinstall adds when I select case-insensitive completion, so I am going with that.

Partial Completion

This is a particularly nice feature. You can type fragments of each path segment and the completion will try to complete them all at once:

If the fragments are ambiguous, there are different strategies to what the completion system suggests. I have configured these like this:

Commands with built-in completion

zsh comes with several completion definitions for many commands. For example, when you type cp and then hit tab, the system will correctly assume you want to complete a file path and show the suggestions from the current working directory.

However, when you type cp -⇥ the completion can tell from the — that you want to add an option to the command and suggest a list of options for cp , with short descriptions.

As the context of command prompt you are assembling changes, you may get different completion suggestions. For example, the completion for ssh will suggest host names:

zsh comes with completion definitions for many common commands. Nevertheless, it can be helpful to just hit tab, especially when wondering about options.

On macOS completions are stored in /usr/share/zsh/5.3/functions (replace the 5.3 with 5.7.1 in Catalina). This directory stores many functions used with zsh and is in the default fpath . All the files in that directory that start with an underscore _ contain the completion definitions command. So, the file _cp contains the definition for the cp command. (Some of the definition files contain the definitions for multiple commands.)

Completions for macOS Commands

There are even a few macOS specific command that come with the default zsh installation.

macOS High Sierra and macOS Mojave come with zsh 5.3, which is now nearly two years old. zsh 5.3 contains less macOS specific completion definitions than the current zsh 5.7.1 which will is the pre-installed zsh in macOS Catalina. Some of the completions in 5.3 have also been updated in 5.7.1.

Tool zsh 5.3 zsh 5.7.1
caffeinate
defaults
fink
fs_usage
hdiutil
mdfind
mdls
mdutil
networksetup
nvram
open
osascript
otool
pbcopy / pbpaste
plutil
say
sc_usage
scselect
scutil
softwareupdate
sw_vers
swift
system_profiler
xcode_select

Load bash completions

Since the default shell on macOS has been bash for so long, there are quite a few bash completion definitions for macOS commands and third party tools available. For example Tony Williams’ bash completion for autopkg (post, Github).

You do not have to rewrite these completions, since the zsh completion system can use bash completion scripts as well: (add this to your zsh configuration file)

When you have multiple bash completion scripts you want to load, you only need to load bashcompinit once.

Build your own completions

Once you start using completions, you will want to have them everywhere. While many built-in completions exists, there are still many commands that lack a good definition.

Some commands, like the swift command line tool, have a built-in option to generate the completion syntax. You can then store that in a file and put it in your fpath :

Note: in the case of swift , its definition will conflict with the _openstack definition in zsh 5.3. You can fix this with the command compdef _swift swift after loading the completion system.

Some commands provide a list of options and arguments with the -h/—help option. If this list follows a certain syntax, you can get a decent completion working with

One example on macOS, where this has decent results is the xed command which opens a file or folder in Xcode.

But for best results, you will often have to build the description yourself. Unfortunately this is not a simple task. The syntax is meticulously, but also quite abstractly documented in the zsh documentation for the Completion System. I also found the ‘howto’ documentation in the zsh-completions repository very useful, as well as the ‘ zsh Completion Style Guide.’

To avoid everyone re-inventing the wheel, I have started a repository on Github for macOS specific completion files. The page has the instructions on how to install them and I will welcome pull requests with contributions. Since I am just starting to learn this as well, I am sure there are improvements that can be made on the completions I have built so far and there are several commands where you can test your skills and build a new one.

I suggest the #zsh channel on the MacAdmins Slack for discussion.

In the next post in this series, we will discuss how to configure zsh ’s command line prompt.

Источник

Читайте также:  Файл не копируется linux
Оцените статью