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

126801Re: function to apply multiple whole-file substitutions

Expand Messages
  • Tony Mechelynck
    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
    • Show all 6 messages in this topic