Hostingheaderbarlogoj
Join InMotion Hosting for $3.49/mo & get a year on Tuts+ FREE (worth $180). Start today.
Advertisement

Getting Spiffy With Powerline

by
Gift

Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.

Final product image
What You'll Be Creating

Every editor and terminal shell has a status line. It’s a place of information that tells you where you are in the file or directory structure, the state of the program, the git status if applicable, etc. Often it’s hard to change and almost always impossible to keep consistent across applications.

That’s where Powerline comes in. Powerline is a python daemon that supplies a consistent, nicely formated status line information that can be used in any editor or terminal shell.

You can also add extra information (weather, time, etc). By the end of the tutorial, you will be able to make the status line in terminal vim, MacVim, zsh, bash, or fish look the way you want it and have it display the information you need.

Installation

The easiest way to install is to use pip from Python. You will need to ensure that Python is version 2.7 or 3.3 to work. You can check the installed Python version with the following command:

python --version

It should return:

Python 2.7.5

If you have an older version of Python, just install a newer version using HomeBrew. If you haven’t installed HomeBrew yet, check out Homebrew Demystified: OS X's Ultimate Package Manager.

Once the Python version is up-to-date, you can use pip to install Powerline. Use this command line:

pip install --user git+git://github.com/Lokaltog/powerline

This will install the latest version from Powerline on GitHub. You should keep a copy of the documentation open in the web browser. 

You need to download a zip version from GitHub. The pip installation does not copy all of the files needed. It only gets what’s needed to make the library for Python. You will use the downloaded copy to install what does not get installed. 

Showing Pip's installation location for Powerline

To see where pip put everything, type this on the command line:

pip show powerline

You need to copy the directory location, and change into that directory.

Going to Pip's installation of Powerline

You can see the powerline directory and Python egg information in the directory. To open a Finder window in that directory, type:

open .
Opening Finder to the Directory and Copying Files

With one Finder window open to that directory, download and unzip the Powerline files from GitHub and copy the powerline and scripts directories from the GitHub download to this directory.

To create the basic configuration for Powerline, enter the following commands in the command line interface of Terminal:

cd
mkdir .config
mkdir .config/powerline

The first cd simply puts you into the home directory. If you already have a .config directory in the home directory, you can skip making it. Copy the configuration files from the powerline/config_files/ directory of the Powerline GitHub download to powerline directory you just created.

Font Installation

Powerline has to have special characters installed into the font used. You can download pre-compiled fonts with these symbols. I use the Inconsolata for Powerline.

Installing the Powerline Font

Once you download the font, change the name from Inconsolata for Powerline.otf to InconsolataPowerline.otf. FontBook will not open it correctly otherwise. After changing the name, double click it to open and install into the system.

Configuring Terminal and iTerm2

Before the different terminal shells get formatted, the Terminal and iTerm2 programs need to be configured to use the font that was downloaded.

Configuring Fonts on Terminal

Open the Preferences dialog for Terminal, select Settings, select Text, and select Change... next to the Font heading. Select the Inconsolata for Powerline font and save.

Configuring Fonts on iTerm2

For iTerm2, open the Preferences dialog, select Profiles, select Text, and select Change Font for the Regular Font to set the Inconsolata for Powerline font and save. Do the exact same for the Non-ASCII Font setting as well.

Tip: If the colors for Powerline end up differing between Terminal and iTerm2, you might want to check to see if the colors specifications are the same. These predefined colors will effect the colors used in Powerline. If you use different profiles, these presets should be the same across profiles to keep from changing Powerline’s look.

Configuring Fish

Fish is a highly configurable shell for all Unix based systems. If you use Alfred, you can use the Fish Toolbox workflow to easily edit the configuration file.

Using Fish Toolbox Workflow

In the Alfred Prompt, type f:editconfig and press the Enter key. The text editor you configured to use with the workflow will open to the Fish configuration file. Otherwise, open the ~/.config/fish/config.fish file in the text editor. Add the following lines at the bottom:

set fish_function_path $fish_function_path "/Users/raguay/Library/Python/2.7/lib/python/site-packages/powerline/bindings/fish"
powerline-setup

The set command needs to have the path to the Powerline directory as you obtained from the pip show powerline command above. The next line runs the powerline-setup fish function to create the Powerline prompt.

Powerline Powered Fish Prompt

