How to kill word

For example, my cursor (point) is at an arbitrary letter in the word «cursor». I want to delete (kill) that word such that it is copied to kill-ring.

JBentley's user avatar

JBentley

6,0405 gold badges36 silver badges71 bronze badges

asked Oct 30, 2015 at 17:49

Jianglong Chen's user avatar

Jianglong ChenJianglong Chen

5051 gold badge3 silver badges13 bronze badges

5

The Emacs way to remove the word one is inside of to press M-backspace followed by M-d. That will kill the word at point and save it to kill ring (as one unit).

If the cursor is at the beginning or after the end of the word, only one of the two is sufficient. An Emacs user will typically move between words using commands such as forward-word (M-f) and backward-word (M-b), so they will be at the word boundary to begin with and thus rarely need to kill the word from the inside.

answered Oct 31, 2015 at 10:45

user4815162342's user avatar

user4815162342user4815162342

138k18 gold badges287 silver badges345 bronze badges

5

You could use this as a framework for killing various kinds of things at point:

(defun my-kill-thing-at-point (thing)
  "Kill the `thing-at-point' for the specified kind of THING."
  (let ((bounds (bounds-of-thing-at-point thing)))
    (if bounds
        (kill-region (car bounds) (cdr bounds))
      (error "No %s at point" thing))))

