On 23.06.15 03:52, Rudra Banerjee wrote:
> Say, I have a file:
> Program bar
> <program text>
> End Program bar
>
> Is it possible that if I change the word "foo" in the first line to
> "bar" (which may not be the first line of the file), the last line's
> "foo" will also be changed to "bar" automatically?
From the shorter repost, I assume there were no replies to the longer
one, so I'll venture the simple semi-automatic method I use:
1) Place cursor on the first "foo", and hit '*'.
2) We're now on the second. Type "cebar<Esc>" to replace with bar.
3) Type "N." to replicate the replacement on the first.
Since the first and second steps (target identification and
replacement), remain manual input in even an automated method, the cost
of a manual "N." for repetition is so low that striving for full
automation seems low. After all, what is the worth of full automation of
2foo when the next use case involves 3foo?
That said, if you hit 'qa' beforehand, perform the above, then 'q',
followed by '"np' , then we can see that we have full automation in
register 'a':
*cebar N.
That could be reused on another target other than "foo", but it is
perhaps too automated, because the replacement text should doubtless
then differ.
My preferred solution for automated consistency enforcement would be a
couple of lines of awk, since that is an efficient text processing tool,
while vim is a text editor. E.g.:
$ cat /tmp/text
Program bar
<program text>
End Program foo
Program far
<program text>
End Program boo
erik@ratatosk:~$ gawk '/^[^End]+Program/ {tgt = $2}
/^ *End *Program/ {$3 = tgt} {print}' /tmp/text
Program bar
<program text>
End Program bar
Program far
<program text>
End Program far
For the given syntax, it'll reform a file of any length, in one step.
Add " > /some/file" to capture the output. Put it in a bash script
with a following overwrite of the original file if that's desired.
It's "horses for courses", I suggest. (And every step pretty simple)
Erik
--
I have long felt that most computers today do not use electricity.
They instead seem to be powered by the "pumping" motion of the mouse!
- William Shotts, Jr. on http://linuxcommand.org/learning_the_shell.ph
--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment