322

Tried to map it from Preferences -> Settings -> Keyboard, but the "key" combo box has only "forward delete" but no "delete". My keyboard on the other hand has only "delete" and no "forward delete"!

Is there some other way to do it except from the preferences?

ibz
  • 44,461
  • 24
  • 70
  • 86
  • By "backward delete word", do you mean the backspace key? I'm not aware of any recent Apple keyboard that has a delete key. – Sören Kuklau Nov 29 '08 at 16:29
  • 1
    By "backward delete word" I mean exactly that - to delete the whole word starting from the cursor backward. :) The last Macbook has a key labeled "delete", but it acts as backspace. – ibz Dec 01 '08 at 05:58
  • i'm facing the same issue. keyboard has backward delete but can't be used as binding in Terminal.app. – knoopx Aug 06 '09 at 13:31
  • For me, your question deserves millions of upvotes. It helped me a lot. Earlier, I was Ubuntu. Now, this made my life easier in Mac OS too. Thanks. – doptimusprime May 19 '17 at 03:51

9 Answers9

601

Enable option key as meta key

  1. Go to Terminal > Preferences > Profiles > Keyboard
  2. Check Use option key as meta key.

Image

On macOS High Sierra 10.13.6, captured on October 23, 2018.

Illustration of the window for toggling option key as meta key

Notes

Many applications (including bash and tcsh) treat Meta-Delete as "backward delete word."

dsapalo
  • 1,819
  • 2
  • 19
  • 37
Andru Luvisi
  • 24,367
  • 6
  • 53
  • 66
  • 1
    Running on OSX Lion and don't see the 'Use option key as meta key' option. – AlikElzin-kilaka Nov 06 '12 at 12:28
  • 44
    It's under Terminal preferences > settings > keyboard, right at the bottom. – Jon Feb 18 '13 at 13:46
  • 9
    On Yosemite, this is setting is now under Preferences -> Profiles -> Keyboard. Looks like you have to change/set it for each profile. – palimpsestor Mar 09 '15 at 21:08
  • 11
    On non-US keyboards, option key is often needed to get certain characters (e.g. pipe '|' is pretty important at shell). In that case b and f mentioned in other answers are probably the best solution? – Touko Apr 27 '15 at 06:52
  • 13
    Unfortunately, this is counter-productive for programmers with foreign keyboards who need to use the option key to get special symbols like `{`, because that won't work when the option key is used as meta key. – Magne Mar 01 '17 at 12:22
  • 1
    Thank you so much! Fortunately I'm using a US keyboard and this works perfectly. – Lucas P. Oct 09 '18 at 13:33
  • 5
    Why hadn't I googled for this before. I think I lost literally days of productivity all these years. – sandre89 Jul 04 '19 at 13:10
  • 1
    It seems that (at least on Big Sur) there's now a dedicated switch for the meta-key using Cmd+Option+O or the Touch Bar, so you can toggle between sane navigation and curly braces. – komodovaran_ Dec 28 '20 at 15:05
  • 1
    @sandre89, you're not alone. At least we learned! – anonymoose Nov 22 '22 at 00:51
97

Delete a word going back:

Ctr-w.

Shai
  • 1,281
  • 9
  • 7
  • 4
    well after reading this i found out that "delete word forward" is ctrl+k (by trial and error). now i wonder why there is so little documentation about all this. – victor n. Oct 07 '15 at 16:14
  • 13
    Ctr-k deletes all in front, not just one word, no? – Shai Oct 08 '15 at 21:14
  • this works on my mac. does it work on ubuntu linux too? – user391339 Mar 08 '18 at 08:49
  • 4
    Ctrl+w deletes a WORD (separated by whitespace), not a word (separated by punctuation+whitespace). With cursor at end of `cat .git/refs/heads`, Ctrl-w results in `cat`. Meta+delete results in `cat .git/refs/` – idbrii Feb 27 '19 at 06:43
84

OS X's terminal runs bash, which includes readline support. Follow Glomek's advice and tell terminal to use option as meta key (or else use Esc) and then you've got a bunch of handy options: Ctrl+w deletes prev word (as does Meta+delete as mentioned), but you can also use Meta+f and Meta+b to walk forward and backwards thru words, Ctrl+a and Ctrl+e to beginning and end of line, Ctrl+k delete (kill) from cursor to end of line, and a bunch more.

See http://www.bigsmoke.us/readline/shortcuts for a nice little reference table.

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
nadafigment
  • 849
  • 6
  • 2
  • 3
    How is this "handy"? The rest of the system uses `delete` with qualifiers to delete words. – idrougge Jul 28 '17 at 07:48
  • Is it possible to have "delete line" as well? – wenbert Nov 20 '17 at 22:20
  • 3
    Readline is great, but this doesn't help OP. Ctrl+w deletes a WORD (separated by whitespace), not a word (separated by punctuation+whitespace). With cursor at end of `cat .git/refs/heads`, Ctrl-w results in `cat`. Meta+delete results in `cat .git/refs/` – idbrii Feb 27 '19 at 06:43
  • @nadafigment, 'a' and 'e' stands for? – Pacerier May 21 '20 at 11:24
61

Command Editing Shortcuts

Ctrl + a – go to the start of the command line

Ctrl + e – go to the end of the command line

Ctrl + k – delete from cursor to the end of the command line

Ctrl + u – delete from cursor to the start of the command line

Ctrl + w – delete from cursor to start of word (i.e. delete backwards one word)

Ctrl + y – paste word or text that was cut using one of the deletion shortcuts (such as the one above) after the cursor

Ctrl + xx – move between start of command line and current cursor position (and back again)

Alt + b – move backward one word (or go to start of word the cursor is currently on)

Alt + f – move forward one word (or go to end of word the cursor is currently on)

Alt + d – delete to end of word starting at cursor (whole word if cursor is at the beginning of word)

Alt + c – capitalize to end of word starting at cursor (whole word if cursor is at the beginning of word)

Alt + u – make uppercase from cursor to end of word

Alt + l – make lowercase from cursor to end of word

Alt + t – swap current word with previous

Ctrl + f – move forward one character

Ctrl + b – move backward one character

Ctrl + d – delete character under the cursor

Ctrl + h – delete character before the cursor

Ctrl + t – swap character under cursor with the previous one

Command Recall Shortcuts

Ctrl + r – search the history backwards

Ctrl + g – escape from history searching mode

Ctrl + p – previous command in history (i.e. walk back through the command history)

Ctrl + n – next command in history (i.e. walk forward through the command history)

Alt + . – use the last word of the previous command

Command Control Shortcuts

Ctrl + l – clear the screen

Ctrl + s – stops the output to the screen (for long running verbose command)

Ctrl + q – allow output to the screen (if previously stopped using command above)

Ctrl + c – terminate the command

Ctrl + z – suspend/stop the command

Bash Bang (!) Commands

Bash also has some handy features that use the ! (bang) to allow you to do some funky stuff with bash commands.

!! – run last command !blah – run the most recent command that starts with ‘blah’ (e.g. !ls)

!blah:p – print out the command that !blah would run (also adds it as the latest command in the command history)

!$ – the last word of the previous command (same as Alt + .)

!$:p – print out the word that !$ would substitute

!* – the previous command except for the last word (e.g. if you type _find somefile.txt /, then !* would give you _find somefile.txt)

!*:p – print out what !* would substitute

rattray
  • 5,174
  • 1
  • 33
  • 27
Curtis M
  • 905
  • 1
  • 8
  • 14
  • 4
    That's a super useful list mate! O: I copied it and added it to my commonplace book! The Ctrl + xx is amazing! – Gregordy Sep 02 '20 at 02:35
  • that list is truly awesome! Only that I tried to use shortcuts with alt + letter and that prints one of those greek letters in current versions of mac (Catalina/Big Sur/...), for example: alt +d does a "∂" (delta, I think). So it's kinda a bummer and don't know how to bypass that. – 40detectives Dec 09 '21 at 09:26
28

⌃W (control+W) is not the same thing than meta + delete (ESC+delete if you don't have checked use option as meta key) meta+delete will treat / _ as word delimiter where ^W will consider space as delimiter.

e.g. using ESC+Bakcspace on (cursor at the end)

rm /dira/dirb/file1

gives

rm /dira/dirb/

while ^W on the same will give

rm

So it is better to use \033\177 rather than ^W when defining the modifying the terminal profile. That way you are really mapping ⌥⌫ to what esc⌫ is doing and you keep having ^W to erase word based on space delimiter.

I would love to be able to post an image on how do this but as a newbies I can't.

refer to bouke comment to see how to modify terminal profile.

Robert
  • 5,278
  • 43
  • 65
  • 115
  • 8
    For those wanting to do this in `iterm2`, you could go to Preferences > Keys > add the Key Mapping `⌥⌫` to send hex codes `0x1B 0x7F`. – qix May 17 '18 at 06:00
  • 1
    How did you find out that `\033\177` is what you want for "delete a part of a word"? – adiabatic Sep 02 '19 at 19:45
  • 2
    indeed you just have to press the key sequence you want in the Terminal Preference dialog. Preferences→ Profiles → Keyboard tab → + button then choose modifier "option" and key "⌫ delete" then select the text field and press escape key then press the ⌫ key and you're done. Esc+⌫ does the same thing as ⌥⌫ – Stephane Gasparini Sep 03 '19 at 21:58
28

By default meta backspace does a backwards word yank in most shells. If you press escape and then press backspace, that pretty much works everywhere.

Dustin
  • 89,080
  • 21
  • 111
  • 133
  • 3
    Fantastic, that requires no extra setup. I notice ` b` and ` f` also work for jumping back or forwards one word. – joeytwiddle Feb 03 '15 at 09:31
  • 2
    I like + ARROW more. ESC does not allow continuous clicks on b or f. – Vitali Pom Sep 25 '15 at 18:45
  • 2
    I have been using + ARROW to move around, but I find that there is no way to delete in a similar way. The CTRL + W approach mentioned above doesn't stop at non-word characters the same way + ARROW does, so it deletes everything up until the whitespace. Any way to delete using a similar boundary detection as + ARROW would be appreciated – Rene Wooller Nov 23 '16 at 05:43
21

In your Terminal's profile, you can bind ⌥⌫ (option+delete) to ⌃W (control+W), so it works similar to other apps.

Terminal Profile Keyboard settings

Bouke
  • 11,768
  • 7
  • 68
  • 102
  • stty has to have ^w mapped to werase (default behavior) for this to work. Use stty -a on the command line to see how mappings are set. – natersoz Aug 29 '17 at 22:24
4

If you are using Iterm2 (which is like the regular terminal of OSX just better :) you can use this like to change this behavior:

You need to set your left ⌥ key to act as an escape character. This can be done going to iTerm2 > Preferences > Profiles > Keys.

enter image description here

Oded BD
  • 2,788
  • 27
  • 30
3

With Natural Text Editing preset enabled in Profile -> Keys. You can simply remove the word

  • Backward with alt + delete
  • Forward with fn + alt + delete
Anthony
  • 1,850
  • 2
  • 15
  • 15