slightly n00b question

topic posted Mon, December 5, 2005 - 7:00 PM by  Alex
Share/Save/Bookmark
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.
posted by:
Alex
SF Bay Area
Advertisement
Advertisement
  • Re: slightly n00b question

    Mon, December 5, 2005 - 7:53 PM
    For 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, December 5, 2005 - 7:55 PM
    :%s/^/\&
    :%s/$/@example.com
    • Re: slightly n00b question

      Wed, December 7, 2005 - 3:02 PM
      OK, 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 PM
        As 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 PM
        You 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 PM
    Is 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 PM
      Haven'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

        Mon, August 27, 2007 - 5:54 AM
        Hi,

        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

Recent topics in "Vi"