Thursday, November 24, 2011

RE: Substitution of text in keywords

Hi Christian,

Thanks for the info...

If I understand you correctly you're saying I should remove the
exclamation mark and add an 'e' to the end of each line, e.g:

:silent %s,\<and\>,And,ge

Also, the syntax file I'm using has lots of syntax keyword groups and an
individual item may exist in any one of them. However, what I'm trying
to achieve is not to change items which are between quotes - can you
advise what the syntax should be for this?

Thanks
Dave

-----Original Message-----
From: vim_use@googlegroups.com [mailto:vim_use@googlegroups.com] On
Behalf Of Christian Brabandt
Sent: 24 November 2011 11:21
To: vim_use@googlegroups.com
Subject: 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


*****************************************************************************
This e-mail contains confidential information and is intended solely for use by the individual named or entity to whom it is addressed. Please notify the sender and postmaster@aviagen.com immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the named addressee, you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. Aviagen accepts no liability for the content of this e-mail and any consequences of actions taken in reliance on the contents, unless that information is subsequently confirmed in writing. Any views, opinions or actions presented in this e-mail are solely the author's and have not been approved by Aviagen. Any defamatory statements or infringing communication is contrary to Aviagen policy and outside the scope of the employment of the individual concerned. No employee or agent is authorized to conclude any binding agreement on behalf of Aviagen with another party by e-mail. Aviagen has taken reasonable precautions to ensure no viruses are present in this e-mail, but cannot accept responsibility for any loss or damage arising from the use of this e-mail or attachments.
*****************************************************************************

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

Post a Comment