Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vim: MovingAmongCamelCaseWords

0 views
Skip to first unread message

Nick Kocharhook

unread,
Aug 15, 2003, 8:03:47 PM8/15/03
to
I just saw someone using CodeWarrior and he pressed Control and used
arrow keys to move among CamelCase words-within-words. Shift could be
employed to select, as well.

I'm wondering if anything similar is available for Vim. I know the
code I write is chock full of camel case words, partly because the
framework I typically use employs them. Is there a built-in object or
a script that would define that?

Any help is appreciated.

Srinath Avadhanula

unread,
Aug 15, 2003, 9:33:01 PM8/15/03
to Nick Kocharhook
You could map to search for the next Upper case character.

map <C-left> :call search('[A-Z]', 'W')<CR>
map <C-right> :call search('[A-Z]', 'bW')<CR>

(using search() instead of / because it will not screw with the search
history).

HTH
Srinath

Joseph Van Valen

unread,
Aug 18, 2003, 11:30:32 AM8/18/03
to
These mappings work to move to the next capital, but is there a way to
include word boundaries in this?

For instance, if I have the statement:

someVarName = someOtherVarName;

I would like the 's' of 'some' to be included in the search along with the
upper case characters in the above expression.

I tried replacing the search string in the mapping with this search command

:call search('\(\<[a-zA-Z0-9]\)\|\([A-Z]\)', 'W')

If I execute the above search function directly from the : prompt, it works,
but when mapping it to a key, the cursor doesn't move. I don't know why.

Does anybody have any ideas for this?

Thanks!
Joseph Van Valen

"Srinath Avadhanula" <srinath...@yahoo.com> wrote in message
news:Pine.WNT.4.50.0308151830390.1312-100000@bartok...

Joseph Van Valen

unread,
Aug 18, 2003, 11:46:57 AM8/18/03
to
Nevermind....

Wrapping the search in a function call seems to have done the trick.

function JvvCamelNext()
let eatThisResult = search('\(\<\a\)\|\(\u\)','W')
endfun

map ,w :call JvvCamelNext()

Joseph Van Valen

"Joseph Van Valen" <vsapsnspv...@saptsts.com> wrote in message
news:bhqrbq$33...@kcweb01.netnews.att.com...

Preben 'Peppe' Guldberg

unread,
Aug 18, 2003, 1:00:58 PM8/18/03
to
Joseph Van Valen wrote:
> Nevermind....

> Wrapping the search in a function call seems to have done the trick.

> function JvvCamelNext()
> let eatThisResult = search('\(\<\a\)\|\(\u\)','W')
> endfun

You don't like just :call'ing search do you? :-)

> map ,w :call JvvCamelNext()

Calling search() directly you could even avoid the function in your map
altogether. Don't know what could cause the problem you saw, though.

For some uses, I'd suspect you would want to match eg. leading "_"
characters and digits. For that I'd suggest a cleaned up version of

:call search('\<\w\|\u', 'W')

To just match at the start of words in general, you could even simplify
it as much as

:call search('\u\|\<', 'W')

Disregarding your option of not wrapping around the end of buffer, I'd
personally be more tempted by

:map ,w /\<\<Bar>\u/<CR>

as it would also work in visual mode.

Peppe
--
se nocp cpo=BceFsx!$ sh=/bin/sh hid bs=2 ic scs hls ai isf-== hi=100
filetype plugin indent on | syn on | se lz ls=2 ru so=4 noea
cno <C-A> <C-B>
omap S V/\n^-- $\\|\%$/<CR>

Joseph Van Valen

unread,
Aug 18, 2003, 1:36:48 PM8/18/03
to
Hello Peppe,

"Preben 'Peppe' Guldberg" </bin/v...@wielders.org> wrote in message
news:slrnbk21id.57q./bin/v...@peppe.wielders.org...


>
> You don't like just :call'ing search do you? :-)
>

I'm not quite sure what you mean by "calling search".

If you mean just typing the call search(... every time, then No, I dont :).

