
Little known features of iTerm2 - milkbikis
https://banga.github.io/blog/2020/03/02/little-known-features-of-iterm2.html
======
nrclark
Not sure whether or not this is a little-known feature.  iTerm2 has _fantastic_
integration with tmux (software used for persisting a terminal session across
multiple logins). 

Tmux can be a pretty complex piece of software, but iTerm can basically wrap
it all up into a nice package.  You don't need to know anything at all about
tmux to use iTerm's tmux integration. 

If you're SSHed into a server that has tmux installed, try running `tmux -CC`
on the server.  It'll pop up a new window that looks and feels just like it's
running natively (complete with tab support), except it's all tunneled over
SSH. 

And if you disconnect, you can just reconnect later and your windows will all
come back in the same state as when you left them. 

I use this one-liner to SSH into a server and reconnect (or start) a tmux
session:

    
    
        ssh -XY -F user@hostname -Ct \
         'sh -l -c "exec tmux -CC -u new-session -AD -s remote"'
    

iTerm2's tmux integration makes SSH access to remote machines feel almost as
native as using your machine locally.  It's really a killer feature, and it
made iTerm2 worth a donation for me. 

~~~
Myrmornis
I use iTerm2 with tmux every day (but I don’t ssh into machines often
nowadays) and I feel oppositely — I don’t get the attraction of the tmux
integration.  But maybe I still haven’t understood it, so please feel free to
tell me what I’m missing.  For me, the purpose of using tmux on my laptop is
session, window, and pane management. ( Perhaps my favorite tmux feature is
zoom-pane.)  So I find that the iTerm2 tmux integration takes away everything I
liked about tmux, and makes using tmux feel like using iTerm2 without tmux! 

~~~
CJefferson
I love tmux just for letting me run long running sessions on remote machines. 
I don't like tmux's window management, I want to use my terminal, scroll back
with mouse, that kind of thing.  Tmux integration is therefore for those of us
who love their GUI terminal :)

~~~
nobleach
You can always `set -g mouse on` in your `tmux.conf`.  But that has the nasty
downside of stopping you from being able to right-click and copy text to your
OS clipboard.  It does let you pane select and scroll with a mouse though. 

------
snide
I was one of those vim users that was stuck on MacVim for years because my
brain was too wired to hitting "cmd-s" for save and other dedicated app
niceties.  I mean, sure I can :w or bind it to another key, but there are a
couple shared OS-level shortcuts like this I just like to retain.  No worry,
iTerm2 can capture ANYTHING, and rebind it as needed.  This means I have a few
remaps like "cmd-s" in iterm2 to map to obscure key combinations, which I then
have vim interpret and pass along to whatever I want to bind over there.  I had
no idea the feature existed for years.  And sure, I know you're all laughing,
why would I even do such a thing... but vim is vim, and we all have our own
weird version of it. 

iTerm2 is amazing.  I've been using it for years daily and it is 100% one of
the best, most reliable pieces of software in my toolkit, right next to
Alfred. 

Show you love for George and contribute to his Patreon[0].  We always love to
complain about how to make money with open source on HN.  I'd love to see him
making a lot more than he does, if for no other reason than we can get more
people working full time on the project and provide some redundancy for a
critical tool we all love.  I wish I could pay for it, but I guess Patreon
works. 

[0]: [https://www.patreon.com/gnachman](https://www.patreon.com/gnachman)

~~~
AlchemistCamp
Really?  I didn't think iTerm would pass along cmd keys, which is the primary
reason I use MacVim instead. 

For example, I use these kinds of mappings:

    
    
        nmap <D-j> <C-j>
        nmap <D-k> <C-k>
        nmap <D-h> <C-h>
        nmap <D-l> <C-l>
        nmap <C-[> <C-T>
        nmap <D-]> <C-]>
        nmap <D-[> <C-]>
        nmap <D-p> :CtrlP pwd<cr>
        nmap <D-r> :CtrlPBufTag<cr>
        nmap <D-e> :CtrlPMRUFiles<cr>
    

This way, when on a Mac I can use cmd in place of ctrl for various commands I
use all the time that are less convenient with ctrl. 

In my .gvimrc, I added the following to make sure that cmd-p and cmd-l don't
get incercepted: "Disable the print key for MacVim" if has("gui_macvim")
macmenu &File. Print key=<nop> macmenu &Tools. List\ Errors key=<nop> "macmenu
&MacVim. Hide\ MacVim key=<nop> set macligatures endif

