1

Commands I use a lot are commands like:

dib (delete everything between two surrounding parantheses)

di{ (delete everything between two surrounding curly brackets)

... it works for a few other characters as well.

How do I make it work for arbritrary charcters?

For instance, I would like the following:

di (delete everything between two surrounding spaces)

dit (delete everything between two surrounding 't' characters)

... and so on; it should work for every character.

2
  • 1
    I don't know how to do it, but di would be the same as diW ('delete inside WORD = contiuous text delimited by whitespace. Aslo, dit already means 'delete inside tag', so you'd be losing a text object by doing it.
    – mattb
    May 2 at 18:31
  • 1
    If you don't mind a plugin, vim-sandwich almost does what you want. Instead of di{char}, dis{char} deletes everything between two {char}s.
    – husB
    May 3 at 4:16
2

I wrote a few functions to do 'delete', 'change', and 'yank' in between, and 'around' two characters on a line, which are called using normal mode commands:

function! DeleteInside(char)
    execute "normal! F".a:char
    execute "normal! ldt".a:char
endfunction
function! DeleteAround(char)
    execute "normal! F".a:char
    execute "normal! df".a:char
endfunction

function! ChangeInside(char)
    :call DeleteInside(a:char)
    :startinsert
endfunction
function! ChangeAround(char)
    :call DeleteAround(a:char)
    :startinsert
endfunction

function! YankInside(char)
    :call DeleteInside(a:char)
    execute "normal! u"
endfunction
function! YankAround(char)
    :call DeleteAround(a:char)
    execute "normal! u"
endfunction

nnoremap <LEADER>di :call DeleteInside('')<left><left>
nnoremap <LEADER>ci :call ChangeInside('')<left><left>
nnoremap <LEADER>yi :call YankInside('')<left><left>
nnoremap <LEADER>da :call DeleteAround('')<left><left>
nnoremap <LEADER>ca :call ChangeAround('')<left><left>
nnoremap <LEADER>ya :call YankAround('')<left><left>

With the cursor in between the x's:

x delete me x

Use <LEADER>di to get:

:call DeleteInside('')

and you just need to put the desired letter between the quotes (the cursor will already be in the right place) and hit enter to get:

xx 

Update

If you can't use a function to do it, then the only thing I can think of is 'hard coding' the problem (you could use the same pattern for 'change' and 'yank', if desired):

nnoremap dia Faldta
nnoremap dib Fbldtb
nnoremap dic Fcldtc
nnoremap did Fdldtd
   .      .    .
   .      .    .
   .      .    .

N.B. I would not do this for ones that already exist (like dib)! You could use di in the nnoremaps to avoid interfering with default mappings. Also these will not be repeatable with the dot command.

Update 2:

See other (better?) solutions to this (possible duplicate) question. Also there is a plugin to do this in a nice way.

2

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.