How to capitalize the first letter of each word?

104 views
Skip to first unread message

Peng Yu

unread,
Feb 8, 2014, 9:53:05 AM2/8/14
to vim_use
Hi,

I need to capitalize the first letter of each word within a visual
block. Is there a quick way to do it vim? Thanks.

--
Regards,
Peng

Tim Chase

unread,
Feb 8, 2014, 10:06:51 AM2/8/14
to vim...@googlegroups.com, peng...@gmail.com
On 2014-02-08 08:53, Peng Yu wrote:
> I need to capitalize the first letter of each word within a visual
> block. Is there a quick way to do it vim? Thanks.

You might try

:'<,'>s/\%V\<./\u&/g

which could be easily mapped if you do it frequently.

-tim



Peng Yu

unread,
Feb 8, 2014, 10:10:11 AM2/8/14
to Tim Chase, vim_use
>> I need to capitalize the first letter of each word within a visual
>> block. Is there a quick way to do it vim? Thanks.
>
> You might try
>
> :'<,'>s/\%V\<./\u&/g
>
> which could be easily mapped if you do it frequently.

I'm not ':help \u' and ':help &' give me the correct help pages. Do
mind where to find help for them? Thanks.


--
Regards,
Peng

Tim Chase

unread,
Feb 8, 2014, 10:25:26 AM2/8/14
to vim...@googlegroups.com
On 2014-02-08 09:10, Peng Yu wrote:
> >> I need to capitalize the first letter of each word within a
> >> visual block. Is there a quick way to do it vim? Thanks.
> >
> > You might try
> >
> > :'<,'>s/\%V\<./\u&/g
>
> I'm not ':help \u' and ':help &' give me the correct help pages. Do
> mind where to find help for them? Thanks.

:help s/\&
:help s/\u

Strangely, the help-target ":help s/\U" (upper-case "U") takes you to
the same place as ":help s/\&" does rather than to the help on \U (at
least in 7.3.547 which is stock on Debian Stable). Fortunately
they're in the same block, but it looks like a bug in the
help-tagging.

-tim





Peng Yu

unread,
Feb 8, 2014, 10:45:39 AM2/8/14
to vim_use
> Strangely, the help-target ":help s/\U" (upper-case "U") takes you to
> the same place as ":help s/\&" does rather than to the help on \U (at
> least in 7.3.547 which is stock on Debian Stable). Fortunately
> they're in the same block, but it looks like a bug in the
> help-tagging.

I have vim 7.4 and have the same problems.

':help s/\&' brings me to *s/\~*.
':help s/\U' brings me to *s/\&*.

--
Regards,
Peng

Bee

unread,
Feb 8, 2014, 12:57:26 PM2/8/14
to vim...@googlegroups.com, peng...@gmail.com

I have been using:

" Titlecase A Line Or Selection
vmap \<F6> :s/\%V\<./\u&/ge<cr>
nmap \<F6> V\<F6>
imap \<F6> <C-O>\<F6>

I have incsearch set and would like to turn off highlighting after the search. I have a mapping to do this manually:

" manually disable search highlight
nmap <silent> <F4> :nohlsearch<cr>
imap <F4> <C-O><F4>
vmap <F4> <esc><F4>gv

I tried to combine the search replace with nohl but it did not work:

vmap \<F6> :s/\%V\<./\u&/ge<cr> | :nohlsearch

Is there a way to do this?

Tim Chase

unread,
Feb 8, 2014, 1:02:18 PM2/8/14
to vim...@googlegroups.com, peng...@gmail.com
Additionally, issuing any of

:help s/\1
:help s/\2
:help s/\9

gives me an

E65: Illegal back reference

but then proceeds to go to the correct help target. Issuing

:help s/\~

gives me

E33: No previous substitute regular expression
E149: Sorry, no help for s/\~

despite such a help-target in the aforementioned section.

I seem to recall Bram looking into this a while back and fixing it,
but it seems to have returned.

-tim




Jan Christoph Ebersbach

unread,
Feb 8, 2014, 2:37:13 PM2/8/14
to vim...@googlegroups.com
Hi,

Thanks for pointing out the very elegant solution of using :s to uppercase every first character. I created a plugin some time ago that would work for all scenarios except visual-block mode. Thanks to your answer, I was able to complete it. The result can be downloaded here: https://raw.github.com/jceb/vimrc/master/plugin/capitalize.vim

Cheers,

Jan Christoph

Bee

unread,
Feb 8, 2014, 4:25:32 PM2/8/14
to vim...@googlegroups.com, peng...@gmail.com

Thank you for this thread...

I decided to try other options,
such as changing only lowercase letters:

vmap \<F6> :s/\%V\<\l/\u&/ge<cr>

When the substitute is complete,
there are no longer any lowercase letters and
therefor no highlighting!

Also this seems to be useless:
imap \<F6> <C-O>\<F6>

Reply all
Reply to author
Forward
0 new messages