23

I am trying to select multiple lines that are not in a continuous chunk. E.g., I want to select line 1 and 3 simultaneously without selecting line 2:

 1. this is line 1 
 2. this is line 2 
 3. this is line 3

Initially I thought this would be a trivia task, but after spending quite some time googling around to no avail, I realized this might not be a simple/common task.

Many thanks in advance for your help.


Edit: Thanks for the responses. I will provide a little more details on how I came up with the question.

I was trying to align a chunk code like the following, using Tabularize:

1. name1="Woof"
2. lucky_dog = lucky( "dog_one"= name1, 
3.                    "dog_two"= name1 )
4. name2="Howl"

I wanted it to align like this:

1. name1     = "Woof"
2. lucky_dog = lucky( "dog_one"= name1, 
3.                    "dog_two"= name1 )
4. name2     = "Howl"

But I cannot do so because Tabularize will take third line into consideration, and align everything into:

1.name1                        = "Woof"
2.lucky_dog                    = lucky( "dog_one"= name1,
3.                   "dog_two" = name1 )
4.name2                        = "Howl"

I believe I could think of some regex trick to archive the desired results, it just occurred to me at first that maybe I could simply select line 1,2,4 and make those align.

Then I realized this is not a easy task.

Hence the question.

Thanks for the responses!

K Z
  • 29,661
  • 8
  • 73
  • 78
  • 4
    No (as far as I know). But, if you tell what exactly you want to do with this selection we may help in finding another way to do it… maybe. – sidyll Sep 03 '11 at 23:05
  • @sidyll Thanks! It all started when I was trying to align a few lines using the plugin Tabularize. I wanted to skip a few lines in a chunk of the code, but align the rest of lines. I could do this with regex, but it also occurred to me that if I could just simultaneously select those lines, Tabularize can just align those I selected while skipping the rest. Hence the question. – K Z Sep 05 '11 at 05:26
  • This question has the perfect answers https://stackoverflow.com/questions/23916256/vim-skipping-lines-in-visual-selection – ahron Dec 12 '21 at 14:06
  • [This script](http://www.vim.org/scripts/script.php?script_id=953) seems to provide the functionality you're looking for. – Frost Sep 03 '11 at 23:10

2 Answers2

15

There is now a brilliant plug-in that enables multi-select in Vim: Vim-multiple-cursors:

enter image description here

K Z
  • 29,661
  • 8
  • 73
  • 78
9

It's not possible to select different chunks of text in vim.

What you can do instead is identify a common, unique pattern that is shared by the lines you want to act on and use the 'global' ex-command or 'g' to do it like so:

:g/shared unique pattern/ex or normal command here

For example to copy the lines to a register, say the 'a' register:

:g/shared unique pattern/normal "Ayy

To paste them hit "ap

The capital A that comes before yy tells vim that you want to copy and append the lines to the a register.

Like sydill said if you can tell us what exactly you want to do with the lines then we can better help you.

holygeek
  • 15,653
  • 1
  • 40
  • 50
  • OK. By the way, this is the only solution I could think of too. I'm wondering if there are other solutions... – lucapette Sep 04 '11 at 09:08