If you mean map ,w :call search(... , that was the first thing I tried. It
worked for searching for just caps, but when I tried to stick the word
boundary stuff in, it would only work directly and not when mapped to a key.
After killing some time with it, I thought I would stick it in a function to
see if that made a difference. Maybe I have some incompatible setting in my
_vimrc?

If you mean within the function
let eatThisResult = search(...

I tried that directly at first also but got an "This is not an editor
function" error.

I would have preferred map ,w : call search(... if it had worked.

>
> For some uses, I'd suspect you would want to match eg. leading "_"
> characters and digits. For that I'd suggest a cleaned up version of
>
> :call search('\<\w\|\u', 'W')
>
> To just match at the start of words in general, you could even simplify
> it as much as
>
> :call search('\u\|\<', 'W')
>
> Disregarding your option of not wrapping around the end of buffer, I'd
> personally be more tempted by
>
> :map ,w /\<\<Bar>\u/<CR>
>
> as it would also work in visual mode.
>
> Peppe

Thanks Peppe. I had planned to revisit this later today, but it looks like
you have covered it. I will definitely try your refinements.

Joseph Van Valen


Preben 'Peppe' Guldberg

unread,
Aug 18, 2003, 2:06:03 PM8/18/03
to
Joseph Van Valen wrote:
> Hello Peppe,

> "Preben 'Peppe' Guldberg" </bin/v...@wielders.org> wrote in message
> news:slrnbk21id.57q./bin/v...@peppe.wielders.org...

> > You don't like just :call'ing search do you? :-)


> I'm not quite sure what you mean by "calling search".

> If you mean just typing the call search(... every time, then No, I dont :).

> If you mean map ,w :call search(... , that was the first thing I tried. It
> worked for searching for just caps, but when I tried to stick the word
> boundary stuff in, it would only work directly and not when mapped to a key.
> After killing some time with it, I thought I would stick it in a function to
> see if that made a difference. Maybe I have some incompatible setting in my
> _vimrc?

It could be because of the special meaning of '|' on the command line
where eg.

:map ,w :call search('\<\|>\u', 'W')<CR>

Would result in a map of ":call search('\<|>\u', 'W')<CR>" where the '|'
is matched literally.

I'd prefer one of these, depending on my mood and whether it goes into
a vimrc or other file for later maintanence:

:map ,w :call search('\<\\|>\u', 'W')<CR>
:map ,w :call search('\<\<Bar>\u', 'W')<CR>

> If you mean within the function
> let eatThisResult = search(...

> I tried that directly at first also but got an "This is not an editor
> function" error.

This was what I was referring to. I'd write that invocation of search()
like this

call search(...)

without the assignment of the return code to a variable.

> I would have preferred map ,w : call search(... if it had worked.

Please check ":map ,w" with my hints above in mind.

Joseph Van Valen

unread,
Aug 18, 2003, 2:55:38 PM8/18/03
to
>
> It could be because of the special meaning of '|' on the command line
> where eg.
>
> :map ,w :call search('\<\|>\u', 'W')<CR>
>
> Would result in a map of ":call search('\<|>\u', 'W')<CR>" where the '|'
> is matched literally.
>

You hit the nail on the head! It was the '\|' on the command line. I would
have never guessed that. I've gotten rid of my functions and am using :call
search directly as desired.

> I'd prefer one of these, depending on my mood and whether it goes into
> a vimrc or other file for later maintanence:
>
> :map ,w :call search('\<\\|>\u', 'W')<CR>
> :map ,w :call search('\<\<Bar>\u', 'W')<CR>
>

I've chosen to use \<Bar> as in your second ',w' map, since that's the
uglier one and I am more likely to remember to use it the next time.

That's one great thing about VIM, there's always something new to learn.

Thanks for straightening me out!

Joseph Van Valen

"Preben 'Peppe' Guldberg" </bin/v...@wielders.org> wrote in message

news:slrnbk25ce.5jd./bin/v...@peppe.wielders.org...


> Joseph Van Valen wrote:
> > Hello Peppe,
>
> > "Preben 'Peppe' Guldberg" </bin/v...@wielders.org> wrote in message
> > news:slrnbk21id.57q./bin/v...@peppe.wielders.org...
>
> > > You don't like just :call'ing search do you? :-)
>
>
> > I'm not quite sure what you mean by "calling search".
>
> > If you mean just typing the call search(... every time, then No, I dont
:).
>
> > If you mean map ,w :call search(... , that was the first thing I
tried. It
> > worked for searching for just caps, but when I tried to stick the word
> > boundary stuff in, it would only work directly and not when mapped to a
key.
> > After killing some time with it, I thought I would stick it in a
function to
> > see if that made a difference. Maybe I have some incompatible setting
in my
> > _vimrc?

Bob X

unread,
Aug 24, 2003, 2:58:21 PM8/24/03
to
Nick Kocharhook wrote:

I like this but I am not sure if it is what you are looking for:

http://vim.sourceforge.net/scripts/script.php?script_id=273

0 new messages