From 882a4b526305ad6b91e0b3c7280ca72566582f5f Mon Sep 17 00:00:00 2001 From: Daniel Segarra Date: Fri, 28 Jun 2019 03:25:10 -0400 Subject: [PATCH] docs: updating macOS terminal guide to include how to change your hostname and display branch name at the command prompt (#35974) --- .../macos-terminal/index.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/guide/english/terminal-commandline/macos-terminal/index.md b/guide/english/terminal-commandline/macos-terminal/index.md index 2a122d2d21..c315d6539e 100644 --- a/guide/english/terminal-commandline/macos-terminal/index.md +++ b/guide/english/terminal-commandline/macos-terminal/index.md @@ -114,6 +114,54 @@ kill #### Previewing file If you would like to preview a file, type the command `cat ` and you would be able to preview a text document through the terminal. +### Customizing command prompt + +If you'd like to you can change how your command prompt looks. +For example, many developers find it useful to view their current version control branch right in the command prompt (we will be accomplishing this). + +#### Changing your computer hostname +In short all you need to do is run the command below, replacing `` with whatever you would want to name your computer. + +``` +sudo scutil --set HostName +``` + +*Note*: This can be done natively through macOS settings `System Preferences -> Sharing -> Computer Name` + +#### Your bash_profile + +To accomplish this you need to alter your **bash profile**. +This can be viewed with the following command `cat ~/.bash_profile`. + +#### Altering bash_profile + +Open `~/.bash_profile` with you favorite editor and add the following: + +``` +git_branch() { + git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' +} + +export PS1="[\u@\h \W]\$(git_branch)\$ " +``` + +Here `git_branch()` is a function to print out the name of the branch. + +If you want you can add colors to the command prompt as well! + +``` +export PS1="\h \[\033[34m\]\W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ " +``` + +#### Apply Changes without signing in/out of terminal + +`source ~/.bashrc` + +- [Change computer hostname](https://knowledge.autodesk.com/search-result/caas/sfdcarticles/sfdcarticles/Setting-the-Mac-hostname-or-computer-name-from-the-terminal.html) +- [Show git branch in terminal command prompt](https://www.shellhacks.com/show-git-branch-terminal-command-prompt/) +- [Bash prompt escape sequences](http://tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html) +- [More bash configurations](https://gist.github.com/justintv/168835) + #### Search your command history as you type ```control+R```