My .zshrc file on Mac (adapted from .bashrc and .inputrc)

Matt Lim
4 min readJan 12, 2020

Since macOS Catalina uses zsh by default, and I just got a new Mac, I decided to make the switch from bash. It’s not that different — I didn’t even notice I was using zsh until I tried to source my .inputrc file and it didn’t work. The main thing I had to do was port over my .bashrc and .inputrc files to a new .zshrc file. I figured I’d write about it since it took me more time than I would’ve liked (hopefully if you’re reading this, it takes you less time!). Anyways, here it is.

Cool, that was easy.

Well, guess I might as well do a quick run through since I’ll inevitably forget how this thing works in a week or two.

Startup

##### STARTUP
echo "hello mlim"
RPS1="${${KEYMAP/vicmd/-- NORMAL --}/(main|viins)/-- INSERT --}"
RPS2=$RPS1

This part is simple. Whenever I create a new iterm window or tab, “hello mlim” is printed and Vim’s mode is displayed. The first part isn’t really important, but if I don’t do the RSP1/RSP2 stuff, then the first terminal line won’t display Vim’s mode. I guess that’s also not that important, it‘s just nice to have.

Bindings

##### BINDINGS
bindkey "^R" history-incremental-search-backward
bindkey "\e[A" history-beginning-search-backward
bindkey "\e[B" history-beginning-search-forward

The first binding makes it so that you can search your history with Ctrl-R. The latter two make it so that Up Arrow and Down Arrow take history into account. For example, let’s say my history looks like this:

131  cat abcc
132 cat abc
133 cat abcd
134 cat abcde
135 cat wut
136 cat yo
137 cat hello

Then, if I type cat abc and press Up Arrow three times, I’ll get abcde, then abcd, then abcc. Pressing Down Arrow will go back through those entries. That is, history is searched for things beginning with cat abc. The default behavior for Up Arrow and Down Arrow is just to cycle through the history regardless of what’s on the command line.

Note: Here’s a cool trick I learned from Stack Overflow. If you use cat without any arguments and start typing, you can see the character sequences used for all the different keys (e.g. Up Arrow is ^[[A, which is equivalent to \e[A).

Vim

##### VIM STUFF
bindkey '\e' vi-cmd-mode
# Make Vi mode transitions faster (KEYTIMEOUT is in hundredths of a second)
export KEYTIMEOUT=1
function zle-line-init zle-keymap-select {
RPS1="${${KEYMAP/vicmd/-- NORMAL --}/(main|viins)/-- INSERT --}"
RPS2=$RPS1
zle reset-prompt
}
zle -N zle-line-init
zle -N zle-keymap-select

These bunch of lines do two things:

  1. Make it so that pressing Esc switches to Vim’s command mode.
  2. Make it so that Vim’s mode is displayed on the right-hand side of every line.
Displaying Vim’s mode

In short, #2 works kinda like this (details may be not totally correct, my knowledge of zsh was gained in the past few minutes):

  • The “function” line defines two functions with the same implementation. These functions update the right-hand side prompt.
  • The zle -N lines bind those two functions to certain zsh events (see here for a list).

History

##### HISTORY
HISTFILE=~/.zsh_history
HISTSIZE=100000000
SAVEHIST=100000000
setopt INC_APPEND_HISTORY
setopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_DUPS

The first part makes it so that I can have a really long history. Then…

  • INC_APPEND_HISTORY makes it so that 1) zsh sessions append to the history rather than replacing it, e.g. when I restart my terminal I still have my history and 2) new commands get added to the history greedily instead of when the shell exits, e.g. when I enter a command in one tab I have it in the history of my other tabs.
  • HIST_FIND_NO_DUPS makes it so that Up Arrow and Down Arrow ignore duplicates.
  • HIST_IGNORE_DUPS makes it so that consecutive duplicates don’t get added to my history.

Check out this page for more history options.

.bashrc and .inputrc

For context, here’s what my .bashrc and .inputrc files look like on my other computer (minus aliases and adding to my path and other stuff you definitely don’t care about).

My .bashrc is basically the equivalent of the “history” section of my .zshrc. Looking at them side-by-side, I think the zsh version is actually a bit cleaner.

My .inputrc is basically the equivalent of the “bindings” and “Vim” sections of my .zshrc combined. It’s a bit more work to display Vim’s mode in zsh, but you get more control as a result.

--

--

Matt Lim
Matt Lim

Written by Matt Lim

Software Engineer. Tweeting @pencilflip. Mediocre boulderer, amateur tennis player, terrible at Avalon. https://www.mattlim.me/