Open a new iTerm or Terminal window with Fish should give you this prompt.

Configuring Zsh

To get Zsh using Powerline, you can install Oh-My-Zsh. Once installed, simply set the theme to agnoster theme by editing the .zshrc file in the home directory. Change the ZSH_THEME line to look like

ZSH_THEME="agnoster"
Powerline Powered Zsh Prompt Using Oh-My-Zsh Agnoster Theme

The Zsh prompt should now look like the above picture. The differences in the Fish prompt and the Zsh prompt have to do with the agnoster theme for Oh-My-Zsh being different than the default Powerline configuration. That’s the problem with this approach: Oh-My-Zsh themes change the colors from the Powerline scheme. You can control it some, but not completely.

If you do not want to use Oh-My-Zsh, then remove all references to Oh-My-Zsh from your .zshrc and place this at the bottom:

source /Users/raguay/Library/Python/2.7/lib/python/site-packages/powerline/bindings/zsh/powerline.zsh

The path should correspond to what you received from the pip show powerline command.

Powerline in Zsh Without Oh-My-Zsh

With it coded this way, none of the Oh-My-Zsh characteristics are in the way. It now looks exactly like the Fish prompt and is controlled by the Powerline configuration.

Configuring Bash

The Bash configuration is almost the exact same as the second approach to Zsh configuration. In your .bashrc file, place this at the bottom:

source /Users/raguay/Library/Python/2.7/lib/python/site-packages/powerline/bindings/bash/powerline.sh

The path should correspond to what you received from the pip show powerline command.

Bash Configuration

The Bash prompt now looks exactly like the Fish and the Zsh prompt.

Configuring Vim and Macvim

Using the text editor, open the .vimrc file and add these lines:

set rtp+=/Users/raguay/Library/Python/2.7/lib/python/site-packages/powerline/bindings/vim

" These lines setup the environment to show graphics and colors correctly.
set nocompatible
set t_Co=256

let g:minBufExplForceSyntaxEnable = 1
python from powerline.vim import setup as powerline_setup
python powerline_setup()
python del powerline_setup

if ! has('gui_running')
   set ttimeoutlen=10
   augroup FastEscape
      autocmd!
      au InsertEnter * set timeoutlen=0
      au InsertLeave * set timeoutlen=1000
   augroup END
endif

set laststatus=2 " Always display the statusline in all windows
set guifont=Inconsolata\ for\ Powerline:h14
set noshowmode " Hide the default mode text (e.g. -- INSERT -- below the statusline)

This configuration works for console Vim and MacVim. Notice the specification of the font in the set guifont definition. You have to escape the spaces in the name of the font. I like a large font, but you can make it smaller by changing the number next to the h. It’s set here for a 14pt font size.

Console Vim with Powerline

To install the lastest MacVim with the proper compiled settings for Powerline, use this HomeBrew install command:

brew install macvim --env-std --override-system-vim

This has MacVim override the system vim and use the standard environment.

MacVim with Powerline

MacVim has syntax highlighting pre-configured, but the console Vim doesn’t. The important thing is that the Powerline status line looks the same on both!

Configuring Powerline Color Schemes and Themes

To show how to change the colors and themes around, I'll modify the shell configurations. To change Vim and MacVim, you will follow a similar procedure.

All of the Powerline configuration files are in the ~/.config/powerline/ directory. There you will see:

cd ~/.config/powerline/
ls
colors.json colorschemes config.json themes

This directory contains two files (colors.json and config.json) and two directories (themes and colorschemes).

Tip: In Powerline, theme refers to the structure of information in the status line. It has nothing to do with colorization.

The colors.json file defines the numerical values for the color name. You will need to refer to this file to see what color names are defined or to create new ones.

The config.json file is the main configuration file for Powerline. This is the first place that Powerline looks for configuration information. It tells Powerline what color scheme and theme to use for each application.

The themes directory contains all the themes for all Powerline enabled apps. The themes for any shell will be under themes/shell/.

The colorschemes directory contains the different color schemes for all Powerline enabled apps. The color schemes for shells are in the colorschemes/shell/ directory.

Open the Powerline configuration file (~/.config/powerline/config.json).

If you are using Alfred, you can download the Powerline Toolbox Alfred workflow. Use the pl:conf command to browse the Powerline configuration directory. Selecting a file allows you to edit it. Selecting a directory allows you to enter it.

