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

Re: function to apply multiple whole-file substitutions

Expand Messages
  • Gary Johnson
    ... %s/ t/ /g A leading colon is OK but not necessary. See ... In a Vim regular expression, a tab can be represented as t. See ... Depending on why and how
    Message 1 of 6 , Nov 1, 2011
    • 0 Attachment
      On 2011-11-01, eda wizard wrote:

      > function Scrub ()
      > :%s// /g
      > :%s/\s*$//g
      > endfunction
      > map :call Scrub ()

      > Hm, somehow things got left out of my post:
      >
      > first substitution is to replace every TAB character in the file
      > with two spaces.

      %s/\t/ /g

      A leading colon is OK but not necessary. See

      :help 41.1

      In a Vim regular expression, a tab can be represented as \t. See

      :help /\t

      Depending on why and how you want to replace tabs by spaces, you may
      want to take a look at

      :help :retab
      :help 'expandtab'

      > second substitution is to match lines containing only whitespace
      > characters and remove all said characters leaving only empty
      > lines.

      %s/^\s\+$//

      See

      :help /^
      :help /\+

      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
    • Tony Mechelynck
      ... Like Sven, I m not sure what you actually want to do — it wouldhave been simpler if you had just told us that, in plain English. If you want to delete
      Message 2 of 6 , Nov 1, 2011
      • 0 Attachment
        On 02/11/11 03:53, eda wizard wrote:
        > Greetings all,
        >
        > I'm trying to craft a function to apply a series of pattern-matching
        > substitutions to the file. So far I've got this in my .vimrc
        >
        > function Scrub ()
        > :%s// /g
        > :%s/\s*$//g
        > endfunction
        > map :call Scrub ()
        >
        > but it's not working, seems to complain about lack of line-endings.
        > Would someone help me out please?
        >
        >
        >
        > Trick-or-treat,
        >
        > Still-learning Steve

        Like Sven, I'm not sure what you actually want to do — it wouldhave been
        simpler if you had just told us that, in plain English.

        If you want to delete all end-of-line spaces, a simple substitute will
        do the trick:

        :%s/\s*$//

        or even

        :%s/\s\+$//e

        the e flag avoids an error message if there are zero substitutions in
        the whole file (none of your lines ends in whitespace). It is not
        necessary in the first case because that will match on every line,
        matching zero or more whitespace, as many as possible.

        Also, what _exact_ error message do you get?

        And there seems not to be a {lhs} in your mapping: after correcting your
        function, you should rewrite the mapping as

        :map <F4> :call Scrub()<CR>

        — with <F4> or anything else as the first space-separated aregument,
        meaning "what you hit to trigger the mapping", and <CR> at the end
        meaning "the mapping generates an Enter key at the end" — the omission
        of the <CR> at the end might be the reason why Vim complains about a
        missing end-of-line, but I cannot be sure without the exact error message.


        Best regards,
        Tony.
        --
        Ray's Rule of Precision:
        Measure with a micrometer. Mark with chalk. Cut with an axe.

        --
        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
      • Tim Chase
        ... Just a little correction: it searches for whatever you searched for last. (referenced in :help g& and more explicitly above :help complex-repeat ) -tim
        Message 3 of 6 , Nov 2, 2011
        • 0 Attachment
          On 11/01/11 22:24, Sven Guckes wrote:
          > * eda wizard<edawizard@...> [2011-11-02 04:17]:
          >> function Scrub ()
          >> :%s// /g
          >> :%s/\s*$//g
          >> endfunction
          >> map :call Scrub ()
          >>
          >> but it's not working,
          >
          > the first substitution searches for *nothing*,

          Just a little correction: it searches for whatever you searched
          for last. (referenced in ":help g&" and more explicitly above
          ":help complex-repeat")

          -tim



          --
          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.