Lookahead and Lookbehind
Look everywhere
Start file
Example for reference:
:%s/\(some\)\@<=thing/one/g
searches for all strings starting with some, then matching thing
changes thing into one
end result: something becomes someone
:%s/\(some\)\@<!thing/one/g
searches for all strings not starting with some, then matching thing
changes thing into one
end result: something is not changed, but everything changes to everyone
:%s/some\(thing\)\@=/every/g
searches for all strings ending with thing, then matching some
changes some into every
end result: something becomes everything
:%s/some\(thing\)\@!/every/g
searches for all strings not ending with thing, then matching some
changes some into every
end result: something is not changed, but someone becomes everyone
End file
Example for reference:
:%s/\(some\)\@<=thing/one/g
searches for all strings starting with some, then matching thing
changes thing into one
end result: someone becomes someone
:%s/\(some\)\@<!thing/one/g
searches for all strings not starting with some, then matching thing
changes thing into one
end result: something is not changed, but everyone changes to everyone
:%s/some\(thing\)\@=/every/g
searches for all strings ending with thing, then matching some
changes some into every
end result: everything becomes everything
:%s/some\(thing\)\@!/every/g
searches for all strings not ending with thing, then matching some
changes some into every
end result: something is not changed, but everyone becomes everyone
View Diff
0a1
>
6c7
< end result: something becomes someone
---
> end result: someone becomes someone
11c12
< end result: something is not changed, but everything changes to everyone
---
> end result: something is not changed, but everyone changes to everyone
16c17
< end result: something becomes everything
---
> end result: everything becomes everything
21c22
< end result: something is not changed, but someone becomes everyone
---
> end result: something is not changed, but everyone becomes everyone
Solutions
The best way to learn is to practice. Below, you will find some of the solutions other golfers have entered. To unlock higher ranked solutions, submit your own entry which does as well or better than the solutions you can currently see - climb the ladder!
Check out these helpful resources to improve your Vim skills... Game on.
#30 Patrick Mosby / @halbtuerke - Score: 27 - 05/07/13 @ 05:02
O<Esc>3jqqlxyyuh3j@"2jq@q@q@qZZ
@zulolosi: @q@q@q and 3@q are equivalent ! Best regards
@udioica: @zulolosi: Not in this case. When running a macro, commands don't expand the undo branch. That's why you can use u after a macro to undo the whole thing, instead of the million little steps it's made of. Result is, if you use u in a macro, it probably won't do what you want. Note that the U command isn't a "real" undo command; it's just a normal editor command that kinda looks like one. That's why u undoes U and such. U is safe in macros, though it's a bit quirky.
@zulolosi: Thanks for the great answer ! Best regards !
3 comments