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

function for global text replacement

Expand Messages
  • eda wizard
    greetings all, I m trying to create a function in my .vimrc that will apply a series of global pattern-matching substitutions but am having some trouble.
    Message 1 of 4 , Nov 1, 2011
    • 0 Attachment
      greetings all,

      I'm trying to create a function in my .vimrc that will apply a series of global pattern-matching substitutions but am having some trouble.
      Here's what I've got so far:


      function Scrub ()
      :%s/<TAB>/ /g<CR>
      :%s/\s*$//g<CR>
      endfunction
      map <silent><F7> :call Scrub ()

      I'm probably not imputting the substitutions correctly. Would someone please help me out?


      TIA,


      Still-learning Steve

      --
      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
    • Taylor Hedberg
      ... Hi Steve, You didn t mention the exact problem you re having, but I can provide some general suggestions anyway: You don t need the trailing on the
      Message 2 of 4 , Nov 2, 2011
      • 0 Attachment
        eda wizard, Tue 2011-11-01 @ 20:02:56-0700:
        > greetings all,
        >
        > I'm trying to create a function in my .vimrc that will apply a series of global pattern-matching substitutions but am having some trouble.
        > Here's what I've got so far:
        >
        >
        > function Scrub ()
        > :%s/<TAB>/ /g<CR>
        > :%s/\s*$//g<CR>
        > endfunction
        > map <silent><F7> :call Scrub ()
        >
        > I'm probably not imputting the substitutions correctly. Would someone please help me out?
        >
        >
        > TIA,
        >
        >
        > Still-learning Steve

        Hi Steve,

        You didn't mention the exact problem you're having, but I can provide
        some general suggestions anyway:

        You don't need the trailing "<CR>" on the substitution lines (I assume
        those characters are typed literally and don't represent an actual
        carriage return character).

        You also don't need the leading colons, though I'm pretty sure they will
        just be ignored if you leave them in. The rule of thumb is, you type the
        colon when issuing a command interactively (i.e. within a Vim session),
        but it can be omitted from commands that are used in Vim scripts. Note
        that this does not apply to the ":call" in your map command; the colon
        is needed there because those characters will be executed as written
        when the mapping is executed.

        You might consider using `noremap` instead of `map` to avoid
        accidentally invoking a mapping recursively. It's a good idea to do this
        by default unless you really need recursive mapping. See `:help
        recursive_mapping` for more info.

        You should also put a space between the "<silent>" and the "<F7>", and
        omit the space between the function name and the parentheses that follow
        ("Scrub()" instead of "Scrub ()").

        If you're still having problems after following those suggestions, then
        if you let us know what's happening, we can work from there.

        --
        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
      • Ben Fritz
        ... Also: in a substitution command means nothing special. It will actually search for 5 characters,
        Message 3 of 4 , Nov 2, 2011
        • 0 Attachment
          On Nov 2, 8:23 am, Taylor Hedberg <tmhedb...@...> wrote:
          > eda wizard, Tue 2011-11-01 @ 20:02:56-0700:
          >
          >
          >
          >
          >
          > > greetings all,
          >
          > > I'm trying to create a function in my .vimrc that will apply a series of global pattern-matching substitutions but am having some trouble.
          > > Here's what I've got so far:
          >
          > > function Scrub ()
          > >   :%s/<TAB>/  /g<CR>
          > >   :%s/\s*$//g<CR>
          > > endfunction
          > > map <silent><F7> :call Scrub ()
          >
          > > I'm probably not imputting the substitutions correctly. Would someone please help me out?
          >
          > > TIA,
          >
          > > Still-learning Steve
          >
          > Hi Steve,
          >
          > You didn't mention the exact problem you're having, but I can provide
          > some general suggestions anyway:
          >
          > You don't need the trailing "<CR>" on the substitution lines (I assume
          > those characters are typed literally and don't represent an actual
          > carriage return character).
          >
          > You also don't need the leading colons, though I'm pretty sure they will
          > just be ignored if you leave them in. The rule of thumb is, you type the
          > colon when issuing a command interactively (i.e. within a Vim session),
          > but it can be omitted from commands that are used in Vim scripts. Note
          > that this does not apply to the ":call" in your map command; the colon
          > is needed there because those characters will be executed as written
          > when the mapping is executed.
          >
          > You might consider using `noremap` instead of `map` to avoid
          > accidentally invoking a mapping recursively. It's a good idea to do this
          > by default unless you really need recursive mapping. See `:help
          > recursive_mapping` for more info.
          >
          > You should also put a space between the "<silent>" and the "<F7>", and
          > omit the space between the function name and the parentheses that follow
          > ("Scrub()" instead of "Scrub ()").
          >

          Also:

          <Tab> in a substitution command means nothing special. It will
          actually search for 5 characters, '<' followed by 'T', then 'a', then
          'b', and finally '>'. You PROBABLY meant "seach for a literal tab
          character" which can be done either by inserting an actual tab
          character, or by using the special "\t" atom, i.e., %s/\t/ /g

          That said, your first substitute command would probably be better
          accomplished with a :retab command anyway.

          --
          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
        • eda wizard
          Thanks for replying and for your help, everyone! I ll give your suggestions a try this morning Thanks again, Still-learning Steve -- You received this message
          Message 4 of 4 , Nov 2, 2011
          • 0 Attachment
            Thanks for replying and for your help, everyone! I'll give your suggestions a try this morning


            Thanks again,

            Still-learning Steve

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