If there's any possible way to accomplish the same with iTerm, I'd give it
another try! 

~~~
snide
Sorry for seeing this late.  Basically, iTerm allows you to send Escape
sequences and even Vim special characters. 

Here's a screenshot [http://snid.es/d156c6b7deea](http://snid.es/d156c6b7deea)

In mine, under profiles/keys, I've set ⌘s to [17~

This is essentially F6, which I then bind in vim to

nnoremap <F6> :w<CR> imap <F6> <Esc>:w<CR>

I do this with a lot of ⌘ or otherwise unbindable settings (again, this only
effects iterm2).  This is how you can get iterm2 to pick up any keybinding and
send it to vim as something else. 

~~~
AlchemistCamp
I think this is a bit different than what I wrote above.  I just want to send
the command key as itself. 

It looks like you're changing your .vimrc itself. 

How would I get iTerm to just send cmd+p as cmd+p?  I tried making a keymapping
in that menu with "Key Combination": "cmd+p", "Action": "Do Not Remap
Modifiers".  Rather than sending it to VIM, it still brings up a print screen
dialogue. 

Similarly, I can't get cmd+[, cmd+], cmd+h (or j, k, l, etc) to send through
to VIM. 

With both VS Code and MacVim, that stuff is easily configurable.  My failure
(thus far) to figure out how to do the same with iTerm is why I don't really
use it. 

