In my fortran code I have a lot of blocks like the following
subroutine name(arg1,arg2,arg3,...)
:
end subroutine
and I want them to become
subroutine name(arg1,arg2,arg3,...)
:
end subroutine name
Where each of these blocks can be indented by any amount of spaces (in fact, such blocks can be nested). I tried with the command
:g/^ *subroutine \(.*\)/;/end subrout/s/end subroutine/& \1
which changes each line (I read it in the bottom line) changing nothing, just as the buffer \1
is empty.
The command
:g/^ *subroutine \(.*\)/;/end subrout/s/end subroutine/& hello
works well, but is obviously not what I want.
So the question is: how can I use in the replacement string a pattern matched by the global ex command :g
?
EDIT I'm editing the question since, at the time I posted it , I hurriedly accepted the answer which was an answer to the specific case I referred to, but not to the title of the question, which is a little bit more general.
I go to the point. I have a file like the following (non-sense just to make it as general as possible):
# sec1
fun1 are you a function?
fun2 no.
fun3 ok, nice to meet you!
# 2nd part
first line
third line
ops (it was the second)
there were 3 lines in the preceding #
# fourth group
now there's just one
and foreveeer
(In fact, the lines following each #
-line are hundreds.)
I have to make substitutions in every line following each #
-line (and preceding the following #
-line); these substitutions should all use the text after the leading #
-line (or part of it). An example of want I would like to achieve is
# sec1
fun1 in sec1 are you a function?
fun2 in sec1 no.
fun3 in sec1 ok, nice to meet you!
# 2nd part
first in 2nd line
third in 2nd line
ops in 2nd (it was the second)
there in 2nd were 3 lines in the preceding #
# fourth group
now in fourth there's just one
and in fourth foreveeer
Note that only the first word following #
is inserted in the following lines.
So, as I already said, I'd like to know if the pattern of the :g
command can be used in substitute string. I'm so interested in this answer since it is very annoying to register a macro (one wrong keystroke means starting all over again!), whereas a command like this,
:g/^# \(\<\w\+\>\)/+;/^#/- s/\(\<\w\+\>\)\(.\+$\)/\2 \1 \3
would be perfect! ... And it's hard for me to belive that there's no way to use a command that does the job!