tuxohaib
tuxohaib

Reputation: 73

Can VIM do this Sublime trick?

I want to edit this text:

Aenean vel sem bibendum, eleifend odio a, dictum nibh.
The third word in the above line is 
Morbi eget est vitae quam ultricies porta vitae quis nunc.
The third word in the above line is 
Praesent accumsan ex a laoreet cursus.
The third word in the above line is 
Quisque efficitur lectus at dolor mollis, sit amet tristique libero lobortis.
The third word in the above line is 

to this text:

Aenean vel sem bibendum, eleifend odio a, dictum nibh.
The third word in the above line is sem
Morbi eget est vitae quam ultricies porta vitae quis nunc.
The third word in the above line is est
Praesent accumsan ex a laoreet cursus.
The third word in the above line is ex
Quisque efficitur lectus at dolor mollis, sit amet tristique libero lobortis.
The third word in the above line is lectus

To do this with Sublime

Select repeated part
ALT+F3
UpArrow
HOME
3 x CTRL+RightArrow
CTRL+Shift+LeftArrow
CTRL+C
DownArrow
END
CTRL+V

this trick is very useful in many cases. can VIM do it?

Upvotes: 3

Views: 150

Answers (3)

SergioAraujo
SergioAraujo

Reputation: 11820

Another vim solution:

:g/^/if line('.') % 2 | normal! wwyiwj$p | endif


g .................. globally
/^/ ................ on every start of line
if
line('.') % 2 ...... Mod of the integer division of line number by 2
normal! ............ normal mode
ww ................. jump to the third word
yiw ................ yank inner word (word under cursor)
j .................. go to the line below
$ .................. jump to othe end of the line
p .................. paste

Upvotes: 1

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58491

As an alternative to a macro solution, you could also use a substitute command

:%s/\vis $\zs/\=split(getline(line('.')-1), ' ')[2]

breakdown

:%s/                                      start a substitute command
\vis $\zs/                                select lines ending with "is "
                                          use \zs to start replacing after the match
\=split(getline(line('.')-1), ' ')[2]     split previous line on spaces and use the 3th item from the list

Note that to use this as a general template following has to hold

  • You need a search criteria to only match the lines you like to change. For your example, this is is $
  • You need to be able to split the previous line on something meaningfull returning the item you need in the same array position for each substituion. For you example this is by splitting on space and returning the 3th item.

Upvotes: 1

Axnyff
Axnyff

Reputation: 9964

It should work easily with a macro. If you have your cursor on the first character of the first line then:

qq: start recording macro named q

2w: advance two words

yw: yank (copy) word

j$: jump to next line and go to end of line

p: paste what you've yanked

+: go to start of next line

q: stop recording macro.

3@q: execute macro named q 3 times

Upvotes: 11

Related Questions