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

vim: want control character in substitution string

Expand Messages
  • Paul
    I have an record-based data file for an application that uses control characters in some fields as flags for certain GUI options, e.g.: ^@ ^A It s barbaric to
    Message 1 of 6 , Mar 11, 2013
    • 0 Attachment
      I have an record-based data file for an application that uses control
      characters in some fields as flags for certain GUI options, e.g.:

      ^@
      ^A

      It's barbaric to try and manipulate the data using the GUI, and I've
      taken to vim'ing the text file. However, it's rough going because the
      fields aren't vertically aligned. It would be much better if I could
      massage the data in Excel, but the text import process seems to ignore
      the control characters. I can use the vim command line to replace the
      control characters with their visual counterpart i.e. ^@ is replaced
      by carat and at-sign (I'll refer to these as fake control
      characters). After importing into Excel and mushing the data, I
      export it to text and use vim to clean it up, including converting the
      fake control characters back to real control characters.

      Therein lies my problem. When I use the command line, this works:

      :% s=^@=^@=g

      where the first ^@ is a fake control character while the second one is
      real, obtained by prefixing the keystroke with ctrl-V. However, when
      I try to put this command in a vim script that I can ":source", it is
      interpretted as an error.

      After much reading, I found that I can match ^@ with \%x00 in the
      search string, but I haven't found a way to specify control characters
      in the substitution string. Is there a way?

      --
      --
      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@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Ben Fritz
      ... I think replacing with n will work. See :help sub-replace-special. ^@ is special because Vim uses it internally to represent end-of-line. Most other
      Message 2 of 6 , Mar 11, 2013
      • 0 Attachment
        On Monday, March 11, 2013 3:05:53 PM UTC-5, Paul wrote:
        > I have an record-based data file for an application that uses control
        >
        > characters in some fields as flags for certain GUI options, e.g.:
        >
        >
        >
        > ^@
        >
        > ^A
        >
        >
        >
        > It's barbaric to try and manipulate the data using the GUI, and I've
        >
        > taken to vim'ing the text file. However, it's rough going because the
        >
        > fields aren't vertically aligned. It would be much better if I could
        >
        > massage the data in Excel, but the text import process seems to ignore
        >
        > the control characters. I can use the vim command line to replace the
        >
        > control characters with their visual counterpart i.e. ^@ is replaced
        >
        > by carat and at-sign (I'll refer to these as fake control
        >
        > characters). After importing into Excel and mushing the data, I
        >
        > export it to text and use vim to clean it up, including converting the
        >
        > fake control characters back to real control characters.
        >
        >
        >
        > Therein lies my problem. When I use the command line, this works:
        >
        >
        >
        > :% s=^@=^@=g
        >
        >
        >
        > where the first ^@ is a fake control character while the second one is
        >
        > real, obtained by prefixing the keystroke with ctrl-V. However, when
        >
        > I try to put this command in a vim script that I can ":source", it is
        >
        > interpretted as an error.
        >
        >
        >
        > After much reading, I found that I can match ^@ with \%x00 in the
        >
        > search string, but I haven't found a way to specify control characters
        >
        > in the substitution string. Is there a way?

        I think replacing with \n will work. See :help sub-replace-special. ^@ is special because Vim uses it internally to represent end-of-line. Most other control characters should work.

        --
        --
        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@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • Gary Johnson
        ... You can use the nr2char() function as a replacement expression, e.g., ... See ... HTH, Gary -- -- You received this message from the vim_use maillist. Do
        Message 3 of 6 , Mar 11, 2013
        • 0 Attachment
          On 2013-03-11, Paul wrote:

          > After much reading, I found that I can match ^@ with \%x00 in the
          > search string, but I haven't found a way to specify control characters
          > in the substitution string. Is there a way?

          You can use the nr2char() function as a replacement expression,
          e.g.,

          :%s/\^D/\=nr2char(4)/g

          See

          :help sub-replace-expression
          :help nr2char()

          HTH,
          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

          ---
          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@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Paul
          ... This is great! I learn about nr2char as well as = at the same time! As Ben said, however, ^@ seems special and nr2char(0) doesn t seem to stick a ^@ in
          Message 4 of 6 , Mar 12, 2013
          • 0 Attachment
            On Mar 11, 4:48 pm, Gary Johnson wrote:
            >On 2013-03-11, Paul wrote:
            >> After much reading, I found that I can match ^@ with \%x00 in the
            >> search string, but I haven't found a way to specify control
            >> characters in the substitution string. Is there a way?
            >
            > You can use the nr2char() function as a replacement expression,
            > e.g.,
            >
            > :%s/\^D/\=nr2char(4)/g
            >
            > See
            >
            > :help sub-replace-expression
            > :help nr2char()

            This is great! I learn about nr2char as well as \= at the same time!

            As Ben said, however, ^@ seems special and nr2char(0) doesn't seem to
            stick a ^@ in the substitution. However, his trick of using \n in the
            substitution string gets around that.

            Thanks!

            --
            --
            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@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • Charles Campbell
            ... Try reading ... Regards, C Campbell -- -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the text you are
            Message 5 of 6 , Mar 13, 2013
            • 0 Attachment
              Paul wrote:
              > <snip> but I haven't found a way to specify control characters
              > in the substitution string. Is there a way?
              >
              Try reading

              :help i_ctrl-v

              Regards,
              C Campbell

              --
              --
              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@....
              For more options, visit https://groups.google.com/groups/opt_out.
            • Paul
              ... Thanks, Charles. I mention ctrl-v in my original post. -- -- You received this message from the vim_use maillist. Do not top-post! Type your reply below
              Message 6 of 6 , Mar 14, 2013
              • 0 Attachment
                On Mar 13, 10:45 am, Charles Campbell wrote:
                > Paul wrote:
                >> <snip> but I haven't found a way to specify control characters
                >> in the substitution string. Is there a way?
                >
                > Try reading
                >
                > :help i_ctrl-v

                Thanks, Charles. I mention ctrl-v in my original post.

                --
                --
                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@....
                For more options, visit https://groups.google.com/groups/opt_out.
              Your message has been successfully submitted and would be delivered to recipients shortly.