How to use VISUAL BLOCK to select words of different sizes?

Viewed 273

I think no one ever asked this question.

Say you want to select just the the variables after the import word on each line(settings, filters, user, countries, favorites) in this text:

import settings from './settings';
import filters from './filters';
import user from './user';
import countries from './countries';
import favorites from './favorites';

I tried to put my cursor at the beginning of the settings word, press C-V4jE but not success. I also tried to apply REGEX after entering in VISUAL BLOCK mode, something like /\w\s but yields the same result(a fixed column length block of text):

settings 
filters f
user from
countries
favorites

The result I'm expecting to do on Vim is this:

settings
filters
user
countries
favorites

I think I saw this feature on VS Code but after MULTIPLE searches I found 0 results about this solution on Google, everyone shows only how to select fixed column length texts OR just saying to use $ which applies only for texts that you want to select to the end of the line.

Does anyone knows? Thanks!

2 Answers

It's visual-block mode: what you select is the whole block, not some arbitrary subset or superset of it.

"Visually selecting" multiple non-contiguous pieces of text of arbitrary length is not something that can be done with built-in means so you can either try some kind of "multi cursor" plugin or approach your problem differently…

For example, if you want to wrap those words in braces because they are no longer default exports, you can do something like this:

:'{,'}norm wciw{ <C-v><C-r>" }<CR>

Where the normal mode command wciw{ <C-v><C-r>" }:

  1. move the cursor to next word with w,
  2. change inner word with ciw,
  3. insert { ,
  4. insert content of unnamed register with <C-r>" (<C-v> is used to insert a literal ^R),
  5. insert },

is executed on each line in range '{,'}, representing the current paragraph.

Or, as per your comment, if you want to yank those words for later use, you could do something like:

qaq
:'{,'}norm w"Ayiwh"Ayiw

Where register a is cleared with qaq and the normal mode command w"Ayiwh"Ayiw:

  1. move the cursor to next word with w,
  2. append it to register a with "Ayiw,
  3. move the cursor to the preceding space with h,
  4. append it to register a with "Ayiw,

is executed on each line in range '{,'}, representing the current paragraph.

Those things come naturally over time.

If you want to copy these words, you could apply some normal mode commands to all lines (or to a visual range) with:

:%norm 0WywG[p 

to grab the 2nd word and paste each one at the end of the file and do what you like with them:

import settings from './settings';
import filters from './filters';
import user from './user';
import countries from './countries';
import favorites from './favorites';

settings 
filters 
user 
countries 
favorites 
filters 

Or you could make a copy of the lines, visually select them and do:

:'<,'>norm dwwd$

to delete the first word, jump to the 3rd and delete the rest of the line, leaving just those words you are trying to select visually in your question:

settings 
filters 
user 
countries 
favorites 
filters 
Related