1

As requested I formulated my question again.

I want to change non title-case to title-case of all words within brackets {}

In order to realize that I want to match all words within brackets but I don't know where to place [^}] in the regex.

This is my search regex but it doesn't select single words within brackets but the entire text within brackets

/{\s*\zs\(\(\<\w[^}]\+\>\ze\)\s*\)\+} 

Whats wrong in my regex?

It would be even better if the regex matches only the non-title case words within brackets but this makes it even more complicated.

2 Answers 2

2

To match a single word inside brackets you must not use \zs/\ze because new match may only start after the end of previous one and \zs/\ze are not taken into account when computing start/end of the match (they are only taken into account for computing matched text and the position where to place cursor). You should have used look-ahead and look-behind instead:

\%({[^}]*\)\@<=\<\l[^ }]\{-}\>\%([^}]*}\)\@= 

. But to change non-title case to title-case there is much simpler approach: rather then trying to construct regex that would match every word inside braces separately match the whole braces content and use nested substitution:

:s/{\zs.\{-}\ze}/\=substitute(submatch(0), '\<\l', '\U\0', 'g')/g 

.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you very much ZyX. I changed the substitution regex a bit in order to change the whole match to lowercase and the 1st letter to uppercase: %s/{\zs.\{-}\ze}/\=substitute(submatch(0), '\(\<.\)\(\S*\>\)', '\u\1\L\2', 'g')/gc However I prefer to use the look ahead regex because I prefer to confirm word by word. The one above crashes my vim. Something doesn't work.
ZyX, can't make your look-ahead and look-behind work. Somethings just doesn't work. Do you have any idea?
@Remonn Crashes? What vim version are you using? Try to update vim or do set re=1 before using my regex: it is likely problem with some vim version between the time when new regex engine was merged and the time when it became stable enough not to crash on some patterns.
@Remonn Thus it is a bug with old regex engine, not new (new was merged at patch 7XX, 8XX or 9XX, do not remember exactly). And in fact with 7.4b and my regex it freezes if I do set re=1. Not as bad as a crash, but still no good. I have simplified it now so that it works fine in both engines.
By the way, this occurs to be the first time I saw some profit from the new engine.
|
2

Try a Normal Map like:

:noremap tc /{<CR>v%:s/\v%V<(\w)(\w*)>%V/\=toupper( submatch(1) ) . tolower( submatch(2) )/g<CR>/}<CR> 

How does it work?

/{<CR> :: Searches an opening curly brace.

v% :: Visual select the content from current position (opening curly brace) until the closing one.

:s/.../g<CR> :: Substitute command that inside visual selection(%V) title-case words doing two groups, the first letter and the rest.

/}<CR> :: After substitution command, if cursor comes from a different line its position is set at the beginning of line, so to avoid repeat the map against same text, set its position after closing curly brace to find next pair in following execution.


I did a test with following text:

Topic One ========= This is some text about topic one. It has {multiple paragraphs.} more more mroe Topic Two ========= This {is some text about topic} two. It {has only one} paragraph. 

And executing tc command in Normal Mode three times, it yields:

Topic One ========= This is some text about topic one. It has {Multiple Paragraphs.} more more mroe Topic Two ========= This {Is Some Text About Topic} two. It {Has Only One} paragraph. 

8 Comments

Thank you very much! That does exactly what I want. I adapted the 'g' to 'gc' to confirm every change. Just one thing.. Is it possible to applicate all in a substitute command in order to use it in a function (and to avoid having to learn more maps)?
and to use it also for brackets only in a selection.
@Remonn: For the first one, do you mean the content of all the curly braces at the same time? And for the second one, I don't understand it, which selection?
Birei, your 1st question: yes (using the confirm flag) - your 2nd question: normally I use normal mode and visual mode to do a substitution. Visual mode:I select a few lines and apply a substitute command on it.
I like your solution a lot especially since a substitute that contains an expression \= doesn't work recursively!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.