Tip #81: Substitution of characters and lines in VIM is made far easier with the s and S commands
tip karma |
Rating 30/30, Viewed by 4257
|
created: |
|
June 21, 2001 14:41 |
|
complexity: |
|
basic |
author: |
|
Substitue characters and lines |
|
as of Vim: |
|
5.7 |
Substitute Characters
-----------------------------------
I was just editing a file that contained the same leading string on many lines.
example:
foo_bar_baz1=a
foo_bar_baz1=abc674
foo_bar_baz1=qrs
foo_bar_baz1=m1
foo_bar_baz1=bz90
foo_bar_baz1=bc
...
Needing to only substitute a portion of the string, I referred to a VIM reference card and discovered a command answering my need exactly. The s command is used to subsitute a certain number of characters. In my example file above, if I only needed to subsititute the characters foo_bar, I set the cursor on the first character where I'd like the subsitution to begin and type 7s. VIM drops the characters foo_bar and goes to insert mode, waiting for the substitution text.
Substitute Lines
-----------------------
After years of using vi and VIM and always deleting multiple lines in order to replace them, I just discovered the S command. If you need to subsitute three lines of text, simply type 3S. VIM drops the three lines and goes into insert mode, waiting for the subsitution text.
<<Restore cursor to file position in previous editing session |
letting variable values be overwritten in a script >>
Additional Notes
|