1

Given the following text: Hello Part1/Part2 Bye

With the cursor on top of any character in Part1/Part2 I would like to be able to refer to the whole Part1/Part2 (for visual selection, deletion, substitution, etc.). For simplicity, let's talk about deletion.

diw considers / a delimiting character, so it will consider Part1/Part2 three different words: Part1, / and Part2, and will delete only one of them (depending on where the cursor is).

I have also tried diSpace (since using di" will delete everything inside quotes, and di( will delete everything inside matching brackets, etc.), but it does not seem to work with arbitrary delimiters.

So two closely-related questions, the first one being a particular case of the second one:

  • How can I quickly refer the word under the cursor (word in this context meaning "text delimited exclusively by blank spaces")?
  • How can I quickly refer to the text between two arbitrary delimiters?

I am aware of the answer to a similar question, but I'd like to keep the solution in line with the di" style (i.e. not visually selecting the text and then performing an operation; but instead first defining the operation and then specifying the text in which it needs to be performed). If no default command will cut it, I'd be happy with something I can map to a customized key binding.

  • 2
    For the first, try a WORD instead of a word: diW. – muru Apr 19 '17 at 8:17
2

Taken from @romainl answer here:

I've had the following snippet in my vimrc for quite a while and it never failed me:

for char in [ '_', '.', ':', ',', ';', '<bar>', '/', '<bslash>', '*', '+', '%' ]
  execute 'xnoremap i' . char . ' :<C-u>normal! T' . char . 'vt' . char . '<CR>'
  execute 'onoremap i' . char . ' :normal vi' . char . '<CR>'
  execute 'xnoremap a' . char . ' :<C-u>normal! F' . char . 'vf' . char . '<CR>'
  execute 'onoremap a' . char . ' :normal va' . char . '<CR>'
endfor

It does not work for arbitrary characters out of the box (since they need to have been defined in the snippet) but it isn't hard to customize for the most common delimiters.

0

The question was answered in substitute ... between ... arbitrary characters

The solution employs the t/ T "Till" commands

I'd like to mention the excellent vim-surround plugin here too, it will allow you to handle the delimiting characters...

  • This doesn't answer the question ("I'd like to keep the solution in line with the di" style"). @muru 's comment already gave the correct answer – Philippos Apr 19 '17 at 9:05
  • 1) @muru's answer is only correct, if the object is surrounded by space, try it yourself. 2) The referenced answer also contains an explanation on how to define text objects yourself, which would allow the OP to achieve what he wants with arbitrary surrounding characters. 3) I mentioned the Till commands, because they are easier. – mike Apr 19 '17 at 9:11
  • Exactly. Which perfectly fits the first question, as Muru said. – Philippos Apr 19 '17 at 9:14
  • @Philippos it is not an answer to the whole question, only for a special case. I referred to a complete answer and you are voting me down for that? – mike Apr 19 '17 at 9:19
  • Thank you for your comment, one of the answers to that question has an interesting option :) – user2891462 Apr 19 '17 at 9:39

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.