Skip to content

Instantly share code, notes, and snippets.

@squarism
Last active May 30, 2024 14:42
Show Gist options
  • Save squarism/ae3613daf5c01a98ba3a to your computer and use it in GitHub Desktop.
Save squarism/ae3613daf5c01a98ba3a to your computer and use it in GitHub Desktop.
An iTerm2 Cheatsheet

Tabs and Windows

Function Shortcut
New Tab + T
Close Tab or Window + W (same as many mac apps)
Go to Tab + Number Key (ie: ⌘2 is 2nd tab)
Go to Split Pane by Direction + Option + Arrow Key
Cycle iTerm Windows + backtick (true of all mac apps and works with desktops/mission control)
Splitting
Split Window Vertically (same profile) + D
Split Window Horizontally (same profile) + Shift + D (mnemonic: shift is a wide horizontal key)
Moving
Move a pane with the mouse + Alt + Shift and then drag the pane from anywhere
Fullscreen
Fullscreen + Enter
Maximize a pane + Shift + Enter (use with fullscreen to temp fullscreen a pane!)
Resize Pane Ctrl + + Arrow (given you haven't mapped this to something else)
Less Often Used By Me
Go to Split Pane by Order of Use + ] , + [
Split Window Horizontally (new profile) Option + + H
Split Window Vertically (new profile) Option + + V
Previous Tab + Left Arrow (I usually move by tab number like ⌘+1)
Next Tab + Right Arrow
Go to Window + Option + Number

My Favorite Shell Key Combos

These might be helpful to getting you faster with the shell. These are just common shell shortcuts unrelated to iTerm itelf. These will usually work in Bash/Zsh/Fish on Mac and on Linux. There are many shortcuts out there but I use these quite a bit. There is also more than one way to do a thing so adopt what you like best.

Hopefully some of these improve your work life. :)

