Delete (forward) character iTerm2 OSX - macos

I am using iTerm2 on OSX, and I am using an external keyboard with a Del (not to be confused with Backspace). The Del key works fine in OSX but in Terminal and iTerm, it inserts "~" instead of deleting forwards one character. This is easily fixable in Terminal, but in iTerm, there is no key binding or escape sequence that allows me to delete a character to the right of the cursor (the equivalent Unix command is Ctrl D). Is there any way I can simulate deleting forwards one character in iTerm?

If you are using zsh, try adding these lines to your ~/.zshrc file, then close and reopen the terminal:
bindkey "^[[3~" delete-char
bindkey "^[3;5~" delete-char
These should enable your delete key on any keyboard, without manual keyboard mappings to the hex command 0x04, which has the problem that it closes the session if the input is empty.
Why this? Because it seems that zsh doesn't read /etc/inputrc with the key bindings by default.

Like Gajus said, using Ctrl-D to delete one backspace forward also closes the terminal tab if there's no input which can be very frustrating.
I am using zsh, but adding the lines ricpacca suggested to .zshrc didn't work for me.
What I did instead is to map Ctrl-Delete to send the hex codes "0x06 0x08".
0x06 translates to "move one character forward", and 0x08 is just a normal backspace.
Thought I'd share my solution for any other frustrated iTerm2 users that scoured the internet to no avail.

You can add a iTerm Keyboard Mapping in Global Preferences or a Profile-based one for the key to send a Ctrl-D (0x04)
i.e: Here I am mapping Ctrl-Delete to Erase (Ctrl-D/0x04) like an Extended Keyboard's Del key:

Related

Shift + Backspace won't work on Vim for Windows

Before you mark this question as a double, hear me out because I've looked around and I can't find anything that'll fix my issue.
In Vim for Windows (accessed through Powershell running from CMD in a console), backspace works fine. Pressing Shift (or Ctrl) + Backspace prints a weird looking I to the screen and presses Ctrl+C.
Using :set to see what the keycode for the backspace is on my Vim yields this: "Î^Cx". This makes sense considering the behavior described above. The interesting part is that this keycode obviously is for the normal backspace key. My problem however, doesn't appear when pressing backspace normally, which doesn't make sense to me considering that the normal keycode is this weird collection of characters. It only appears when combining backspace with the shift key.
I don't understand why the backspace key doesn't send ^H or ^?. I've heard that those two options are popular. I assume my terminal is messing this up in some way, but I don't understand why it would be sending a different keycode for backspace when the shift key is pressed.
Thanks in advance.
I solved this issue like this:
imap Î^Cy <BS>
In my (Windows) terminals, Î^Cy is shift-backspace and Î^Cz is ctrl-backspace, and I have remapped them both. If Î^Cx is shift-backspace in your terminal, which it seems like it is, try adding this to your vimrc file:
imap Î^Cx <BS>
I don't know why this happened in your configuration, but you should be able to use inoremap to make it do what you want.
Interactively, you can test my idea by typing this
:inoremap <hit control-V><hit shift-backspace> <hit control-v><hit backspace>
What this will do is create a non-recursive map in insert mode for the escape sequence generated by <shift-backspace> to the escape sequence generated by <backspace>.
In my case, the result looked like
:inoremap ^? ^?
because my <shift-backspace> produces the same sequence as <backspace>, but in your case it should produce something more useful for you.
Once you've figured this out, move it to your .vimrc without the colon, and that will hopefully fix your issue in a lasting way.
Are you having the same issue as me I wonder where this happens in Windows Powershell but doesn't happen in gvim or when you run vim under cmd. If so I think the other responders are correct that vim does nothing different with shift+BS than BS but the Powershell command running it is. It's not an answer, I don't know a way around it other than use gvim. But if you're trying to dig further I'd suggest it's Windows Powershell that's the issue (unless I'm wrong of course) rather than vim.