------
raamdev
I’m surprised to see that the iTerm2 dedicated hotkey window hasn’t been
mentioned.  It’s essentially a system-wide terminal window that you can open
with a hotkey, similar to the old Visor app, and you can configure it as a
floating window that appears over other apps' full screen windows. 
[https://www.iterm2.com/documentation-
hotkey.html](https://www.iterm2.com/documentation-hotkey.html)

~~~
jaaames
Yeah - I use this workflow and love it.  cmd + esp (with caps lock remapped to
escape) to toggle a full screen terminal from any application.  Means don't
have to cmd + tab and hunt for it, it's always one shortcut away. 

~~~
Myrmornis
Agreed this is the most important feature for me.  I use it in the same way. 

------
saagarjha
A couple more that I use often:

* imgcat: you can view images without leaving your terminal.  Somehow this works even across SSH connections…

* Python scripting API: basically AppleScript for iTerm except it lets you touch pretty much everything and it's not soul-crushing to write.  I have mine switch between themes based on the system appearance (which changes based on the time of day). 

~~~
edge17

      -bash: imgcat: command not found
    

am I missing something? 

~~~
sankha93
GP is talking about the inline images protocol:
[https://www.iterm2.com/documentation-
images.html](https://www.iterm2.com/documentation-images.html)

------
yen223
I've found a lot of use from cmd-shift-e, which shows timestamps for each
output line in the terminal.  Great for figuring out how long a process ran
for. 

~~~
chaos_a
Is there a way to make the timestamps permanent?  Kinda sucks that they
disappear when you close iTerm. 

~~~
paxswill
Would having them print directly in the shell prompt work?  For bash, I combine
PS0 support (in bash >=4.4) and PROMPT_COMMAND to do this. 

My prompt command:
[https://github.com/paxswill/dotfiles/blob/c4c6d50123fc2535e2...](https://github.com/paxswill/dotfiles/blob/c4c6d50123fc2535e23cb75e6da576d46f93ca4b/util/apps.sh)

And PS0:
[https://github.com/paxswill/dotfiles/blob/c4c6d50123fc2535e2...](https://github.com/paxswill/dotfiles/blob/c4c6d50123fc2535e23cb75e6da576d46f93ca4b/util/apps.sh)

Basically the prompt command writes the current time stamp on the right edge
of the window whenever the prompt is printed.  Then PS0 overwrites that time
stamp right before a command is executed. 

If you want the time stamp on the left you can skip the prompt command and
tput usage by using the \t escape code in PS1, but PS0 will need to be
modified for the new location while still using tput. 

------
gnachman
Hi, author here.  What a great way to wake up, an HN love fest :) Check our the
Tip of the Day feature, which is meant to help you find stuff like this. 

~~~
jhallenworld
Any chance of a port to Linux/X (Ubuntu) and especially Windows?  I recently
needed a terminal emulator that supports Sixel. 

~~~
gnachman
It’s a simple matter of funding me for the next 7 years :)

~~~
jhallenworld
I suspect it's a simple matter of giving you a job where you are forced to use
Windows for everything :)

------
_chendo_
Semantic History is pretty handy and not well-known.  It lets you Command+Click
on a path to open it in your editor (or associated application). 

It also lets you Command+Drag a path to get a usable file-handle to drop into
other apps that handle file-related dropzones. 

Demo video I made when I added the feature:
[https://vimeo.com/21872771](https://vimeo.com/21872771)

~~~
Myrmornis
Can it extract the correct line number from tracebacks, compiler / linter
output etc?  I wrote this tool, but I know George Nachman has added some
features since I wtote it:

[https://github.com/dandavison/iterm2-dwim](https://github.com/dandavison/iterm2-dwim)

~~~
_chendo_
It should, I originally built it for rspec output but it should work with most
common formats that put the line number attached to the path with a colon. 

~~~
gnachman
Hey!  Glad to see you’re still around.  Hope you’re doing well :)

~~~
_chendo_
Oh hey!  Still kicking about but haven't really done Mac dev for a couple of
years.  Hope you're doing well too and keep up the great work with iTerm2! 

------
donatj
This feels like a dumb question, but is the text rendering really dark for
others in iTerm2?  It seems like on a black background I cannot get pure white
text.  It’s always a middle shade of gray for me - before and after getting a
Retina display. 

I have messed with the colors and text rendering for literal hours over the
last ten years and I genuinely cannot get it as clear and readable as
Terminal.app which is a big part of the reason I always end up falling back to
Terminal

~~~
cerberusss
iTerm -> Preferences -> Profiles tab -> Colors subtab. 

You'll see two columns of options.  The left column is titled "Basic Colors",
the right is titled "ANSI Colors". 

Under Basic Colors, click the color next to Foreground, and a color picker
appears.  Slide it to the lower-left: bright white. 

Now it should be fixed.  But if it didn't, repeat the same procedure for the
White that's at the bottom of the column ANSI Colors. 

~~~
TurkTurkleton
There's also a setting that controls the thickness of font strokes,
independent of color.  I forget exactly how it's labeled (not at my Mac right
now), but I remember it's a dropdown with combinations of options for using
thin strokes on dark backgrounds and/or Retina displays.  I believe it defaults
to using thin strokes on Retina displays.  I believe turning this off will make
font rendering identical to Terminal.  Either way, I think it looks better
turned off, even on a Retina display. 

------
guessmyname
iTerm2 has so many features [1] I don’t think I have enough space here to
promote them all. 

Please consider setting up recurrent donations to George Nachman [2] to
support his work. 

[1] [https://www.iterm2.com/documentation-one-
page.html](https://www.iterm2.com/documentation-one-page.html)

[2] [https://www.iterm2.com/donate.html](https://www.iterm2.com/donate.html)

------
Myrmornis
A little plug: iterm2-dwim is a click handler that makes use of the iTerm2
smart selection feature to open files in your editor at the correct line. 

[https://github.com/dandavison/iterm2-dwim](https://github.com/dandavison/iterm2-dwim)

From the README:

iterm2-dwim is a click handler for iTerm2.  The aim is that you command-click
on any file path, relative or absolute, and it opens the file in your editor. 
If there was a line number, your editor goes to that line.  So, compiler/linter
output, tracebacks, git output, etc. 

Currently Emacs, PyCharm and Sublime are supported. 

~~~
drcongo
I _love_ this.  Thank you. 

~~~
Myrmornis
No problem!  Don’t hesitate to get in touch on github if you have questions /
issues. 

------
mark_l_watson
For me, the killer feature is supporting inline plots in a shell, in a
terminal-based Emacs session, etc.  I have a short section in my latest book on
setting it up, which you can read free online: [https://leanpub.com/hy-lisp-
python/read#leanpub-auto-bonus-p...](https://leanpub.com/hy-lisp-
python/read#leanpub-auto-bonus-points-configuration-for-macos-and-iterm2-for-
generating-plots-inline-in-a-hy-repl-and-shell)

------
aloer
I’ve said it before: iterm2 is one of the two reasons I feel so much more
productive on mac compared to Windows or Ubuntu

The other one being quicklook (space preview of files)

~~~
jfkebwjsbx
Weird thing to say, considering both Windows and Linux-based systems are way
more developer-friendly. 

(Windows due to VS and the rest of MS resources/docs/tools, Linux due to its
package management systems natively supporting just about anything you could
ever need for development.  For macOS to compete, you need a plethora of best-
effort or paid tools that don’t come from Apple). 

~~~
atom-morgan
What's your iTerm2 replacement on Windows?  I use Hyper right now but the clear
command doesn't even work correctly. 

~~~
garethrowlands
Have you given Windows Terminal a try?  It's in the store. 
[https://github.com/microsoft/terminal](https://github.com/microsoft/terminal)

------
shabbyrobe
iTerm2 is fantastic!  It can be a bit CPU hungry but you get a stack of mod
cons out of the bargain. 

There are tons of proprietary escape codes, not least of all the incredibly
useful one for "fireworks"! [ 1]

    
    
        ^[]1337;RequestAttention=fireworks^G
    

[1]: [https://iterm2.com/documentation-escape-
codes.html](https://iterm2.com/documentation-escape-codes.html)

~~~
peteretep
To save people needing to lookup how to use that:

    
    
        perl -e 'print "\e]1337;RequestAttention=fireworks\007\n"'

~~~
MockObject
and

    
    
       echo -e "\x1B]1337;RequestAttention=fireworks\007"

------
wincy
I thought I was being fancy when I figured out I could use CMD+shift+c to get
vim-style visual selection to yank some terminal output in a pinch. 

------
ashton314
iTerm2 is going to be the killer app that keeps me on macOS for a long time. 
Is there anything remotely as good for Linux systems? 

------
Corrado
I love iTerm2 and have been using it for years.  Thanks to this post I've even
learned a couple of new things that make me love it even more. 

One thing I've been trying to get working in iTerm2 has been AWS profile
badges.  iTerm2 has the ability to use text "badges"[0] to give feedback about
the current environment.  I'm an AWS engineer and tend to switch between
environments a lot, so I thought it would be great to use the badges
functionality to let me know when I was in the production environment vs. say,
the development environment.  This seems pretty simple but I haven't been able
to get it to work. 

[0] [https://iterm2.com/documentation-
badges.html](https://iterm2.com/documentation-badges.html)

~~~
MockObject
How does your shell know which environment you're in?  Is it that you ssh into
one environment or another one, and you want the badge to show which one
you've connected to? 

~~~
Corrado
I cut and paste environment variables into the shell to authenticate to
different accounts.  Originally, I was trying to trigger an event by looking
for specific words (ie.  AWS_SECRET_ID) but that didn't seem to work reliably. 

~~~
MockObject
If you're switching between envs with cut and paste, maybe you could do
likewise with the badge? 

    
    
       echo -e "\x1B]1337;SetBadgeFormat=$(echo "production" | base64)\007"
    
       echo -e "\x1B]1337;SetBadgeFormat=$(echo "development" | base64)\007"
    

Consider giving yourself some bash functions with appropriate names, which set
the env vars and switch the badge too.  Then instead of pasting each time,
you'll just use the function name in the shell. 

[https://linuxize.com/post/bash-functions/](https://linuxize.com/post/bash-
functions/)

------
bad_user
iTerm2 is what made me finally use Emacs in the terminal, completely replacing
vim for me. 

You can configure Emacs when running from the terminal to respond to mouse
input or to integrate with the system's clipboard.  You make it run in server
mode, via a Launchd config and you get instant startup times too. 

iTerm2 also has built-in powerline glyphs.  It was actually easier to configure
Emacs's powerline package to work from the terminal than it was to work from
the GUI version. 

I also like iTerm2's tmux integration. 

iTerm2 is one of the reasons why I won't abandon MacOS for my software
development needs anytime soon. 

~~~
allarm
Why using Emacs in terminal though?  I find it much better to run in gui. 

------
locusofself
tmux -CC support is my favorite feature of iTerm2.  It's amazing. 

~~~
rhlsthrm
Can anyone ELI5 on Tmux?  I want to know if I’m missing out on an awesome tool
by not knowing about it by having heard the name it a lot. 

~~~
nrclark
Have you ever needed to run a long-running job over SSH?  If your command is
still running and SSH disconnects, your command gets terminated.  You can work
around this by using nohup like this: `nohup <command> &` to launch your
program. 

Tmux is kind of like nohup, but for the whole shell.  If you SSH into a
terminal and start tmux, it'll give you a shell that survives
disconnect/reconnect. 

Tmux can also act sort-of like a tiling window manager for your shells.  So you
could have a few shells open, and maybe split your screen 50/50 between two
shells, or 50/25/25 or something. 

It's a pretty useful program if you spend a lot of your day SSHed into
external machines (or if you like to work locally in a full-screen terminal
window).  It's also got some esoteric keybindings and a steep learning curve
and it messes around with your mouse, scrollback, and copy-paste. 

iTerm2's tmux integration ignores the disconnect/reconnect stuff (although if
you reconnect, your session will just "come back" in whatever state it was
when you disconnected).  The killer feature is that it takes tmux's window-
manager functions and maps them onto tabs and windows in the GUI app.  This
basically makes it look and feel like you're running iTerm from whatever
machine you're logged into over SSH, except that what actually happens under
the hood is a bunch of tmux magic. 

------
cs02rm0
Anyone else have any problems getting the first one to work? 

I could run _/ usr/local/bin/hub browse -- commit abc123_ without issue, but
iTerm wasn't even changing to cursor when hovering over a hash and holding
cmd. 

~~~
cs02rm0
Nevermind.  Think quitting iTerm was key. 

Off/on again. 

------
babayega2
Question.  Do you know how to get iTerm2 to open on split into the same
directory that it was in like in Terminator on Ubuntu?  Currently it always
open to home directory whether I do a vertical/horizontal split. 

~~~
kevsim
Go to Preferences->Profiles->Default and find the option over on the right for
"Working Directory".  Then set it to "Reuse previous session's directory". 

Then for me Cmd+D and Cmd+Shift+D (split with current profile) gives the same
current directory. 

~~~
sorahn
Depending on your workflow, you might even want the advanced configuration
version of this. 

I want new windows (which I rarely use) and new tabs to be my home directory,
but split panes to be the directory I was in when I split.  Works wonders. 

------
dzhiurgis
I love iTerm! 

I've wondered whether it's possible to setup profile-specific (or folder-
specific) aliases?  I understand it might be zsh feature that I'm really after,
but perhaps iTerm can solve it too? 

~~~
eddyg
Profile-specific aliases are easy. 

In your .zshrc (or .bashrc), just use the value of $ITERM_PROFILE to set (or
source a file containing) the aliases you want:

[[ $ITERM_PROFILE == “AWS” ]] && source ~/.aliases.aws

------
sk0g
I'm a little jealous of iTerm, not going to lie.  The UX is pretty nice from
what I've seen, but I can't find a like for like replacement for either
Windows or Linux. 

~~~
bdcravens
Every time I've considered a full-on switch to Linux, the many little tools
that I'd have to give up weigh heavily the cons column. 

~~~
pkrumins
Which tools would you have to give up? 

~~~
bdcravens
Some of the most obvious things I'd give up:

\- ability to plug into any monitor at any resolution that monitor supports

\- ability to copy and paste to/from my phone

\- Transmit - I really haven't found many good s3 clients on Linux

\- ScreenFlow

\- Git Tower

Most of these have some sort of a work-around or close analog of course. ( So
I'd likely not be "giving anything up" as much as compromising or learning new
techniques) However, I'm not sold on the benefits (other than ideological
ones, the only thing that comes to mind is increased Docker performance)
outweighing the costs.  For someone with a different workflow and set of tools,
a different conclusion may result. 

~~~
j88439h84
> ability to plug into any monitor at any resolution that monitor supports

Doesn't xrandr do that? 

~~~
toyg
Problem with xrandr is figuring out how and where to run it automatically. 

------
andreareina
iTerm2 has (had?  I stopped using it a while ago) the tendency to drop data
from a large paste.  No problems with the built-in Terminal.app so I just went
back to that. 

------
freakcage
Cool article, but here I am don't know how to jump between words on iterm2

~~~
mikeappell
You can set this up as a shortcut.  I forget exactly how I did it, but here's
what seems to be a similar guide on macos:

[https://coderwall.com/p/h6yfda/use-and-to-jump-forwards-
back...](https://coderwall.com/p/h6yfda/use-and-to-jump-forwards-backwards-
words-in-iterm-2-on-os-x)

