Advertisement
despite having used vi for ten years, i still don't know how to do a lot of stuff, cuz i'm lazy. so, my question is, how do I repeat an arbitrary function on every line? Like, say I have a list of usernames, and I want to prepend '&' and append '@example.com' to every single line.
Advertisement
Advertisement
-
Re: slightly n00b question
Mon, December 5, 2005 - 7:53 PMFor simple things you can prepend a number before the command: 99dd will delete 99 lines.
But for what you want to do, that would require a : command - two of them, in fact.
:2,24s/^/\& will prepend the ampersand at the beginning of each line from lines 2 to 24. Ampersand is a special character, so needs to be backslashed.
:%s/$/@example.com will append "@example.com" to every line in the file - % being shorthand for "current file name".
Vi's search and replace is powerful and mighty and worth learning.
-
-
Re: slightly n00b question
Mon, July 17, 2006 - 1:27 AMI realise this is an old thread, but I'm curious why you chose to go with two commands rather than :
:%s/\(.*\)/\&\1@example.com
I assume perhaps it was for the sake of presenting both % and 2,24? -
-
Re: slightly n00b question
Mon, July 17, 2006 - 1:29 AM..just remembered that registers are vim specific.
is there no way to edit posts on here?
-
-
-
Re: slightly n00b question
Mon, December 5, 2005 - 7:55 PM:%s/^/\&
:%s/$/@example.com -
-
Re: slightly n00b question
Wed, December 7, 2005 - 3:02 PMOK, I guess I was looking for a way to do it without using a regexp search-and-replace, but it makes sense that that would be the simplest way to do it.
While we're at it, is there a mode for vim that uses perl-style regexp instead of vi-style? I really hate having to escape every damn parenthesis and bracket when I'm doing pattern matching. -
-
Re: slightly n00b question
Wed, December 7, 2005 - 10:54 PMAs for choosing the range for the substitutions I use the Visual Mode for that if the area is not the whole file or from the point to the end. Just choose the area in Visual Mode and then use the substitution command.
Also for prepending and inserting text without regexps you can use the Block Visual Mode (^V), choose the area and then I for block insert. This works for simple inserts (like # at the beginning of the lines), but for more complicated stuff you need to use regexps.
-
Re: slightly n00b question
Sun, December 11, 2005 - 12:48 PMYou could use "map" so a single key press does this, eg:
map g I&^V^[A@example.com^V^[+
as long as you the right thing with control V and escapes, so pressing g should do the action, and move to the next line, so if you only want it on those lines you were selective about, you press g otherwise press return.
-
-
-
Re: slightly n00b question
Fri, January 13, 2006 - 5:53 PMIs there any way in VIM to change the delimiter used by the search and replace commands?
Using these sed-like commands to replace a Unix path requires an escape character before every single metacharacter character, so I get wonky formulas like:
s/\/usr\/local\/\(foo|bar\)/\/opt\/bar/\1/c
That's hard to read.
Perl supports alternate search delimiters, so you can search & replace like for 's[/usr/local/(foo|bar)][/opt/\1]' (Change /usr/local/foo and /usr/local/bar to /opt/foo or /opt/bar).
-
-
Re: slightly n00b question
Mon, January 16, 2006 - 7:03 PMHaven't tested to see if VIM is vi-compliant, but *any* special character you use after the "s" in a substitute command will act as a delimiter.
Try replacing the /'s with +'s where appropriate and see if that works. -
-
Re: slightly n00b question
Tue, January 17, 2006 - 10:04 AMTested and works!
and vim is vi-compliant, there are also setting in the .vimrc file for that.
-
Re: slightly n00b question
Tue, January 17, 2006 - 6:28 PMWow! Somehow I missed that feature after using vi for 5 year. -
-
Re: slightly n00b question
Tue, January 17, 2006 - 11:16 PMI've never really used it, but it's been one of the bits of vi trivia that's stayed with me.
It's sort-of a geek bar bet kind of bit of knowledge...somewhat akin to knowing what letters of the alphabet aren't flags to "ls"... -
-
Re: slightly n00b question
Tue, January 17, 2006 - 11:23 PMI remember it from using ed in college, I used to use '.' as my delimiter when '/' didn't work very well. -
-
Re: slightly n00b question
Wed, January 18, 2006 - 12:37 PM
I second the Wow! I did not know that either, and I've been using vi and sed for a lot of years. (Does this work in regular sed also, or just within vi?)
-
-
Re: slightly n00b question
Wed, January 18, 2006 - 2:21 PMpretty sure it's sed too,
Tested Solaris 9 sed, works!
-
-
Ed man! man ed!
Thu, January 19, 2006 - 8:22 AMOh! No! Did you really use ed!!!!?
Like this ed: www.gnu.org/fun/jokes/ed.msg.html
-
I <3 girl geeks.
Thu, January 19, 2006 - 11:49 AM"...remember it from using ed ..."
'scuse me, I just got a little verklempt.
-
Re: slightly n00b question
Fri, January 20, 2006 - 6:30 PMBah, feh!
I can't use an alternative delimiter on my Solaris 8 systems. I finally moved to a company that runs Solaris 9. Now it works.
-
Re: slightly n00b question
Sat, April 1, 2006 - 10:20 PMI don't like using "+" since in extended grep that means one or more (like * is zero or more).
Similarly . is dangerous as it is a special character meaning any character matching pattern.
I use comma, ie: s,x,y, when / is getting unreadable or I want to cut/paste things in (like HTML), and comma is quite rare, and doesn't upset special characters. If I know , is going to be there I'll use underscore (_) - but much more rarely.
But . and + are as valid as anything else -
-
Re: slightly n00b question
Sun, April 16, 2006 - 10:08 PMWell, + was the first key that came to mind. The important part is that you can use any character you like. + is just visually distinctive to me. -
-
Re: slightly n00b question
Sat, April 22, 2006 - 12:21 PMThe # (pound sign) sign seems to be widely used in a number of examples. It seems like that would work pretty well. -
-
This is the maximum depth. Additional responses will not be threaded.
Re: slightly n00b question
Sat, April 22, 2006 - 2:13 PM
I have been using this trick a lot since I learned it here, so thanks a lot, everyone!!
(The fact that I can use it means it also works under MKS Toolkit, by the way.)
-
-
-
-
-
-
-
Re: slightly n00b question
Mon, July 17, 2006 - 1:25 AMThanks for this extremely useful tip! I wasn't aware either.
-
Re: slightly n00b question
Mon, August 27, 2007 - 5:54 AMHi,
This works for substitution (:s). Is there something similar for search ('/')?
If I would like to search for 'foo/bar', I need to type: /foo\/bar, because
/foo/bar would also match just 'foo' due to the presence of the '/' delimiter.
Searching for e.g. pasted dir names requires replacing all ocuurences of
'/' in '\/' which is sometimes a pain.
Thanks,
Paul
-
-