Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to remap option-backspace behave in vim in Mac OS X's Terminal app similarly to how it would behave in other Mac applications.

It should perform a backwards delete word, also deleting any leading whitespace characters.

The closest match I've been able to find so far is the db command, but this only deletes to the beginning of the word; it does not remove any leading whitespace.

Here are some examples of what I am trying to accomplish (| is the cursor position):

test text| here
" After hitting option-backspace:
test| here

test text |here
" After hitting option-backspace:
test| here

high-lighting| text
" After hitting option-backspace:
high-| text

midway throu|gh text
" After hitting option-backspace:
midway|gh text

start |of line
" After hitting option-backspace:
|of line

I initially was going to do dbdh, but in the case of the third example, this would not work (the hyphen character is not whitespace, so it should be preserved).

Is there a good way to accomplish this?

EDIT: After doing some more typing, I realized that the "delete leading whitespace" behavior is actually not Mac-like; on my Mac, pressing option-backspace stops at the first non-word character, just like Vim does.

Thank you for joining me on this thought experiment, though (:

share|improve this question

3 Answers 3

up vote 1 down vote accepted

I have no access to a Mac, but I think this does almost, but not quite what you want (it doesn't place the cursor where you indicate in example two):

" Control-backspace works like control-w:
imap <c-backspace> <c-w>

Just change the mapping to use option instead of control.

You may also want/need to do:

:set backspace=indent,eol,start

See:

:help i_ctrl-w
:help 'backspace'
share|improve this answer
test text| here
" After hitting option-backspace:
test| here

test text |here
" After hitting option-backspace:
test| here

The cursor is supposed to be ON a character not between. So I don't really see the difference here. d?<Space> seems to do the trick, though. Or dbx, maybe.

high-lighting| text
" After hitting option-backspace:
high-| text

db works.

start |of line
" After hitting option-backspace:
|of line

If the cursor is On the space, d^. or d^x.

share|improve this answer
    
Thanks. The goal is to have one command that can handle all 4 situations. –  todofixthis Jun 21 '11 at 17:00
    
@Phoenix - (one of) the great thing(s) with Vim and text-objects and motions and so on is IMO that these are all actually quite specific. They form a kind of powerful yet basic vocabulary/grammar that you can mix and match to handle (virtually) any situation. It may seem daunting to learn these basic editing commands but once they are there, in you mind, its kind of hard to go back. But I admit I'm interested by the question and its answer. –  romainl Jun 21 '11 at 20:04

I discovered one method that almost works:

imap ^[^? <esc>bdw<insert>

(^[^? is what Terminal sends when I press option-backspace)

This backs the cursor up to the start of the previous word and deletes the following word, which also appears to include trailing whitespace.

As expected:

test text| here
" After hitting option-backspace:
test| here

There are two problems with it, though:

high-lighting| text
" After hitting option-backspace:
high|- text
" Expected:
high-| text

test te|xt here
" After hitting option-backspace:
test| here
" Expected:
"test|xt here"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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