Rename refactor in VIM

Rename refactor using VIM

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 the files in your project. VIM does not have any direct way of doing this, which is really very upsetting if you want to use VIM as your main IDE. But there are solutions…

TL;DR

:set hidden
:argadd “Your files”
:argdo silent! %s/old_variable_name/new_variable_name/ge
:argdo update

Argument list in VIM

The argument list feature is the way that VIM gives you to manipulate and group multiples files at once. You can visualize it as the next set of files that you will work with.
Using this argument list for renaming words start to make sense since you want to apply an action (renaming) among multiple files (Argument list). In this article I will expose how to achieve rename refactor using the commands argdo, argadd, and args.
Lets first see what each of those command does
  1. argadd [file] : Adds the file to the argument set. Note that there is opposite command argdelete
  2. args : It list the files in the argument set.
  3. argdo [cmd] : This is where the fun start, this command let you execute a command in each of the fiels

Rename refactor in action

Suppose I have three C source code files A.c, B.c, and C.c , each of the with the same content:
// Dummy content
int a;
int b;
Let say that I want to change the variable a to in the file A.cand B.cbut not C.c. The following animated GIF illustrates the process:
Renaming refactor in VIM

Some notes

  1. I used :set hidden to not to close the buffers when I change the current buffer in the window. https://medium.com/usevim/vim-101-set-hidden-f78800142855
  2. I used :silent! before each of the :argdo command to not to display the output of the command.
  3. I used :update (well.. :silent! argdo update)to save the buffers to disk. By default I would just keep the buffers opened (Since we are using :set hidden)without saving them.

Comments

Popular posts from this blog

Saber la temperatura de tu pc