You should see a large json file. The section shell is what I'll modify:

"shell": {
	"colorscheme": "default",
	"theme": "default",
	"local_themes": {
		"continuation": "continuation",
		"select": "select"
	}
}

Change the colorscheme key to solarized.

Solarized Color Scheme Under the Default Color Scheme

That will make a nice start. To make your own, change the colorscheme key to mine in the config.json file, and copy colorschemes/shell/solarized.json file to colorschemes/shell/mine.json.

The mine.json will look like this:

{
	"name": "Solarized Dark",
	"groups": {
		"jobnum":               { "fg": "oldlace", "bg": "darkgreencopper" },
		"user":                 { "fg": "oldlace", "bg": "blue", "attr": ["bold"] },
		"superuser":            { "fg": "oldlace", "bg": "red", "attr": ["bold"] },
		"virtualenv":           { "fg": "oldlace", "bg": "green" },
		"branch":               { "fg": "gray61", "bg": "royalblue5" },
		"branch_dirty":         { "fg": "yellow", "bg": "royalblue5" },
		"branch_clean":         { "fg": "gray61", "bg": "royalblue5" },
		"continuation":         { "fg": "lightyellow", "bg": "darkgreencopper" },
		"continuation:current": { "fg": "oldlace", "bg": "darkgreencopper", "attr": ["bold"] },
		"cwd":                  { "fg": "lightyellow", "bg": "darkgreencopper" },
		"cwd:current_folder":   { "fg": "oldlace", "bg": "darkgreencopper", "attr": ["bold"] },
		"cwd:divider":          { "fg": "gray61", "bg": "darkgreencopper" },
		"hostname":             { "fg": "oldlace", "bg": "darkgreencopper" },
		"exit_fail":            { "fg": "oldlace", "bg": "red" },
		"exit_success":         { "fg": "oldlace", "bg": "green" },
		"environment":          { "fg": "oldlace", "bg": "green" },
		"mode":                 { "fg": "oldlace", "bg": "green", "attr": ["bold"] }
	},
	"mode_translations": {
		"vicmd": {
			"groups": {
				"mode": { "fg": "oldlace", "bg": "blue", "attr": ["bold"] }
			}
		}
	}
}

You need to change the name key to the value Mine. Also, make these changes:

"cwd":                  { "fg": "lightyellow", "bg": "gray13" },
"cwd:divider":          { "fg": "gray61", "bg": "gray13" },

When you save your file and hit return on the terminal, you will get the new color scheme.

The Mine Color Scheme Under the Solarized Color Scheme

It is useful to show a date and time indicator. In order to change the segment layouts, a new theme has to be created. In the config.json file, change the theme key to the value mine. Copy the theme/shell/default.json file to theme/shell/mine.json. Just before the json list containing the hostname, add this json list:

{
	"name": "date",
	"args": {
		"format": "%m-%d %H:%M"
	}
},

This bit of json code will add a new segment using the date command. The date command takes two arguments: format that contains the format for the strftime() python command, and istime that tells if you should use a separate time color scheme than the date one. The default value for istime is fine. This format will display the date by a month-day 24hour:minutes format.

Now that there is a new segment, a color scheme has to be added. In the colorscheme/shell/mine.json file, add these lines just above jobnum:

"date":		{"fg":"mediumcyan", "bg":"brightpurple"},
"time":		{"fg":"mediumcyan", "bg":"brightpurple"},
"time:divider":	{"fg":"mediumcyan", "bg":"white"},

In order to shorten the number of parent directories shown in the prompt, you will need to change the theme/shell/mine.json file to have the cwd segment look like this:

{
	"name": "cwd",
	"args": {
		"dir_limit_depth": 2
	}
},

The dir_limit_depth defines how many parent directories will be shown. Here it is set to 2.

Save the configuration files and you can see what it looks like.

Date and Time Added to the Shell Prompt

The download available with this tutorial has the configuration files that you will have by now. If you make an error in the files, the Powerline prompt will quickly display errors.

The list of common and application specific commands will help you find more ways to modify your prompt. Since it is all in python, you can even create your own!

Conclusion

In this tutorial I have shown you how to configure the Fish shell, Zsh shell, Bash shell, console Vim, MacVim to use Powerline. You now know how to change the look and feel of the shell prompt. The only thing left is to play around with your new knowledge and get the ultimate configuration just for you!

Advertisement