Search code examples
emacscopy-pastekey-bindings

How do I make C-w behave the same as bash?


In bash when I press C-w it kills the word before the cursor. In emacs, it yanks the region but when there is no active region it kills forward. So I have to press M- to (backward-kill-word) How do I make C-w behave like the bash shell? Do I have to make a function to check for active region and then decide to kill either (backwards-kill-word) or (kill-region) or is there another way?


Solution

  • From my defuns.el (probably ripped from elsewhere):

    (defun kill-region-or-backward-word ()
      "If the region is active and non-empty, call `kill-region'.
    Otherwise, call `backward-kill-word'."
      (interactive)
      (call-interactively
       (if (use-region-p) 'kill-region 'backward-kill-word)))
    

    Edit: tali713 on #emacs had this better version with call-interactively.