Loading ...
Sorry, an error occurred while loading the content.

replacing all characters in the current line?

Expand Messages
  • Jose Caballero
    Hi, this happened to me today. I had a line like this one =================== and I wanted to replace all characters by - , so I could have something like ...
    Message 1 of 6 , Nov 1, 2011
    • 0 Attachment
      Hi,

      this happened to me today. I had a line like this one
      ===================
      and I wanted to replace all characters by '-', so I could have something like
      --------------------

      I thought I could do it by combining 'g' and 'r' as I understood 'g' is good to repeat the same command over all chars in a line (like guu or gUU).
      However, I was not able to make it work.
      I ended up doing something likeĀ  :.s/=/-/g

      Is not really possible to replace all chars in the current line with a combination of 'g' and 'r' commands?


      Cheers,
      Jose

      --
      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
    • Sven Guckes
      ... sorry... different context of g here. you see, the letters g , q , and v were not taken with vi so they could be used for more. while q was used
      Message 2 of 6 , Nov 1, 2011
      • 0 Attachment
        * Jose Caballero <jcaballero.hep@...> [2011-11-02 05:25]:
        > I had a line like this one
        > ===================
        > and I wanted to replace all characters by '-',
        > so I could have something like
        > --------------------
        >
        > I thought I could do it by combining 'g' and 'r' as I understood 'g' is
        > good to repeat the same command over all chars in a line (like guu or gUU).

        sorry... different context of 'g' here.

        you see, the letters 'g', 'q', and v' were not taken with vi
        so they could be used for more. while 'q' was used for
        "macro recording" and the Vs ('v', 'V', and CTRL-V) were
        then used for the three visual modes, the 'g' was simply used
        as a prefix for more added command. no "global" in it at all.

        the ":g/RE/cmd" command is to be read as
        "go through all lines of the buffer,
        tag each line which contains a match to the regular expression,
        and then apply the following command to each line in turn."

        a common command is 'p' for "print", so it read ":g/RE/p" in short.
        by the way.. this is where the "grep command comes from..

        and then ther is the 'p' *flag* to substitutions which means
        "do not stop after the first match in each line,
        but *continue* searching the line for more matches",
        ie "global" for each line.

        so the ":g" command typically goes "globally" through a
        buffer while the 'p' flag makes substitution command go
        "globally" through each line, looking for more matches.

        > However, I was not able to make it work.
        > I ended up doing something like :.s/=/-/g

        perfect! :)

        nitpick: the '.' for "current line" can be left out.
        so ":s/foo/bar/g" is the way to go.

        > Is not really possible to replace all chars in the current
        > line with a combination of 'g' and 'r' commands?

        in short: naaah. ;)

        the 'r' command is a command of the visual mode -
        and can only be used through a ":normal" command.
        but that's a lot of hassle - and probably slow, too.

        Sven

        --
        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
      • Tony Mechelynck
        ... In Normal mode, IIUC g is not a complete command, but the first character of quite a number of multikey commands, see :help g To replace all characters in
        Message 3 of 6 , Nov 1, 2011
        • 0 Attachment
          On 02/11/11 05:21, Jose Caballero wrote:
          > Hi,
          >
          > this happened to me today. I had a line like this one
          > ===================
          > and I wanted to replace all characters by '-', so I could have something
          > like
          > --------------------
          >
          > I thought I could do it by combining 'g' and 'r' as I understood 'g' is
          > good to repeat the same command over all chars in a line (like guu or gUU).
          > However, I was not able to make it work.
          > I ended up doing something like :.s/=/-/g
          >
          > Is not really possible to replace all chars in the current line with a
          > combination of 'g' and 'r' commands?
          >
          >
          > Cheers,
          > Jose
          >

          In Normal mode, IIUC g is not a complete command, but the first
          character of quite a number of multikey commands, see :help g

          To replace all characters in a line, I would use the :s[ubstitute]
          command, as you ended up doing, or one of its variants:

          To underline a (left-justified) heading:

          yyp

          then

          :s/./-/g

          The . range is optional, since the default for the :s command is the
          current line. However the above method wouldn't work if there are hard
          tabs in the line.


          Best regards,
          Tony.
          --
          Celestial navigation is based on the premise that the Earth is the
          center of the universe. The premise is wrong, but the navigation
          works. An incorrect model can be a useful tool.
          -- Kelvin Throop III

          --
          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
        • Gary Johnson
          ... Using g, ... Using r, 0v$r- Or did you mean something else? Regards, Gary -- You received this message from the vim_use maillist. Do not top-post! Type
          Message 4 of 6 , Nov 1, 2011
          • 0 Attachment
            On 2011-11-02, Jose Caballero wrote:
            > Hi,
            >
            > this happened to me today. I had a line like this one
            > ===================
            > and I wanted to replace all characters by '-', so I could have something like
            > --------------------
            >
            > I thought I could do it by combining 'g' and 'r' as I understood 'g' is good to
            > repeat the same command over all chars in a line (like guu or gUU).
            > However, I was not able to make it work.
            > I ended up doing something like :.s/=/-/g
            >
            > Is not really possible to replace all chars in the current line with a
            > combination of 'g' and 'r' commands?

            Using g,

            :s/./-/g

            Using r,

            0v$r-

            Or did you mean something else?

            Regards,
            Gary

            --
            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
          • Jose Caballero
            Thanks a lot everyone for your comments and tips. Jose -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the
            Message 5 of 6 , Nov 2, 2011
            • 0 Attachment
              Thanks a lot everyone for your comments and tips.
              Jose

              --
              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
            • AK
              ... In this particular case (no leading indent), I think Vr- is the easiest command, much easier than :s. Incidentally, I have a visual mode mapping to select
              Message 6 of 6 , Nov 2, 2011
              • 0 Attachment
                On 11/02/2011 12:43 AM, Gary Johnson wrote:
                > On 2011-11-02, Jose Caballero wrote:
                >> Hi,
                >>
                >> this happened to me today. I had a line like this one
                >> ===================
                >> and I wanted to replace all characters by '-', so I could have something like
                >> --------------------
                >>
                >> I thought I could do it by combining 'g' and 'r' as I understood 'g' is good to
                >> repeat the same command over all chars in a line (like guu or gUU).
                >> However, I was not able to make it work.
                >> I ended up doing something like :.s/=/-/g
                >>
                >> Is not really possible to replace all chars in the current line with a
                >> combination of 'g' and 'r' commands?
                >
                > Using g,
                >
                > :s/./-/g
                >
                > Using r,
                >
                > 0v$r-
                >
                > Or did you mean something else?
                >
                > Regards,
                > Gary
                >


                In this particular case (no leading indent),
                I think Vr- is the easiest command, much easier
                than :s. Incidentally, I have a visual mode
                mapping to select only the text in current line
                and it's often very useful, and I'd use it in
                this case if the line was indented.

                -ak

                --
                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
              Your message has been successfully submitted and would be delivered to recipients shortly.