On Apr 30, 9:36 am, Tim Chase <[email protected]> wrote: > On 04/30/2011 10:39 AM, Bee wrote: > > > On Apr 29, 4:47 pm, "John Beckett"<[email protected]> wrote: > >> Tim Chase wrote: > >>> let s=substitute(s, '\w\+', '\u\1', 'g') > > >> The above is intended to change each word in s, making the first > >> letter uppercase and not changing the rest. > > >> The search pattern needs brackets, or the \1 should be replaced. > >> The following works: > > >> let s=substitute(s, '\w\+', '\u&', 'g') > > > How would I pass the visual selection to a function with this > > substitute? > > If you just want to do the replacement over the selected text, > you can do the literal substitute: > > :'<,'>s/\%V\w\+\%V/\u&/g > > or, if your selection starts/ends in the middle of a word and you > don't want to effect them, you can riff on > > :'<,'>s/\%V\<\w\+\>\%V/\u&/g > > adding/removing "\<" and "\>" as desired. Any of these can > pretty easily be mapped, e.g. > > :vnoremap <f4> :s/\%V\w\+\%V/\u&/g<cr> > > If you want to take the selected text, pass it to this > transforming function, and then assign it to a variable without > altering the in-line text, you'll have to grab the selection > either by yanking it to a register, or by using getline() or some > such function (likely in a loop). That gets a bit messy if you > don't yank it to a known register. > > -tim > > PS: yeah, John caught my "just a little change after I copy & > paste" flub where I previously had used \(...\) to capture things > and \1 referred to them. Sorry if it caused any confusion.
I already have the following which works great: " Title Case A Line Or Selection vnoremap <F6> :s/\%V\<\(\w\)\(\w*\)\>/\u\1\L\2/ge<cr> But I would like to know how to do it in a "function" for the educational value. function! TitleCase() let save = @r normal! gv"rx let @r = substitute(@r, '\(\w\)\(\w*\)', '\u\1\L\2', 'g') normal! "rP`< let @r = save endfun This also works, but seems too "messy". Can it be simplified? -Bill -- 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
