0

I am sorry that my English was not good enough to clearly describe the task in the title.

Here is a more descriptive attempt. Let us say I have the following text

\gls{tla}{TLA}

(As a side note, this is a partial excerpt from a latex document. This command is useful for defining acronyms and generating glossary later on.)

The question is the following: Let us say, I have copied the line immediately below with yyp.

\gls{tla}{TLA}
\gls{tla}{TLA}

I wish to change the second line to the following with a single edit operation.

\gls{tla}{TLA}
\gls{tbd}{TBD}

i.e. I wish to perform a substitution for the case-sensitive and case-insensitive version in one go. I believe this is possible with some knowledgeable vim-fu, since it involves only a single semantic transformation (and the case conversion can be seen as a side-effect/add-on operation that remain fixed in sequence.)

Any ideas on how to achieve this? I am not opposed to the use of plugins to aid this, but standalone & elegant pure vim-only solutions (with some very-magic regex) will also be highly appreciated.

  • 3
    I know you said you prefer a non-plugin solution, but this sounds like a prime use-case of github.com/tpope/vim-abolish – Mass Apr 8 '18 at 21:03
  • @Mass, cool. I just tried and that indeed did work! . In the meantime, I have to do about 70 of these glossary terms in my thesis. I am new to vim. Can you suggest a vimscript function that will relieve the burden from manually specifying the word to substitute. I just want to keep the structure \gls{something}{SOMETHING} and keep moving on with my definitions. – Krishna Apr 8 '18 at 21:17
3

I will assume you are starting with the following, i.e., after changing all the first arguments using any text editing method (e.g., visual block mode).

\gls{tbd}{TLA}
\gls{foo}{TLA}
\gls{bar}{TLA}

Now, all we need to do is make the second argument match. A simple way to do this is:

:%s/\\gls{\(\w\+\)}{\zs\w*\ze}/\=toupper(submatch(1))

Breaking it down, we look for \gls{word}{..} and capture the word into group \1. Targeting only the second argument using \zs..\ze, we replace with a transformed version of the word (\= allows us to use an expression for replacement).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.