0

In my .emacs file I have also set ctrl-o to delete a word:

(defun backward-delete-word (arg)
  "Delete characters backward until encountering the beginning of a word.
With argument ARG, do this that many times."
  (interactive "p")
  (delete-region (point) (progn (backward-word arg) (point))))

(global-set-key "\C-o" 'backward-delete-word)

Please note that this works. I want to have same setup on my Terminal shell. By default \C-w does it (link).


So I created following key binding (\C-o) in iTerm2 in order to delete a word by folliwing this solution, which works on Terminal shell.

Delete a word - Send escape sequence - 0x1b 0x08 or 0x17

enter image description here

But when I enable it, my keybinding for \C-o in emacs start to not work. iTerm's keybinding supress emacs's, even though they do the same job.

[Q] How can I use link emacs and iTerm for the same keybinding? I think I have to do a incode-decode-map (https://stackoverflow.com/q/36613839/2402577) but I wasn't able to do it.

  • Take Emacs out of the equation. Why are you using iTerm to implement "delete word"? What problem are you trying to solve? – nega Mar 20 at 6:08
  • Emacs is affected due to configuration on the iTerm. – alper Mar 20 at 8:26
  • Yes. @drew is correct. You have a conflict. But why are you configuring iTerm to “delete word”? – nega Mar 20 at 13:22
  • It is not important actually but I use a lot of bash scripts on iTerm, so I get use to emacs's keybinding. When I switch to iTerm from emacs, automatically I press \C-o to delete a word, which is actually enter. – alper Mar 20 at 14:02
  • Thanks, I'm trying to formulate some suggestions for you. One more question, why \C-o? – nega Mar 20 at 14:34
1

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-o4, 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.

| improve this answer | |
  • 1
    That got much longer than I intended. While general questions about binding keys in your shell are better asked at Unix.SE, there's plenty of info at your fingertips. For zsh look at the manpage for zshbuiltins and search for "bindkey". For bash look at the bash manpage, and search for "bind" or "editing-mode" or "emacs". – nega Mar 20 at 17:31
  • In bash C-w is bound to backward-kill-word where in emacs its bound to yank(cut) the selected work, so I couldn't bind C-w for delete words in emacs. That's why since I spend more time in emacs rather than bash, I learn C-w as to use as cut selected words. This was the starting point of this struggle. – alper Mar 21 at 10:25
  • I have done: bindkey -r "^O" && bindkey "^O" backward-kill-word and fixed the issue. link: unix.stackexchange.com/a/285210/198423 – alper May 4 at 11:30
1

I'm not familiar with iTerm. But it sounds like it is capturing the keys you specified, which you want, in general.

You just don't want it to do that when Emacs has the input focus. You'll probably need to check the iTerm doc to see if there is some way to exclude certain applications (e.g. Emacs) from iTerm's key capturing.

| improve this answer | |
  • I was thinking like that but iTerm does not provide any feature like that – alper Mar 13 at 20:58
  • I only find following sectoin Testing escape codes under their doc (emacswiki.org/emacs/iTerm2) – alper Mar 14 at 9:06
  • I have did bindkey -r "^O" && bindkey "^O" backward-kill-word and fixed the issue – alper May 4 at 11:29
0

The actual solution was pretty simple; I added following lines into .zshrc or .bashrc.

bindkey -r "^O" 
bindkey "^O" backward-kill-word

Link: https://unix.stackexchange.com/questions/285208/how-to-remove-a-zsh-keybinding-if-i-dont-know-what-it-does/285210#285210

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.