How to set up Iterm / vim so that vim can recognize alt key mapping?

I have read a couple of different solutions to get the alt modifiers working on mac/iterm in vim.
The first solution I see posted online is to map the special characters produced by the alt key within .vimrc. This work-around almost works but I encountered a problem with alt-e. On macos catalina, and USA keyboard, when I type alt-e it produces a ´ character and then waits to for more input. If I then type a j for example, the ´ turns into j́. This waiting for more input is problematic for mapping alt-e in vim.
The other solution I see online is to configure iterm to send the Esc+ codes with alt key presses. As it turns out, this Esc code is the same code sent to vim when you press Esc, which means mapping alt characters would have the unwanted effects such as literally pressing Esc + the character to trigger the same keybind.
How could I get this to work?

iTerm2: delete line?

I'm trying to map ⌘+Delete (backspace) to delete to the beginning of the line (like it works in browsers and text editors) in iTerm2 and I'm unable to find a working escape code for it. I tried 1K (^[1K) based on what I read in Wikipedia. It just prints a "K".
Edit: I found Ctrl+U. Now to find out how to map it. Maybe Hex code 21 (U being 21st letter), so 0x15?
I got it. I have no idea why Hex Code mappings in iTerm2 produce the associated Ctrl+key mappings, but they do. No idea what 0x00 means, either, as it's not assigned to A as might be expected. (though I do believe Unix has its own conventions relating to treating null bytes -- we have e.g. xargs accepting a null byte delimiting format from find for example -- It would be neat if we can bind this to a hotkey with iTerm2)
I was able to find that Ctrl+U does nearly the exact task I want (it deletes the entire line rather than deleting only what is before cursor, but whatever... Ctrl+Y as a bonus can bring it all back). Then I curiously saw that I had hex codes 0x1 and 0x5 mapped to ^A and ^E respectively, for my Cmd+Left and Cmd+Right... so 0x15 is for ^U!
For Mac OS, most editor share the common shortcut ⌘ + Delete: delete to start of the line, in iTerm2 we can switch to this key configuration
Mapping hex code 0x15 to ⌘ + ←Delete in most shells deletes the entire line (content to the left and right of the cursor). While sometimes not as compatible, I find that mapping:
⌘+←Delete to Send Hex Codes:
0x18 0x7f
performs the desired functionality. If you're running ZSH, you'll likely also need to add this to your .zshrc file:
$ echo 'bindkey "^X\\x7f" backward-kill-line' >> ~/.zshrc
as by default ZSH doesn't map backward-kill-line to anything.
Furthermore, you can also delete everything to the right of your cursor by mapping:
⌘+fn+←Delete or ⌘+Delete→ to Send Hex Codes:
0x0b
I wrote a comprehensive guide to adding most of OSX's standard keybinding to your terminal here
In OSX, ⌥+⌫ and ⌘+⌫ are the shortcuts for deleting a word and deleting a line respectively. ⌘+ ← and ⌘+ → are for going to the beginning and end of lines. By default, iTerm2 isn't configured this way, and there are a lot of misleading guides online. The following is what I've found to work on my machine.
Open the preferences (⌘+,) and go to the Keys tab.
Add a global shortcut key, and just type in your shortcut
In the Action dropdown, select Send Hex Code
The hex codes for...
Deleting a word: 0x17.
Deleting a line: 0x15.
Moving to the beginning of the line: 0x01.
Moving to the end of the line: 0x05.
Just open a new tab, and it should work!
Here is a screenshot, for clarity:
I hope this may help you
map ⌥ <- Delete to Send Hex Codes: 0x1B 0x08
I had test for it, and it is correct.
18.09.2013 update
this delete one word, not a line.
As pointed ^U deletes the line. You can easily remap the command by using Better Touch Tool.
It also has cool features for automation, mouse, pad and keyboard mapping. Also includes a window feature for smart borders.
On iTerm2 you can set the exact same shortcuts of your OS. In this case, as the default shortcut to delete a line on Mac OS is ⌘+Delete (backspace), you can do so.
To set the default keybindings of iTerm you have to:
Go to Preferences (or ⌘+,)
Profiles
Keys
Key Mappings
Presets...
Select the "Natural text editing" option
The location has changed. In order to enable natural editing go to:
Preferences -> Profile -> Keys -> Load Presets... -> and select Natural Text Editing
For more info and tweeks go https://blog.arturofm.com/install-iterm-terminal-emulator-on-macos/

bindkey with zsh on Mac

I've been trying to bind keys in zzh on a Mac but am having some trouble. I know I should be Googling this but couldn't find what I need...
I've been trying to bind ctrl-backspace to be an equivalent of ctrl-w, ie delete one word. But I can't find the key code for it. Nothing shows up when I do it with 'read'. I can't find a chart online either.
Also, I couldn't bind home/end keys (ie fn-left, fn-right) somehow. It is not bound in Terminal's keyboard assignments or .zshrc but it's still doing weird stuff when I press it. Page up and down seem to behave less strangely. Any help?
Thanks
Open a shell and try pressing Ctrl-v and then the key you want to bind (eg. Home)
This is the key sequence you should use with bindkey.
My bindings for Home/End (start/end of line) on Snow Leopard:
bindkey '^[[H' beginning-of-line
bindkey '^[[F' end-of-line
Still trying to bind cmd-left though ...

How to move the cursor word by word in the OS X Terminal

I know the combination Ctrl+A to jump to the beginning of the current command, and Ctrl+E to jump to the end.
But is there any way to jump word by word, like Alt+←/→ in Cocoa applications does?
Out of the box you can use the quite bizarre Esc+F to move to the beginning of the next word and Esc+B to move to the beginning of the current word.
On macOS (all versions) the following keyboard shortcuts work by default.
ALT+F to jump Forward by a word.
ALT+B to jump Backward by a word.
Note that you have to make set the Option key to act like the Meta key. You can do this in Terminal by accessing preferences (CMD+,) and selecting Profiles -> Keyboard. In iTerm2 Pselect rofiles -> Keys -> General and select "Option key as Esc+."
Additionally some Emacs-style key bindings for simple text navigation seem to work on bash shells. You can use:
CTRL+F to move forward by a char
CTRL+B to move backward by a char
CTRL+A to jump to start of the line
CTRL+E to jump to end of the line
CTRL+K to kill the line starting from the cursor position
ALT+D to delete a word starting from the current cursor position
CTRL+W to remove the word backwards from cursor position
CTRL+Y to paste text from the kill buffer
CTRL+R to reverse search for commands you typed in the past from your history.
CTRL+S to forward search (works in ZSH for me but not bash)
Here's how you can do it
By default, the Terminal has these shortcuts to move (left and right) word-by-word:
esc+B (left)
esc+F (right)
You can configure alt+← and → to generate those sequences for you:
Open Terminal preferences (cmd+,);
At Settings tab, select Keyboard and double-click ⌥ ← if it's there, or add it if it's not.
Set the modifier as desired, and type the shortcut key in the box: esc+B, generating the text \033b (you can't type this text manually).
Repeat for word-right (esc+F becomes \033f)
Alternatively, you can refer to this blog post over at textmate:
http://blog.macromates.com/2006/word-movement-in-terminal/
Switch to iTerm2. It's free and much nicer than plain old terminal. Also it has a lot more options for customization, like keyboard shortcuts.
Also I love that you can use cmd and 1-9 to switch between tabs. Try it and you will never go back to regular terminal :)
How to set up custom keyboard preferences in iterm2
Install iTerm2
Launch and then go to preference pane.
Choose the keyboard profiles tab
You will either need to copy the profile to something new and then delete the arrow key shortcuts such as ^+ Right/Left or if you don't care about a backup just delete them from the default profile.
Next make sure your modified profile is selected (starred)
Now choose the keyboard tab (very top row)
Click on the plus button to add a new keyboard shortcut
In the first box type CMD+Left arrow
In the second box choose "send escape code"
In the third box type the letter B
Repeat with desired key combinations. escape+B moves one word to the left, escape+f moves one word to the right.
you may also wish to set up cmd+d to delete the word in front of the cursor with escape+d
I often hit the wrong button (cmd / control / alt) with an arrow key and so i have my arrow key combinations with those buttons all set to jump forward and back words, but please do what fits you best.
Actually there is a much better approach. Hold option ( alt on some keyboards) and press the arrow keys left or right to move by word. Simple as that.
option←
option→
Also ctrle will take you to the end of the line and ctrla will take you to the start.
I have Alt+←/→ working: open Preferences » Settings » Keyboard, set the entry for option cursor left to send string to shell: \033b, and set option cursor right to send string to shell: \033f. You can also use this for other Control key combinations.
Use Natural Text Editing preset!
Essentially it binds, among other key sequences, Option + LeftArrow to ^[b sequence and Option + RightArrow to ^[f
This works in fish and bash, as well as in psql terminal.
Actually it depends on what shell you use, however most shells have similar bindings. The bindings you are referring to (e.g. Ctrl+A and Ctrl+E) are bindings you will find in many other programs and they are used for ages, BTW also work in most UI apps.
Here's a look of default bindings for Bash:
Most Important Bash Keyboard Shortcuts
Please also note that you can customize them. You need to create a file, name as you wish, I named mine .bash_key_bindings and put it into my home directory. There you can set some general bash options and you can also set key bindings. To make sure they are applied, you need to modify a file named ".bashrc" that bash reads in upon start-up (you must create it, if it does not exist) and make the following call there:
bind -f ~/.bash_key_bindings
~ means home directory in bash, as stated above, you can name the file as you like and also place it where you like as long as you feed the right path+name to bind.
Let me show you some excerpts of my .bash_key_bindings file:
set meta-flag on
set input-meta on
set output-meta on
set convert-meta off
set show-all-if-ambiguous on
set bell-style none
set print-completions-horizontally off
These just set a couple of options (e.g. disable the bell; this can be all looked up on the bash webpage).
"A": self-insert
"B": self-insert
"C": self-insert
"D": self-insert
"E": self-insert
"F": self-insert
"G": self-insert
"H": self-insert
"I": self-insert
"J": self-insert
These make sure that the characters alone just do nothing but making sure the character is "typed" (they insert themselves on the shell).
"\C-dW": kill-word
"\C-dL": kill-line
"\C-dw": backward-kill-word
"\C-dl": backward-kill-line
"\C-da": kill-line
This is quite interesting. If I hit Ctrl+D alone (I selected d for delete), nothing happens. But if I then type a lower case w, the word to the left of the cursor is deleted. If I type an upper case, however, the word to the right of the cursor is killed. Same goes for l and L regarding the whole line starting from the cursor. If I type an "a", the whole line is actually deleted (everything before and after the cursor).
I placed jumping one word forward on Ctrl+F and one word backward on Ctrl+B
"\C-f": forward-word
"\C-b": backward-word
As you can see, you can make a shortcut, that leads to an action immediately, or you can make one, that just inits a character sequence and then you have to type one (or more) characters to cause an action to take place as shown in the example further above.
So if you are not happy with the default bindings, feel free to customize them as you like. Here's a link to the bash manual for more information.
Hold down the Option key and click where you'd like the cursor to move
If you happen to be a Vim user, you could try bash's vim mode. Run this or put it in your ~/.bashrc file:
set -o vi
By default you're in insert mode; hit escape and you can move around just like you can in normal-mode Vim, so movement by word is w or b, and the usual movement keys also work.
If you check Use option as meta key in the keyboard tab of the preferences, then the default emacs style commands for forward- and backward-word and ⌥F (Alt+F) and ⌥B (Alt+B) respectively.
I'd recommend reading From Bash to Z-Shell. If you want to increase your bash/zsh prowess!
As of Mac OS X Lion 10.7, Terminal maps Option-Left/Right Arrow to Esc-b/f by default, so this is now built-in for bash and other programs that use these emacs-compatible keybindings.
In Bash, these are bound to Esc-B and Esc-F.
Bash has many, many more keyboard shortcuts; have a look at the output of bind -p to see what they are.
Under iterm2's Preferences > Profile > Keys, you click the + below Key Mappings and record a new shortcut. For Action, select Send Escape Sequence and type b or f for backwards and forwards respectively.
When I tried to record one for (Ctrl+←), I noticed in the Keyboard Shortcut field that the arrow never showed up. Turns out I had to disable the default mac's System Preferences > Keyboard > Shortcuts > Mission Control shorcuts first to get things to work, as they'll override iterm2's default shortcuts. Should be true for the standard terminal app, too.
For some reason, my terminal's option+arrow weren't working. To fix this on macOS 10.15.6, I opened the terminal app's preferences, and had to set the bindings.
Option-left = \033b
Option-right = \033e
For some reason, the option-right I had was set up to be \033f. Now that it's fixed, I can freely skip around words in the termianl again.
Here's the CLI way to do so, verified it works on bash.
Add the following to your ~/.inputrc:
# macOS Option + Left/Right arrow keys to move the cursor wordwise
"\e\e[C": forward-word
"\e\e[D": backward-word
The advantage of this method is that it is terminal application agnostic - doesn't matter whether you use Terminal.app, iTerm2, or any other application.
Inspiration got from this other answer.
New answer for iTerm2 Build 3.3.4 users:
Step 1: (macOS X) System Preferences > Keyboard > Shortcuts tab > Select Mission Control (left panel) > Uncheck shortcuts that labeled as "Move left a space" and "Move right a space"
Step 2: (iTerm2 Build 3.3.4) Preferences > Profiles > Select * Default (left panel) > Keys tab > Delete both "⌥->" and "⌥<-" entries > Set both "Left Option (⌥) Key:" and "Right Option (⌥) Key:" to Esc+
No messing around with shell profiles, no messing around with inferior masOS (default) Terminal, no awkwards Esc+F/B, rinse & repeat non-sense.
Done deal!!!
Enjoy this tip, my fellow PROGRAMMERS!
As answered previously, you can add set -o vi in your ~/.bashrc to use vi/vim key bindings, or else you can add following part in .bashrc to move with Ctrl and arrow keys:
# bindings to move 1 word left/right with ctrl+left/right in terminal, just some apple stuff!
bind '"\e[5C": forward-word'
bind '"\e[5D": backward-word'
# bindings to move 1 word left/right with ctrl+left/right in iTerm2, just some apple stuff!
bind '"\e[1;5C": forward-word'
bind '"\e[1;5D": backward-word'
To start effect of these lines of code, either source ~/.bashrc or start a new terminal session.
Just check the "Use Option as meta key" option in Terminal > Preferences > Settings > [profile] > Keyboard, as mentioned here already by #cris-page.
Note however, that in macOS Catalina (10.15) and newer, zsh becomes the default shell for newly added users: its default configuration considers only whitespaces as word-boundaries, whereas the old bash makes meta-left/right jump to the nearest non-alphanumerical character (similar to B/W as opposed to b/w for those familiar with vim):
v----v- bash jumps here
$ vim some-folder/what.txt_<- jump left twice from here
^---^- zsh jumps here by default
(similar motions are true for meta-backspace as well)
There are more than one ways to make zsh command line editor navigation work similarly to bash's - here is one such method:
# Place in your profile init script, e.g. `~/.zshrc`
autoload -U select-word-style
select-word-style bash

Resources