(defun my-kill-word-at-point ()
  "Kill the word at point."
  (interactive)
  (my-kill-thing-at-point 'word))

(global-set-key (kbd "s-k w") 'my-kill-word-at-point)

answered Oct 31, 2015 at 10:41

phils's user avatar

2

You can do that by moving to the beginning of the word (if not standing there already) by M-b, then deleting it with M-d. You can then press C-y to put it back. If you want to automate it, you can create a short elisp function and assign it to a key:

(global-set-key [24 C-backspace] ; C-x C-backspace
                (lambda () (interactive)
                  (save-excursion
                    (backward-word)
                    (kill-word 1)
                    (yank))))

answered Oct 30, 2015 at 17:56

choroba's user avatar

chorobachoroba

228k25 gold badges207 silver badges283 bronze badges

2

How can I delete a word backward at the command line? I’m truly used to some editors deleting the last ‘word’ using Ctrl+Backspace, and I’d like that functionality at the command line too.

I am using Bash at the moment and although I could jump backward a word and then delete forward a word, I’d rather have this as a quick-key, or event as Ctrl+Backspace.

How can accomplish this?

Anthon's user avatar

Anthon

77.5k42 gold badges164 silver badges221 bronze badges

asked Oct 9, 2013 at 21:57

lucidquiet's user avatar

1

Ctrl+W is the standard «kill word» (aka werase).
Ctrl+U kills the whole line (kill).

You can change them with stty.

-bash-4.2$ stty -a
speed 38400 baud; 24 rows; 80 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
        -extproc -xcase
iflags: -istrip icrnl -inlcr -igncr -iuclc ixon -ixoff ixany imaxbel
        -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -ocrnl -onocr -onlret -olcuc oxtabs -onoeot
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
        min = 1; quit = ^; reprint = ^R; start = ^Q; status = <undef>;
        stop = ^S; susp = ^Z; time = 0; werase = ^W;
-bash-4.2$ stty werase ^p
-bash-4.2$ stty kill ^a
-bash-4.2$

Note that one does not have to put the actual control character on the line, stty understands putting ^ and then the character you would hit with control.

After doing this, if I hit Ctrl+P it will erase a word from the line. And if I hit Ctrl+A, it will erase the whole line.

AdminBee's user avatar

AdminBee

21.1k20 gold badges47 silver badges70 bronze badges

answered Oct 10, 2013 at 4:13

kurtm's user avatar

kurtmkurtm

6,7851 gold badge23 silver badges21 bronze badges

8

Alt+Backspace works for me in bash and zsh.

Anthon's user avatar

Anthon

77.5k42 gold badges164 silver badges221 bronze badges

answered Oct 9, 2013 at 22:06

Luis's user avatar

LuisLuis

2,3602 gold badges20 silver badges19 bronze badges

7

In Ubuntu 18.04.4 pressing Ctrl+V and then Ctrl+Backspace reveals that the Ctrl+Backspace combination is written as ^H.

You can show all current terminal key combinations using stty -a. In this case we want to change werase to ^H which can be done with

stty werase ^H

For any other key you want to remap you would again see how to write the new combination using Ctrl+V followed by the combination.

And you can find the command name (such as werase) by looking at stty -a and looking for the key combination an action is currently bound to.

AdminBee's user avatar

AdminBee

21.1k20 gold badges47 silver badges70 bronze badges

answered Aug 6, 2020 at 9:22

Kvothe's user avatar

KvotheKvothe

3534 silver badges13 bronze badges

3

On Mac, you can use:

Fn+Delete

Anthon's user avatar

Anthon

77.5k42 gold badges164 silver badges221 bronze badges

answered May 19, 2016 at 15:54

J.Yang's user avatar

2

Surprisingly, esc then delete works on macOS too.

Also, if using iTerm2, you can update the default profile:

Keys > Key Mapping > Presets > Natural Text Editing

answered Jul 16, 2022 at 17:32

skube's user avatar

skubeskube

1636 bronze badges

In Linux I’m used to press CtrlBackspace to delete the last word but I don’t know how to do it in Bash terminal.

In OS X I use AltBackspace to do the same thing.

Is there any way of making Bash recognize the Ctrl— or AltBackspace key combination to delete last word?

In Linux I use terminator as terminal emulator, in OSX I use iTerm2.

slhck's user avatar

slhck

220k69 gold badges596 silver badges585 bronze badges

asked Jun 13, 2011 at 16:43

licorna's user avatar

1

You can always use CtrlW. It deletes the word before the cursor and works in every Bash.

See here for a list of Bash keyboard shortcuts.

answered Jun 13, 2011 at 17:15

slhck's user avatar

slhckslhck

220k69 gold badges596 silver badges585 bronze badges

3

You just need to set Option as Meta key in iTerm’s preferences (Profiles » Keys).

backward-kill-word is bound to Meta-Rubout (i.e. Opt-Backspace) by default.

The same option exists (albeit less flexible) in Apple’s Terminal.app.

answered Jun 13, 2011 at 17:28

Daniel Beck's user avatar

Daniel BeckDaniel Beck

109k14 gold badges285 silver badges331 bronze badges

2

You can create a file .inputrc in your home directory and add this content inside:

"C-h": backward-kill-word

answered Apr 19, 2017 at 16:23

Sel's user avatar

SelSel

2531 silver badge6 bronze badges

1

In terminator you can use the following «plugin», along with

[keybindings]
    kill_word = <Control>BackSpace

in your config file. This will map CtrlBackspace to ESCDEL, another shorcut to delete last word.

answered Sep 17, 2013 at 7:56

OlivierBlanvillain's user avatar

2

As some have mentioned, this may also depend on your terminal emulator. For example in KDE’s Konsole, you can define the behavior of Ctrl+Backspace in the key bindings settings of the profile:

Set Backspace+Ctrl to the output Ex7f (default b)

Konsole Key Binding List

Dan Dascalescu's user avatar

answered Jan 7, 2021 at 21:30

quazgar's user avatar

quazgarquazgar

6256 silver badges9 bronze badges

You can just use AltDEL instead of the normal StrgDEL.

answered Sep 8, 2015 at 18:30

Luca Steeb's user avatar

In my C# application I use MailMeger in Word. I use Microsoft.Office.Interop.Word library.
And for Merge i use  method Execute of  MailMerge object.

But sometimes Winword hangs on method MailMerge.Execute
And then I want to kill this process WINWORD.EXE after 2 minutes of waiting
Now I do it so:

  using WinWord = Microsoft.Office.Interop.Word;

  public static void MergeExec(WinWord.MailMerge wrdMailMerge)
  {
     MergeExecChecker mergeChecker = new MergeExecChecker(wrdMailMerge.Application);

     System.Threading.TimerCallback timerDelegate = new System.Threading.TimerCallback(mergeChecker.TimeElapsedHandler);

     // wait for 2 minutes
     System.Threading.Timer timerMergeExec = new System.Threading.Timer(timerDelegate, null, 1000 * 60 * 2, 0);

     wrdMailMerge.Execute(ref WordUtils.oFalse);

     timerMergeExec.Dispose();

   }

   private class MergeExecChecker
   {
       private WinWord.Application _wrdApp;
       private bool _timeElapsed = false;

       public MergeExecChecker(WinWord.Application wrdApp)
       {
          this._wrdApp = wrdApp;
       }

       // This method is called by the timer delegate.
       public void TimeElapsedHandler(object retVal)
       {
           this._timeElapsed = true;

           System.Diagnostics.Process[] aProcWrd = System.Diagnostics.Process.GetProcessesByName(«WINWORD»);

           foreach (System.Diagnostics.Process oProc in aProcWrd)
           {
               oProc.Kill();
           }
       }

    }

But I think that this way don’t fit for me, i.e.:
System.Diagnostics.Process[] aProcWrd = System.Diagnostics.Process.GetProcessesByName(«WINWORD»);

I want to create multithreading application and to kill process WINWORD for my wrdApp only, i.e. :

System.Diagnostics.Process myProcWrd = System.Diagnostics.Process.GetProcessById( processID );
myProcWrd.Kill();

But I don’t know how to receive processID from my Microsoft.Office.Interop.Word.Application object
Help me please.
Whether there can be somebody can give me advice correct my approach in general ?

Nicholas C. Rossis

~ Award-winning, dream-protecting author

Nicholas C. Rossis

01
Tuesday
Sep 2020

I came across this great slideshow by Patrick McLean on Slideshare (many thanks to Laura for the tip!)

As a big part of writing and, particularly, editing is removing redundant words, I thought you’d enjoy it as much as I did!

20 thoughts on “How to Kill a Word”

  1. This is brilliant. Thanks for sharing. I’m sure I need to kill loads of words!

    LikeLiked by 1 person

    Reply

    • For added impact, try screaming “die! Die!” as you delete them. Just try not to do it in public.

      LikeLiked by 2 people

      Reply

  2. Now I am paranoid about the fact that I obviously use too many words!
    Best wishes, Pete.

    LikeLiked by 1 person

    Reply

  3. I thought about using this for my writing process course in the editing module of the course, but I finally decided against it. Gun violence and killing, even metaphorical, is too much of a risk. Maybe I’ll reconsider when/if our culture becomes more sane than it is now.

    LikeLiked by 1 person

    Reply

    • So glad you enjoyed it, Jan! Thank you 😀

      LikeLike

      Reply

  4. Love this! (I’d like to send this to some of my editing clients and use it as my defense.)

    LikeLiked by 1 person

    Reply

  5. Thats a great idea. Thank you for sharing this, Nicholas.

    LikeLiked by 1 person

    Reply

  6. “Sometimes you get to kill all of the words.” I laughed so hard.

    LikeLiked by 1 person

    Reply

Please share your thoughts before you go!

Like this post? Please share to your friends:
  • How to keep your word
  • How to install microsoft word
  • How to insert text in word
  • How to insert tables in word
  • How to insert table in excel