If I have a text containing markdown matching markings e.g.

"Strikethrough uses two tildes. ~~Scratch this.~~"

What would be a good way to delete all those occurrences such as - in this example - "~~Scratch this.~~"?

  • Do you want to delete the characters between the characters? Or do you just want to delete the characters and not the text? – klaus Apr 2 '19 at 6:47

I guess you want to search all strike-through text and delete it. That can be done with the following substitute command:

  • The escaped tilde \~ is needed as ~ "matches the last given substitute string" (:help /~)
  • The \_. matches any character including newline, so multi-line strike-through is handled. (:help /\_. and :help /\_)
  • The \{-} matches "as few as possible". (help /\{-) BTW: Using * here would match from the first to the last ~~ in the file, as * matches "as many as possible".

This command also handles multiple strike-throughs per line.

| improve this answer | |

Put these in your vimrc and then source the vimrc with :source $MYIMRC:

" simple text-objects
for s:char in [ '~', '_', '.', ':', ',', ';', '<Bar>', '/', '<Bslash>', '*', '+', '%', '`' ]
    execute 'xnoremap i' . s:char . ' :<C-u>normal! T' . s:char . 'vt' . s:char . '<CR>'
    execute 'onoremap i' . s:char . ' :normal vi' . s:char . '<CR>'
    execute 'xnoremap a' . s:char . ' :<C-u>normal! F' . s:char . 'vf' . s:char . '<CR>'
    execute 'onoremap a' . s:char . ' :normal va' . s:char . '<CR>'

Then you can do di{char} to delete the texts in between the {char} and da{char} to delete the texts in between the {char} along with the characters themselves.

The for loop iterates over :h list containing the {char} and execute :h mapping commands for :h operator-pending-mode and :h visual-mode so that the {char} in the list can act as :h text-objects

| improve this answer | |

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.