Function Key Combination Use
Delete to start of line Ctrl + U Use this to start over typing without hitting Ctrl-C
Delete to end of line Ctrl + K Use this with command history to repeat commands and changing one thing at the end!
Repeat last command Up Arrow Cycle and browse your history with up and down. Ctrl-R is faster if you know the string you are looking for.
Move back and forth on a line Left/Right Arrow Keys This takes you off the home row but it's easy to remember
Move back and forth on a line by words + Left/Right Arrow Keys Fast way to jump by words to correct a typo or "run again" with minor changes to last command. Ctrl as modifier might also work on mac and non-mac keyboards/shells/apps.
Delete previous word (in shell) Ctrl + W It's faster to delete by words. Especially when your last command was wrong by a single typo or something.
Clear screen Ctrl + L This is telling the shell to do it instead of an explicit command like clear or cls in DOS. If you use + K, this is telling iTerm to clear the screen which might have the same result or do something terrible (like when using a TUI like top or htop. In general, use this instead of typing clear over and over.
Exit Shell Ctrl + D Instead of typing exit, just get this in muscle memory. It works in many contexts as exit of end of file (EOF).

Moving Faster

A lot of shell shortcuts work in iterm and it's good to learn these because arrow keys, home/end keys and Mac equivalents don't always work. For example + Left Arrow is usually the same as Home (go to beginning of current line) but that doesn't work in the shell. Home works in many apps but it takes you away from the home row.

Function Shortcut
Move to the start of line Ctrl + A or Home (Home is fn+Left arrow)
Move to the end of line Ctrl + E or End (End is fn+Right arrow)
Moving by word on a line (this is a shell thing but passes through fine) Ctrl + Left/Right Arrow
Cursor Jump with Mouse (shell and vim - might depend on config) Option + Left Click

About keyboard shortcuts 💡

So, some keyboard shortcuts are Mac's. For example fn+Left Arrow is the Home key. On a fullsize Mac keyboard, there is a Home key. Home will usually pass through to iTerm and the shell. By shell, I mean zsh, bash or fish. The shell is the program running inside of iTerm when you open iTerm. If you launch vim or something, zsh/bash/fish is "gone" because vim is running. So, it's complicated to explain when keys work and when they don't.

For example, Home will work in zsh. It will take you to the beginning of the line. If your cursor is at the end of "three" in this below example

one two three|

When you press Home (fn+Left Arrow) your cursor will be on one: |one So, in this way, Home works the same in "the shell" as it does in TextEdit.app or any basic text box on Mac. This is not the case if you start up vim or emacs. This is not iTerm's fault. This is just how Mac/Linux works. Just a head's up on that little detail.

Copy and Paste with iTerm without using the mouse

I don't use this feature too much. I instead just mouse select (which copies to the clipboard) and paste. There's no need to Copy to the clipboard if you have General > Selection > Copy to pasteboard on selection enabled.

Function Shortcut
Enter Copy Mode Shift + + C
Enter Character Selection Mode in Copy Mode Ctrl + V
Move cursor in Copy Mode HJKL vim motions or arrow keys
Copy text in Copy Mode Ctrl + K

Copy actions goes into the normal system clipboard which you can paste like normal.

Search the Command History

Some of these are not directly related to iTerm and are just "shell features". Like, if you open Terminal.app on Mac some of these still work because it's the shell and not iTerm. I'm including them anyway.

Function Shortcut
Search as you type Ctrl + R and type the search term; Repeat Ctrl + R to loop through result
Search the last remembered search term Ctrl + R twice
End the search at current history entry Ctrl + Y
Cancel the search and restore original line Ctrl + G

Misc

Function Shortcut
Clear the screen/pane (when Ctrl + L won't work) + K (I use this all the time)
Broadcast command to all panes in window (nice when needed!) + Alt + I (again to toggle)
Find Cursor + / or use a theme or cursor shape that is easy to see
@squarism
Copy link
Author

@jirikrepl Wow. That's a new one. I've never heard of this feature! Amazingly, it's working here like a regular undo. Wow. I'm pressing Ctrl + _ and it works in iTerm2 and Terminal.

Hmm. I don't see any Ctrl based key mappings in iTerm's profile. I was thinking maybe something is remapping. To test that it isn't the shell, maybe you could invoke bash or zsh from iTerm. There's a flag to start the shell without reading your dotfiles too IIRC. Maybe this would help isolate/troubleshoot.

@alanhg
Copy link

alanhg commented Jun 12, 2022

⌘ + Shift + arrow-up
⌘ + Shift + arrow-down
For navigating marks. If shell integration is enabled all commands are marked automatically, and you can quickly go to previous commands and their output.

I didn't understand that when I navigated to pre/next Mark, the cursor was still in the same position.

so what is the point of navigation?

@squarism
Copy link
Author

@alanhg I don't know what he meant by that. I use mcfly right now but have used other history searchers in the past. You configure it and then just hit ctrl-r and start typing. Also fish (or zsh) has a thing when you type a command and hit up arrow and it will filter by what you have typed already. So like sudo journalctl<up> would get you all the journalctl commands.

@Arbyrd33
Copy link

Arbyrd33 commented Jul 27, 2022

Is there any way to duplicate a tab into a new tab with a keyboard shortcut? I find myself having to ctrl + t to make a new tab, cycle back to the original tab, right click on the tab, and then duplicate it. Catch is that the original tab has to be color coordinated or I'll accidentally delete the duplicated tab. Additionally, you can't duplicate a tab in the right-click context menu unless it's actually in tab form, not window form, which is why I need to make an arbitrary tab in the first place, and at this point I wonder if it would just save more time to manually cd into my desired directory.

EDIT: I have found a workaround, sort of. In the settings, I discovered I can show tabs even when there's only one tab. This will make duplicating them a lot easier, however, I still wasn't able to figure out how shortcuts work. To the docs..

@normand1
Copy link

moving forward or backwards is not working for me, anyone know why that might be?

@SantaHub
Copy link

SantaHub commented Dec 1, 2022

I would love an option to run a command on all the terminal.

@squarism
Copy link
Author

squarism commented Dec 21, 2022

I would love an option to run a command on all the terminal.

@SantaHub

  1. Open "all your terminals" as split panes. For example. Cmd+D two times. Now you have 3 panes in one window.
  2. Type Cmd+Option+I to enable broadcast mode. Click ok at the warning.
  3. Type your command once to all panes.
  4. Type Cmd+Option+I to disable broadcast mode.

This is very handy for cheap mass server administration at times. 🌹

@ospatil
Copy link

ospatil commented Jan 20, 2023

Thanks @squarism! It's a such a handy list.
One bash key combination I find useful:
esc + # : comment out the current command and move to next line

@pdothash
Copy link

What does Cmd+R do?

@jackyyeh5111
Copy link

Fantastic! Extremely useful.

@rprimmer
Copy link

rprimmer commented Feb 9, 2024

Good stuff!

@Warkanlock
Copy link

What does Cmd+R do?

I sum up to this: Where's the Shift+Cmd+R documented?

@ralexrdz
Copy link

I'm very used to alt + . command in Linux, which pastes the last argument from previous command. Any alternative? So far I use !$

@squarism
Copy link
Author

@ralexrdz I had muscle memory in bash for !$ as last argument when I used bash (I still use bash), which will work in every terminal. So, I would use that. It's fast and probably pretty universal. It works in zsh too.

Lately, I use fish (and this is not an endorsement of fish necessarily) and the equivalent in fish is alt + up arrow. 🐟 These are shell things, not iTerm2 things, just to be clear. 😄

@dcorking
Copy link

@ralexrdz M-. works for me in iTerm2 (3.5) in bash 5.2 and zsh 5.9. Thank you for teaching me a new shortcut 😄 Perhaps you have overridden the default binding somehow (such as with set -o vi). It is yank-last-arg from GNU readline so you'll find it in bash emacs mode, but not in many other shells.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment