ITerm2, zsh and .zshhistory – how to have separate zsh history

bashitermpathterminal

I installed iTerm2 on Catalina 10.15.6 on a MacBook Pro bought in early 2020 and have two profiles – Default and MyUser.

For MyUser, I set a custom location of /Users/john-doe/iTerm2/zsh (copy of zsh from /bin/zsh) and set a custom .zshrc file via the command

touch .zshrc

The default zsh file (/bin/zsh) saves to .zsh_history in Users/john-doe (home directory) but I am not sure how to with zsh, or iTerm2, set a custom location for that profile's installation of zsh.

If anyone could help I'd appreciate that!

Best Answer

I think you're confusing iTerm2 Profiles and user shells. So, I wrote this primer to help you get started/clarification.

The Terminal Client

When I say "Terminal" I don't mean Apple's Terminal App. I mean Terminal Emulator Clients. iTerm2 is a terminal emulator has absolutely nothing to do with with the remote computer other than providing an interface on your local machine through which you connect to the remote.

Whether you use iTerm2, Terminal, PuTTY for Mac, or the myriad of SSH clients available, they all do basically the same thing - give you an interface to work with the connected device. Some of these clients offer just the nitty gritty; iTerm2 has tons of features. For example, some clients (like iTerm2 and Terminal) allow you to store profiles which is what configures iTerm2 and it's behavior. You can set the background/foreground colors, the blinking of the cursor, how URLs are handled etc., but none of it is related to the actual shell on the remote computer.

You can have 1 remote assigned 1 profile or 100 remotes assigned to 1 profile. Multiple profiles on your computer doesn't give you multiple users or sessions on the remote. Multiple profiles just gives you multiple ways of interacting with the remote computer.

When you Connect

It would be beneficial if you read my primer on ZSH files: ZSH: .zprofile, .zshrc, .zlogin - What goes where? because it will help you in understanding what happens when you connect to the remote machine.

When you connect to a remote machine, whether it's your personal Mac or some big shared computer with many users, you are assigned a shell; assigned when your account was created. On new Catalina installs, the shell is ZSH. Mojave and earlier, it's Bash. You could install newer and different shells like CSH or TCSH or even fish and then choose which one you used *when you connected or authenticated.

When you log in, ZSH reads it's configuration files in order (see the above link). It starts with the ones in /etc/ then moves onto the ones in your home directory /Users/username/. As a side note, your home directory can be referenced as a variable $HOME or with the tilde notation; ~/ (eg: ~/Documents)

Bottom line here is that every time you invoke the shell it's going to read those files - nothing in iTerm will affect that.

ZSH History File

ZSH has no built-in setting for the history file. It's set by environment variable, HISTFILE in any of the following: /etc/zprofile, /etc/zshrc, ~/.zprofile or ~/.zshrc. In macOS, it defaults to .zsh_history. You can set it to anything you want by editing any one of those four files with the line HISTFILE=foo.bar.

Every time you load a shell (and you can load a shell within a shell within another shell) it's going to read those files. Again, your iTerm2 profile has nothing to do with it; your history file is going to be what you (or the admin) configured it to be.

TL;DR

For MyUser, I set a custom location of /Users/john-doe/iTerm2/zsh (copy of zsh from /bin/zsh) and set a custom .zshrc file via the command

That's not how it works. The user shell on the remote has nothing to do with the Terminal client's profiles. You can't link a custom shell config file with a terminal profile the way you're trying to. As far iTerm2 and the shell, they actually have nothing to do with each other much like the Mail App has nothing to do with Gmail's servers other than a method to connect to them.