tl;dr
@drew is right, you have a conflict. Use C-backspace
in Emacs to delete the word to the left of the point. At your shell command like use C-w
to do the same.
Long answer
@Drew is correct, you have a conflict between iTerm and Emacs. Unfortunately there's not much you can do about that, so let's focus on what you want to do, "delete word".
As you've discovered, Emacs has a "delete word" function, backward-kill-word
. This will delete the word1 to the left of the point. By default backward-kill-word
is bound to C-backspace
.
Now, when you have iTerm open in front of you, and you're at your shell command line, you can also "delete word". By default this is bound to C-w
, in both bash
and zsh
.2 Generally both bash
and zsh
have "emacs keybindngs" by default, so general movement and editing use the same keys. "Delete word" is one place where they differ.
Like in Emacs, you can rebind keys in your shell, but unifying your keybindings so you have a common "delete word" key in your shell and in Emacs isn't that simple. In Emacs, rebinding backward-kill-word
to C-w
will step on the default binding of kill-region
, which I don't recommend. Binding C-backspace
in your shell isn't that simple, or simple of a conflict. Generally, in your shell, when you press the backspace key, the shell does not receive a "backspace event" like Emacs does. The shell receives C-?
3 and this is bound to backward-delete-character
. This is also something you don't want to overwrite for obvious reasons.
Recommendations
While you could bind "delete word" to a single unused key in Emacs and your shell, say C-o
4, I strongly recommend you don't. The same goes for any of the basic movement and editing keys. (up, down, left, right, forward-word, backward-word, kill-word, kill-backward-word, end-of-line, beginning-of-line, etc.) You'll always be able to sit at a "unix" computer and get things done. There's nothing like sitting at a customer's system, and they're quite literally breathing down your neck to fix something "now!", and you choke because your "weird" keybindings and aliases are missing. (Pro tip.)
Obviously if you have a real need to rebind keys, do it. But learn the defaults too.
--
[1]: The Emacs definition of a "word" depends on the syntax table active in the buffer you're visiting. For the sake of discussion we'll just assume a "word" is a group of letters and numbers by characters that are not letters or numbers.
[2]: In bash
C-w
is actually bound to unix-word-rubout
and can be described as "delete the previous group of letters and numbers bounded by whitespace" and is closer to zsh
's backward-kill-word
. bash
's backward-kill-word
and can be described as "delete the previous group of letters and numbers bounded by charactars that are not letters or numbers" and is bound to Escape-Control-Backspace
.
[3]: Your shell could receive C-h
which is also bound to backward-kill-character
. It's something you might see on other older "unix" systems. In our case, macOS + iTerm + bash/zsh, it's going to be C-?
.
[4]: C-o
is actually bound in bash
and zsh
, but you could get away with overwriting it.