Wednesday, November 2, 2011

Re: function for global text replacement

On Nov 2, 8:23 am, Taylor Hedberg <tmhedb...@gmail.com> 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

No comments: