Skip to content

Commit 845c50c

Browse files
committed
Shell completions, part 2
1 parent a6945a3 commit 845c50c

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

en/book/10-git-in-other-environments/chapter10.asc

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,39 +116,80 @@ Visual Studio 2013's Git support is a separate feature with its own command pane
116116
=== Git in Bash
117117

118118
If you're a Bash user, you can tap into some of your shell's features to make your experience with Git a lot friendlier.
119+
Git actually ships with plugins for several shells, but it's not turned on by default.
119120

120-
One way is by enabling tab-completion.
121-
// TODO
122-
123-
To enable the tab-completion plugin, open your +.bashrc+ and add a line like this:
121+
First, you need to get a copy of the `contrib/completion/git-completion.bash` file out of the Git source code.
122+
Copy it somewhere handy, like your home directory, and add this to your `.bashrc`:
124123

124+
[source,shell]
125125
-----
126-
# TODO
126+
. ~/git-completion.bash
127127
-----
128128

129+
Once that's done, change your directory to a git repository, and type:
130+
131+
[source,shell]
132+
----
133+
$ git chec<tab>
134+
----
135+
136+
…and Bash will auto-complete to `git checkout`.
137+
This works with all of Git's subcommands, command-line parameters, and remotes and ref names where appropriate.
138+
129139
It's also useful to customize your prompt to show information about the current directory's Git repository.
130140
This can be as simple or complex as you want, but there are generally a few key pieces of information that most people want, like the current branch, and the status of the working directory.
131141
To add these to your prompt, just copy the `contrib/completion/git-prompt.sh` file from Git's source repository to your home directory, add something like this to your +.bashrc+:
132142

143+
[source,shell]
133144
-----
134145
. ~/git-prompt.sh
135146
export GIT_PS1_SHOWDIRTYSTATE=1
136147
export PS1='\w$(__git_ps1 " (%s)")\$ '
137148
-----
138149

139-
This makes your shell prompt look like <<git_bash>> when you're anywhere inside a Git-controlled project.
150+
The `\w` means print the current working directory, the `\$` prints the `$` part of the prompt, and `__git_ps1 " (%s)"` calls the function provided by `git-prompt.sh` with a formatting argument.
151+
Now your bash prompt will look like <<git_bash>> when you're anywhere inside a Git-controlled project.
140152

141153
[[git_bash]]
142154
.Customized +bash+ prompt.
143155
image::images/git-bash.png[Customized +bash+ prompt.]
144156

157+
Both of these scripts come with helpful documentation; take a look at the contents of `git-completion.bash` and `git-prompt.sh` for more information.
158+
145159
=== Git in Zsh
146160

147-
If you're a Zsh user, TODO
161+
Git also ships with a tab-completion library for Zsh.
162+
Just copy `contrib/completion/git-completion.zsh` to your home directory and source it from your `.zshrc`.
163+
Zsh's interface is a bit more powerful than Bash's:
164+
165+
[source,shell]
166+
----
167+
$ git che<tab>
168+
check-attr -- display gitattributes information
169+
check-ref-format -- ensure that a reference name is well formed
170+
checkout -- checkout branch or paths to working tree
171+
checkout-index -- copy files from index to working directory
172+
cherry -- find commits not merged upstream
173+
cherry-pick -- apply changes introduced by some existing commits
174+
----
175+
176+
Ambiguous tab-completions aren't just listed.
177+
They have helpful descriptions, and you can graphically navigate the list by repeatedly hitting tab.
178+
This works with Git commands, their arguments, and names of things inside the repository (like refs and remotes), as well filenames and all the other things Zsh knows how to tab-complete.
179+
180+
Zsh happens to be fairly compatible with Bash when it comes to prompt customization, but it allows you to have a right-side prompt as well.
181+
To include the branch name on the right side, add these lines to your `~/.zshrc` file:
182+
183+
[source,shell]
184+
----
185+
setopt prompt_subst
186+
. ~/git-prompt.sh
187+
export RPROMPT=$'$(__git_ps1 "%s")'
188+
----
189+
190+
This results in TODO
148191

149-
* tab completion in the box?
150-
* raw zsh custom prompt
151-
* oh-my-zsh, themes
192+
TODO: oh-my-zsh, themes
152193

153194
=== Git in Powershell
154195

@@ -161,12 +202,14 @@ image::images/git-posh.png[Powershell with Posh-git.]
161202

162203
If you've installed GitHub for Windows, Posh-Git is included by default, and all you have to do is add these lines to your +profile.ps1+ (which is usually located in +C:\Users\<username>\Documents\WindowsPowerShell+):
163204

205+
[source,powershell]
164206
-----
165207
# TODO
166208
-----
167209

168210
If you're not a GitHub for Windows user, just download a Posh-Git release from (TODO: URL), uncompress it to the +WindowsPowershell+ directory, and add this to your +profile.ps1+:
169211

212+
[source,powershell]
170213
-----
171214
# TODO
172215
-----

0 commit comments

Comments
 (0)