r/vim•
liquidcloud9

Fixing camel case typos?

Is there a way to fix capitalization with camel case? I'm aware of how to change the case of a word or line, but I'll often look at a line of code and realize I capitalized two letters in a row, like 'someVAriableNAme' that should be 'someVariableName'. Is there an easy fix for this that I've missed?

Posted Apr 19, 2022, 3:37 PM UTC

716
Another_moose•

~ toggles the capitalization of a single character. Not sure if that's what you're looking for?

6
Dummies102•

I’m not near a computer to test this but thinking something like :s/\([A-Z]\)\([A-Z]\)/\1\L\2/. You might need to fix some escapes

3
slapnuttz•

FWIW you can use \u instead of [A-Z] for "uppercase" so then \u\u would be uppercase uppercase

5
Dummies102•

Nice tip!

3
liquidcloud9OP•

Using these two suggestions, I came up with this: :s/\u\u/\u\L&/, which works for my use case. Thank you both!

2
rgnkn•

Either what u/Another_moose suggested or you might want to checkout https://github.com/kamykn/spelunker.vim

Maybe this helps.

If you repeat the misspelling - e.g. by the means of autocompletion - you could also move your cursor on one occurrence, type * and then :s//someVariableName/g or use some LSP solution.

2
[deleted]•

If you have Vim 8.2.0953 or newer (from June 2020) then you can use :set spelloptions=camel to spellcheck camel-cased words. For your someVAriableNAme example it marks VAriable and NAme as wrong, and you can correct it with the regular Vim spellcheck tools.

2
Schnarfmannnoremap gr gT•

Taking inspiration from comments below: I would write a regex like /\U\u\zs\u\ze\U\+ to highlight the second capital in a pattern of no-cap, cap, cap, no-cap. Then n~ to your hearts content.

Or in your example, fA~ would work. :help f is case sensitive. Startup time is significantly shorter, but there’s no .-repeatability.

1
vim-help-bot•

Help pages for:

  • f in motion.txt

^`:(h|help) <query>` | ^(about) ^(|) ^(mistake?) ^(|) ^(donate) ^(|) ^Reply 'rescan' to check the comment again ^(|) ^Reply 'stop' to stop getting replies to your comments

1
Schnarfmannnoremap gr gT•

:help \zs :help \U

1
vim-help-bot•

Help pages for:

  • /\zs in pattern.txt
  • /\U in pattern.txt

^`:(h|help) <query>` | ^(about) ^(|) ^(mistake?) ^(|) ^(donate) ^(|) ^Reply 'rescan' to check the comment again ^(|) ^Reply 'stop' to stop getting replies to your comments

1
TankorSmash•

Vim abolish, it lets you change between cases, so hitting crc should swap the case to camel case for you. Strongly recommend the plugin.

1
funbike•

This seems like something a linter can handle for you. Some Vim linter plugins will even fix it for you.

Check out ALE, Coc, or nvim-lspconfig.

You might also use autocomplete more, so you aren't re-typing in variable names.

0
McUsrII•

Seing the answer about tilde above, I figured the correct way to use the tilde for flipping case of one char to be ~l. I have set tildeop in my .vimrc, its not set by default (vim 8.2). Mapping: nnoremap ~ ~l gets back the old behavior.

1
liquidcloud9OP•

Oh definitely - Coc.nvim's autocomplete works great. In these cases, it's easy enough to do a global replace when an autocomplete entry clues me into the fact that I bungled the case.

I was hoping to save myself a few keystrokes for the times I write a line and notice my mistake. Figured I can't be the first person to be annoyed by this.

1
funbike•

I was hoping to save myself a few keystrokes for the times I write a line and notice my mistake. Figured I can't be the first person to be annoyed by this.

Coc can do that if you have it fully set up.

Using the example Coc key bindings, if I misspell a variable name, like your example, then one of the linters I use (coc-eslint, coc-tsserver) marks a line as incorrect. Typing the following will go to the issue, fix it, and return to where I was:

[g,qf<c-o>
  • [g - Jump to previous diagnostic error
  • <leader>qf - Fix the problem
  • <c-o> - Go to location before the jump

You can also configure Coc to auto-fix on save, but I don't like that. I like to have the negative feedback so I won't do it again.

3