Changing variable names in Vim

vim rename a variable
vim refactor
vim replace
vim rename under cursor
s in vim
vim gsub
vim use sed
replace word vim

I am trying to read a lot of C/Perl code through Vim which contain many single letter variable names.

It would be nice to have some command which could help me 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 quickly do this?

I don't think regexes would work because:

  1. the same single letter name might have different purposes in different scoping blocks

  2. the same combination of letters could be part of another larger var name, or in a string or comment... would not want to modify those

Any known solutions?


The following is how to rename a variable which is defined in the current scope {}.

Move your cursor to the variable usage. Press gd. Which means - move cursor to the definition. Now Press [{ - this will bring you to the scope begin. Press V - will turn on Visual Line selection. Press % - will jump to the opposite } thus will select the whole scope. Press :s/ - start of the substitute command. <C-R>/ - will insert pattern that match variable name (that name you were on before pressing gd). /newname/gc<CR> - will initiate search and replace with confirmation on every match.

Now you have to record a macros or even better - map a key.

Here are the final mappings:

" For local replace
nnoremap gr gd[{V%::s/<C-R>///gc<left><left><left>

" For global replace
nnoremap gR gD:%s/<C-R>///gc<left><left><left>

Put this to your .vimrc or just execute. After this pressing gr on the local variable will bring you to :s command where you simply should enter new_variable_name and press Enter.

Renaming variables, Place cursor at name to rename and type. gd (or gD if you're rename a global variable). gd Goto local Declaration. When the cursor is on a  When doing a rename with the intent of a refactor I usually take the following precautions: Limit the scope of the change my using marks. When entering the regex, bracket the name with \< and >. This will make it match an entire word which reduces the types of incorrect renames that Don't do a


AFAIK, there is no actual refactoring support in VIM. When doing a rename with the intent of a refactor I usually take the following precautions:

  1. Limit the scope of the change my using marks.
  2. When entering the regex, bracket the name with \< and >. This will make it match an entire word which reduces the types of incorrect renames that will occur.
  3. Don't do a multiline replace to reduce chances of a bad replace
  4. Look through the code diff carefully if it's anything other than a small change.

My end change looks something like this

:'a,'bs/\<foo\>/bar

I would love to be wrong about there not being a refactoring tool for VIM but I haven't seen it.

Best way to rename variable in several spots in a block of code? : vim, For developing, I thrash between using Eclipse and vim, which suit different This single visual mode binding is superior to several other plugins I could name. The coding style for one project may use CamelCase for variables, while another may use under_scores. Here are some Vim procedures to switch between CamelCase and under_score variable names.


Put this in your .vimrc

" Function to rename the variable under the cursor
function! Rnvar()
  let word_to_replace = expand("<cword>")
  let replacement = input("new name: ")
  execute '%s/\(\W\)' . word_to_replace . '\(\W\)/\1' . replacement . '\2/gc'
endfunction

Call it with :call Rnvar()

expand("<cword>") gets the word under the cursor. The search string uses % for file-scope, and the \(\W\) patterns look for non-word characters at the boundary of the word to replace, and save them in variables \1 and \2 so as to be re-inserted in the replacement pattern.

Rename refactor using VIM - Vicente Bolea, One of the most useful features when it comes to refactor is the ability to change a variable/class/namespace name through all or a subset of  Best way to rename variable in several spots in a block of code? For developing, I thrash between using Eclipse and vim, which suit different needs for different times for me. One thing that I find myself aching to do in vim that I do in Eclipse with the mouse is:


Changing variable names in Vim - vim - html, I am trying to read a lot of C/Perl code through Vim which contain many single letter variable names. It would be nice to have some command which could help​  Vim options echo 'tabstop is ' . &tabstop if &insertmode echo &g:option echo &l:option Prefix Vim options with & Operators a + b " numbers only! 'hello ' . name " concat let var -= 2 let var += 5 let var .= 'string' " concat Strings Strings


You could use the 'c' modifier in the global search and replace that would ask you for confirmation for each replace. It would take longer but it might work for a non-humongous code file:

%s/\$var/\$foo/gc

The c stands for confirm.

Converting variables to or from camel case | Vim Tips Wiki, Here are some Vim procedures to switch between CamelCase and under_score variable names. Converting PHP variables. This is a simple procedure to change​  Note: If those color schemes don’t seem to make any change to your vim session, try some of the other color schemes listed below, such as darkblue, evening, shine, etc. Specifying a colorscheme in your vimrc file. Once you’ve settled on a vim colorscheme that you like, you’ll want to configure that as the default colorscheme in your vim configuration file, vimrc.


usr_41.txt, The most often used ones are: b:name variable local to a buffer w:name A backslash is taken literally, thus you can't use it to change the meaning of the  Tip 47 Printable Monobook Previous Next created 2001 · complexity basic · version 6.0 Contents[show] Normal-mode commands Swap the current character (the character under the cursor) with the next: xp Swap the current character with the previous: Xp Swap the current line with the next (but see below for a better method): ddp Swap the current line with the previous: ddkP or ddkkp Swap the


Vimscript Style Guide, When submitting vim plugin code, you must adhere to these rules. Use :unlet for variables that may change types, particularly those assigned inside loops. which shares your plugin's name (with a "vim-" prefix or ".vim" suffix if desired). Vim has a way to increment a number automatically: <c-a> (or Ctrla) in normal mode. So, using a macro, it is fairly easy to construct an increasing sequence of numbers in Vim. Start at the line containing a+ 1 =b and a+1=c and: qq Yp^Aw^A q The ^A stands for pressing Ctrla. Then use @q to repeat the macro. So 4@q would produce:


Variables / Learn Vimscript the Hard Way, Variables. Up to this point we've covered single commands. For the next third of the Using an ampersand in front of a name tells Vim that you're referring to the option, Go through your ~/.vimrc file and change some of the set and setlocal  By default Vim uses a set of colors which, in my opinion, are overly bright and confusing (comments are colored the same as variable names!); in any case, everyone has their own opinions on color, so you will probably want to know how to change them to your own tastes.