I am using Vim to read through a lot of C and Perl code containing many single letter variable names.
It would be nice to have some command to change the name of a variable to something more meaningful while I’m in the process of reading the code, so that I could read the rest of it faster.
Is there some command in Vim which could let me do this quickly?
I don’t think regexes would work because:
-
the same single letter name might have different purposes in different scoping blocks; and
-
the same combination of letters could be part of another longer variable name, a string literal, or a comment.
Are there any known solutions?
The following is how to rename a variable which is defined in the current scope
{}.gd. Which means – move the cursor to the definition.[{– this will bring you to the scope begin.V– will turn on Visual Line selection.%– will jump to the opposite}and thus will select the whole scope.:s/– start of the substitute command.<C-R>/– will insert a pattern that matches the variable name (that name you were on before pressinggd)./newname/gc<CR>– will initiate search and replace with confirmation on every match.Now you have to record a macro or even better – map a key.
Here are the final mappings:
Put this to your
.vimrcor just execute. After this pressinggron the local variable will bring you to:scommand where you should enternew_variable_nameand press Enter.