问题
Is there an easy way to flip code around an equal sign in vi/vim?
Eg: I want to turn this:
value._1 = return_val.delta_clear_flags;
value._2._1 = return_val.delta_inactive_time_ts.tv_sec;
value._2._2 = return_val.delta_inactive_time_ts.tv_nsec;
value._3 = return_val.delta_inactive_distance_km;
(...)
into this:
return_val.delta_clear_flags = value._1;
return_val.delta_inactive_time_ts.tv_sec = value._2._1;
return_val.delta_inactive_time_ts.tv_nsec = value._2._2;
return_val.delta_inactive_distance_km = value._3;
(...)
on A LOT of lines in a file.
I know this seems a little trivial, but I've been running into lots of cases when coding where I've needed to do this in the past, and I've never had a good idea/way to do it that didn't require a lot of typing in vim, or writing a awk script. I would think this would be possible via a one liner in vi.
Explanations of the one-liners is very welcome and will be looked upon highly when I select my accepted answer. :)
回答1:
Something like this:
:%s/\([^=]*\)\s\+=\s\+\([^;]*\)/\2 = \1
You might have to fiddle with it a bit if you have more complex code than what you have shown in the example.
EDIT: Explanation
We use the s/
find/
replace comand. The find part gets us this:
- longest possible string consisting of anything-but-equal-signs, expressed by
[^=]*
... - ... followed by one or more spaces,
\s\+
(the extra\
in front of+
is a vim oddity) - ... followed by
=
and again any number of spaces,=\s\+
- ... followed by the longest possible string of non-semicolon characters,
[^;]*
Then we throw in a couple of capturing parentheses to save the stuff we'll need to construct the replacement string, that's the \(
stuff\)
syntax
And finally, we use the captured strings in the replace part of the s/
find/
replace command: that's \1
and \2
.
回答2:
For interest's sake, here's how I did it as a recorded macro:
qq0"+df=xA<BACKSPACE> = <ESC>"+pxi;<ESC>jq
Peforming that on the first line sets the "q" macro to do what's required. Then on each subsequent line you can execute the macro by typing:
@q
or, say you want to apply the macro to the next 10 lines:
10@q
I always find macros easier for a quick switch like this than figuring out the regex, because they're essentially an extension of how I would do it by hand.
Edit: Dan Olson points out in his comment that if you want to then apply the macro to a range of lines, for instance lines 6-100, you can enter the following. I don't know if there's a more concise syntax that doesn't require the ".*" pattern match.
:6,100g/.*/normal @q
Explanation of the macro
- qq
- start recording in register q
- 0
- go to beginning of line
- "+df=
- delete up to the '=' and put the text into the '+' register
- x
- delete extra space
- A
- go to end of line and enter insert mode
<BACKSPACE>
=<ESC>
- Delete the semicolon, insert an equals sign and a space
- "+p
- insert the test copied earlier into register '+'
- xi;
<ESC>
- reinsert the trailing semicolon
- j
- move down to the next line, ready to reapply the macro
- q
- stop recording
回答3:
:%s/^\s*\(.\{-}\)\s*=\s*\(.\{-}\)\s*;\s*$/\2 = \1;/
should work nicely.
回答4:
:%s/\([^ =]*\)\s*=\s*\([^;]*\);/\2 = \1;/
来源:https://stackoverflow.com/questions/1374105/swap-text-around-equal-sign