Thursday, November 24, 2011

Re: Substitution of text in keywords

On Thu, November 24, 2011 11:23 am, Clark, David wrote:
> Hello all...
>
> I've been using of Vim as my everyday editor for many years but have
> only recently started to program it to do repetitive things for me.
>
> I've written a function to standardise the case of keywords in my code,
> however when I run it it's not just the keywords that are being changed
> - text within quotes are also being changed e.g. in a message.
>
> Here's a snippet of my function:
>
> " create function to standardise code...
> function! StandardiseCode()
> :silent! %s,\s\+$,,
> :silent! %s,^{,Begin,
> :silent! %s,^},End,
> :silent! %s,\<activerow\>,ActiveRow,g
> :silent! %s,\<and\>,And,g
> :silent! %s, \<date\>, Date,g
> endfun

You don't need the ! for the silent call, you can use the
flag 'e' for the :s command.

>
> I have a syntax file that defines the keywords, comments etc for the
> code - is there a way to make the substitution work on just keywords?

Provided you have used something like this:
syn keyword MyKeyword And

you can then use something like this:
:%s/\<and\>/\=synIDattr(synID(line('.'),col('.'),1),'name')=~'MyKeyword'?'And':submatch(0)/g

(one line)
which uses the expression, to evaluate the syntax name found for the
keyword 'and' and only if this matches 'MyKeyword' it will be replaced by
'And' otherwise it will be replaced by itsself.

You can read about it at
:h sub-replace-\=
:h expr1

regards,
Christian

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