What is the representation of the mac command key in the terminal? - macos

Like control key is represented by a '^' in the terminal, what is the equivalent for the command key (mac)?
I am trying to remap my bash shortcuts using stty
For eg
stty eof ^D
But instead of control, I want to use the command key.
EDIT:
Okay so the issue I was trying to solve was that I wanted to interchange command and control keys because I work on osx and linux and the different key combinations cause me a lot of pain.
So I interchanged the modifier keys using osx preferences. But now all the bash shortcuts like Ctrl+C etc had become equivalent of using the key sequences 'cmd+c' - which is not acceptable.
Thankfully iTerm2, supports remapping of modifier keys as well, so for iterm2 I reversed them again which means iTerm2 recognizes command as command and control as control.
So problem solved for now.

The command-key shortcuts do not generate actual input for your terminal, so they are not represented in any way. Terminal allows you to bind certain key combinations to produce actual input (in Preferences > Settings > Keybaord), but you don't get the choice of a Command modifier for them.

Type this in your bash shell :
stty ctlecho
then hit Command
That will display what you need.
To go back to normal
stty -ctlecho
If it doesn't work, try a combo.
Example with Ctrl+C
$ stty ctlecho
$ ^C
$ stty -ctlecho
$

Related

how do you enable command line editing with vim keys using mac terminal?

I'm reading this book and I really do not understand what this author is talking about. It appears that you can program your mac to go forward one character by either hitting l or ^F. I do not understand the difference between emac key stroke and vim keystroke. He also says run this command and 'place it in your $HOME/.bash_profile but I cannot figure out how to place the command in the bas_profile.
The Author is talking about make you terminal console behaves like vi, this means typing set -o vi in your terminal, the console will work similar vi. So you will be able to navigate using the motion keys of vi, use INSERT mode, x to delete, etc.
You can set that permanently if you include this command in your ~/.bash_profile file.
If you are not sure what it does, I don't recommend so.

Searching your command history on macOS terminal

What is the shortcut to search my command history in macOS terminal?
For how long is the history available for searching? Where is it stored?
How about using Ctrl+R for searching on the Terminal Utility in Mac for searching on the command history,
dudeOnMac: freddy$ whoami
freddy
(reverse-i-search)`who': whoami
Well for controlling how long the history would be retained that depends on a few shell environment variables, HISTFILESIZE which is nothing but number of lines of history you want to retain. Set a huge value for it in .bash_profile for it to take effect
HISTFILESIZE=10000000
Use Ctrl + R for searching a command from history in Terminal.
(reverse-i-search)`':
Type any substring of the command you want to search e.g. grep
(reverse-i-search)`grep': grep "XYZ" abc.txt
It will return the latest command that matches your input. If that is not the command you were searching for, keep pressing Ctrl + R for next match until you find your command.
Once you found your command press Return to execute it.
If you want to exit without running any command, press Ctrl + G
PS: This answer is same as suggested by Inian, just giving more details for easy usage.
The command history is stored under your home folder in a hidden file called .bash_history. To view it's content in nano, use the following command in Terminal:
nano ~/.bash_history
Or open with your text editor (default is TextEdit):
open ~/.bash_history
In my case it's a very long list and as I scroll through seems like the last ~500 command is stored here.
Migrating an answer to SO from this answer on the Unix and Linux Stack Exchange:
Pressing ctrl+R will open the history-search-backward. Now start typing your command, this will give the first match. By pressing ctrl+R again (and again) you can cycle through the history.
If you like to be super lazy you can bind the up/down arrow keys to perform this search, I have the following in my .inputrc to bind the up/down arrow key to history-search-backward and history-search-forward:
# Key bindings, up/down arrow searches through history
"\e[A": history-search-backward
"\e[B": history-search-forward
"\eOA": history-search-backward
"\eOB": history-search-forward
Just type something (optional), then press up/down arrow key to search through history for commands that begin with what you typed.
To do this in .bashrc rather than .inputrc, you can use:
bind '"\e[A": history-search-backward'
Use this command -
history
This works on both OSX and Linux.
History is stored in ~/.zsh_history or ~/.bash_history or ~/.history depending on your shell.
History is stored for 1000 or 2000 lines depending on your system.
echo $HISTSIZE
You can also try the following:
history | grep 'git'
Where 'git' is the command you are looking for.
For those who want to search specific command from history, you can do so with reverse-i-search. Reverse search allow you to type in any key words(any) that is part of the command you are looking for and reverse search navigate back to history, match previous commands incrementally and return the entire command.
It is especially useful as when one cannot remember all handy lengthy commands they use often. To do reverse-search ctrl + R and type any clue you have and that will return your previous commands matching the words you type. Then once found the command, hit Enter to execute it directly from search.
Automation AppleScript
Since you mentioned viewing your history as a quick solution, via the Terminal.app. You might want to automate, or quickly view history, maybe from the dock. You may use the AppleScript application as one alternative. This is an optional approach to create a simple shortcut, as to many others.
Open the AppleScript editor application.
Add your specified commands, for history.
Code
tell application "Terminal"
do script "history"
end tell
Save as application, drag to dock for convenience.
History Storage & Time Stored Details
HISTSIZE Determines how many lines will be written to the history file.
HISTFILESIZE Determines how long the file.
Find out how long history is stored:
echo $HISTSIZE $HISTFILESIZE
Note: You may also increase your command history storage size in the length of two variables. You may achieve this through HISTSIZE and HISTFILESIZE environment variables which are located in your ~/.bash_profile file.
It is possible to achieve this by modifying ~/.bash_profile, the number placeholder with SIZE represent's the number, lines value as example:
export HISTFILESIZE=SIZE # Example 1000
export HISTSIZE=SIZE # Example 10000
Pre macOS 11 Big Sur
cat ~/.bash_history
HISTFILESIZE will only set a maximum history value which is stored to the history file when a session is started. HISTSIZE will determine specifically how many lines will be stored or in other words, written at the end of the session. If the set HISTFILESIZE is determined to be a large value than what HISTSIZE is set, you will not view history larger than your set HISTSIZE. The reason is that the history file is overwritten with the HISTSIZE unless using histappend option turned ON.
You may use also histappend to append history, If the histappend shell option is turned on lines are appended to the history file. Otherwise, the overwritten alternative proceeds.
Bash GNU - histappend
macOS 11 Big Sur
nano ~/.zprofile
Modify history environment variables, set to a value:
export HISTFILESIZE=1000
export HISTSIZE=SIZE=1000
Run the source command can be used to load any functions file into the current shell script or a command prompt.
source ~/.zprofile
echo $HISTSIZE $HISTFILESIZE
Outputs:
1000 1000
Output where some history is stored:
cat ~/.zsh_history
For macOS Big Sur the file is now .zsh_history
If you do vi ~/.zsh_history in the terminal you can use regex by pressing the / and then the search term.
To review or recall recently used commands, you can just press the up arrow key to sequentially read back through the history stored in .bash_history.
To search through history with ease, I advise you to install fzf.
It's an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.
Just install it, click ctrl + R, and you'll be to scroll through you shell history, without the need to grep or waiting ages until the command you're waiting for pops up.
It supports Mac OS, Linux and even Windows.
# USAGE: find.history cd
# filter commands in shell history by a search term and execute the selected command
function find.history {
eval $(history | grep "$1" | tail | awk '{$1=""}1' | tail -r | peco)
}
You will need to have peco installed.
https://github.com/peco/peco
[$]> brew install peco

How to map mac command key in vim?

I want to map my NERDTREE toggle to Cmd + space but I don't know what character represents Mac's command key in .vimrc. I searched and found that we can use 'D' to represent cmd key from this link, but it doesn't work.
BTW I am using vim from the terminal not MacVim.
iTerm2 can do this - here I mapped command enter to :wq
It is true that <D- maps to command key. You can see it by :help <D-.
However, this will not work in your vim+terminal. It only works in MacVim. See here
The best shot for your intention is to map the combination as hex code, sending to terminal.
Still I will not recommend doing this. command is too important for mac os environment. For example, anyone uses an input method can't map it to command+space.\
Please consider using the usual method here: <leader>.
With IdeaVim you can use: <M-*>, for example <M-c> means Cmd + c
An example of mapping CMD+p to vim's Meta P to call :CtrlP command.
This has the advantage that Meta is usually not used for anything so it won't clash with existing commands.
And on .vimrc you'd add
map <M-p> :CtrlP<CR>

tmux is not using screen-256color even it is set in the config file

I am trying to get tmux to use screen-256color instead of xterm-256color, as it is not recommended. But when I am not using tmux, I would like to keep it as xterm-256color
A little bit of my setup, I am currently using iTerm2 and ssh to my development linux box, which is using zsh.
In my ~/.zshrc, I have:
export TERM="xterm-256color"
In my ~/.tmux.conf, I have:
set -g default-terminal "screen-256color"
With this configuraiton, without tmux, echo $TERM returns xterm-256color (which is right) and with tmux, echo $TERM is still returning xterm-256color instead of screen-256color.
Is there anything else I need to set in order for this to work?
Thanks!
The reason this does not work as expected is, that the zsh session that is started inside tmux overwrites TERM.
You should not set TERM from within in your shell. TERM is the way the terminal informs the shell and other applications about its capabilities (number of colors, key sequences for special keys, etc.). If you change TERM inside the shell, you change the what features the shell and applications expect from the terminal without the terminal itself knowing about it. Oftentimes this may not be an actual issue, but it is better to change the terminal configuration and set the desired value there.
You already did so when setting screen-256color in the configuration of tmux, which is essentially a terminal emulator, too. To do it for iTerm2 (tested with version 3.0.10):
Open the Preferences dialog (in the Menu: iTerm2 → Preferences, or press ⌘+,).
In the dialog go to Profiles → Terminal.
There you can choose the desired value for TERM under Report Terminal Type.
You could modify your .zshrc file to check if you are running in tmux:
[ -z "$TMUX" ] && export TERM=xterm-256color

Using keyboard to navigate the OS X terminal scrollback buffer

I hate using the mouse. When working in the OS X terminal, sometimes I want to navigate to a line in the bash shell a few rows up, copy a word or two. For this I always end up using the mouse. Any solution for this? Perhaps the terminal supports a key combination that puts it in to navigation/select/copy mode, where I can use the usual C-F, C-B, C-N and C-P keys.
If you feel comfortable with Vi key combinations, you can use this command to switch to vi-mode and use key strokes:
$ set -o vi
Or use C-xC-e to open current line in your $EDITOR.

Resources