0

If I have text like this:

cat = "john"
leopard = "frank"
tiger = "josh"

and I want to select the first word of each line in block-wise visual mode, I want to be able to do something like this:

  1. put cursor before first word of first line
  2. hit <C-v>
  3. hit j twice
  4. hit w to move forward a word, selecting the first word of each line

However, this doesn't work, as vim doesn't allow each line's selection length to be of variable length. It ends up selecting an equal number of characters on each line, e.g. (removed unselected text for display purposes):

cat = "
leopard
tiger =

Is there a way to let <C-v> select text in each line asynchronously by hitting w or b to select words on each line even if they aren't the same length?

If not, is there a way to create a macro for this?

  • 1
    There are lots of ways to manipulate those 3 words (though Idk of any visual ones, because of the way visual-block works). Can you share your end-goal instead? e.g.., you can remove those words with :%s/.*=// which is much faster than visual mode. Just depends on what you really want to do. – D. Ben Knoble May 7 at 19:43
  • @D.BenKnoble i want to cut/copy them. Your regex works for deleting them, thanks for solving that case. But still is it not possible to do this visually or modify visual-block to be able to do this? – Zaid Gharaybeh May 7 at 20:11
  • depending on what you want, either line wise selection might do for you, or :set virtualedit=all – Christian Brabandt May 7 at 20:37
2

In general, visual block selections must be blocks (rectangles). The only "jagged" selections are when you hit <C-v>$ to select to the end of multiple lines.

However, depending on what you want to do, there are often several approaches. In this case, to copy:

" clear register A
let @a = ''
" yank all the words
global/=/normal! 0"Ayaw

Using a capital letter will append to the register instead of overwriting it.

| improve this answer | |
  • 1
    Thanks, this works. I see it is a similar solution to this question. – Zaid Gharaybeh May 7 at 21:12

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.