The twist here, is that I'm using IdeaVim inside PhpStorm, so I'm looking for something simple, that works both in Vim (~/.vimrc
) and PhpStorm (~/.ideavimrc
).
I'm actually using Neovim (LazyVim), but I would like a solution that works in regular Vim if possible. This isn't a must, though.
The mission
Go to the next uppercase character, ideally using E
(shift+e) and B
(shift+b).
So for a word like this: theQuickBrownFoxJumpsOverTheLazyDog
.
Then I can set the cursor in the beginning of the word and type: EEE
(and then the cursor is at the F
).
Followed by Shift+e Shift+e h
(selecting FoxJumps
).
Press x
to delete it and then press i
(insert mode) and write a replacement.
Solution attempt 1: noremap
in ~/.vimrc
I tried this:
noremap <silent> E :set nohlsearch<CR>/\u<CR>:set nohlsearch<CR>
noremap <silent> B :set nohlsearch<CR>?\u<CR>:set nohlsearch<CR>
The :set nohlsearch
is because the files can be long, and it adds a delay, if it momentarily should select all uppercase letters, for large files.
That works in normal mode, for both Vim and PhpStorm (IdeaVim).
But I can't modify that, so it works in visual-mode.
Solution attempt 2: A function
I tried something like this:
function! CamelForward()
call search('\u', 'W')
endfunction
function! CamelBackward()
call search('\u', 'bW')
endfunction
nnoremap <silent> E :call CamelForward()<CR>
nnoremap <silent> B :call CamelBackward()<CR>
And that works in Vim, but not in PhpStorm (IdeaVim).
And even so, then the select-part doesn't work either, even if I add something like this:
vnoremap <silent> E :<C-u>call CamelForward()<CR>
vnoremap <silent> B :<C-u>call CamelBackward()<CR>
Solution attempt 3: Add a global toggle shortcut
It would also be a solution, if there is a global shortcut (such as <leader>c
that toggles "CamelCase mode" on and off. And if it's on, then e
and b
goes to previous/next uppercase letter.
But this involves a function as well, making it not work in IdeaVim.
And I also can't get the visual mode to work in regular Vim either.
E
andB
already do something out of the box and you want to override the default behavior for your "CamelCase mode"?