Refactoring in Vim

Of course, the fact that you can reorganize to an IDE is invaluable to many, I almost never do this when I code, but I can try to do this by editing some other source. How to perform such a trivial task for multiple files in Vim?

I found this Ruby refactoring plugin , but what about โ€œanyโ€ language?

+82
vim refactoring
Jan 08 2018-12-12T00:
source share
13 answers

I agree with the "Vim is a IDE" paradigm. But there are times when there is no IDE. Here is what I use in situations like this:

: grep ,: vimgrep ,: Ag ,: Ggrep

Refactoring, which is more associated with regular replacements, I usually use : grep in my project tree, and then write a macro to refactor -: g and: s are not difficult. This usually allows me to quickly change a large number of files without much effort. Honestly, I use this method more than any other.

Inline commands may be slow / inconvenient depending on your workflow. If you use git, then you will want to use the excellent Fugitive plugin and its command :Ggrep only to search for files marked in git. I also like Silver Searcher for its speed.

: argdo ,: cdo, and: bufdo

: cdo and : argdo are convenient for executing vim commands on a set of files.

command line

When using :vimgrep determine the list of files requiring changes becomes more difficult, I resort to the grep / find command line commands to more accurately make a list of files that I need to reorganize. Save the list to a text file and use :e and a macro collage to make the necessary changes.

I find that the less rusty macro writing skills I have, the more useful I find Vim for refactoring: convenient saving / restoring from registers, increasing / decreasing variable counter counts, cleaning / saving macro entries to a file for later use, etc. .




Update

Since this video was written about the methods that I describe, have been published on vimcasts.org (I urge you to watch ALL Vimcasts! ). For refactoring see these:

Wimgolf is also a great way to practice.

The ubiquity of Language Server Protocol servers since I wrote this answer has also made refactoring possible in Vim (and other editors). In their opinion, they are far from comparing the refactoring capabilities that you would see in a specially created IDE (I use them and prefer coc and ALE). See other answers to this question for more information!

+66
Jan 09 2018-12-01T00:
source share

C-family

  • Try the Clighter plugin to rename-refactor for the c-family. It is based on clang, but there are limitations, and the plugin is marked as deprecated.

    Suggested Clighter Mapping -

     nmap <silent> <Leader>r :call clighter#Rename()<CR> 

    Note. The serial clighter8 plugin removed the rename function to commit 24927db42 .

  • If you are using neovim, you can take a look at the clamp plugin. This implies

     nmap <silent> <Leader>r :call ClampRename()<CR> 
+12
Aug 20 '14 at 6:55
source share

python

For Python, the following plugins provide smart renaming options for vim:

  • jedi-vim ( github ) <leader>r
  • ropevim ( github ) CTRL-c rr
  • python-mode ( github ) :h pymode-rope-refactoring
+12
Aug 21 '15 at 12:24
source share

Language Server Protocol (LSP)

The language server protocol contains an intelligent character renaming function:

https://microsoft.imtqy.com/language-server-protocol/specification#textDocument_rename

For example, the following language server supports this:

The Vim Editor client is required to use them in Vim. The options are as follows:

  1. LanguageClient-neovim (requires rust) offers the following mapping:

     nnoremap <silent> <F2> :call LanguageClient_textDocument_rename()<CR> 
  2. coc.nvim (node.js required) offers the following mapping:

     " Remap for rename current word nmap <leader>rn <Plug>(coc-rename) 
  3. vim-lsp provides the following command

     :LspRename 

    No matching is offered. However, of course, you can define one of the following

     nmap <leader>r <plug>(lsp-rename) 

    ( <leader>r should be replaced by your choice; I don't know the one that most plugins agree with)

  4. vim-lsc default mapping:

     'Rename': 'gR' 

Neovim received a pending (April 2019) request for lsp support out of the box:

https://github.com/neovim/neovim/pull/6856

I do not know if there are plans for the lsp protocol to support more complex refactoring, such as changing the structure of a class, adding parameters to methods / functions, or moving a method to another class. For a list of refactoring, see https://refactoring.com/catalog/ .

+8
Jul 27 '17 at 11:04 on
source share

I wrote this plugin for general refactoring. It still requires a lot of improvements. Sometime in the future, I will try to abandon ctags in favor of clang for refactoring C & C ++.

+5
Mar 25 '13 at 13:52
source share

This may not be the most elegant solution, but it was very convenient for me: I use ECLIM to connect VIM and Eclipse. Of course, all the editing of the source code was done in VIM, but when the time for reorganization came, you can use the excellent features of Eclipse in this matter.

Give it a try.

+4
May 09 '13 at 18:14
source share

Plugin Factor

There is another vim plugin for refactoring called factorus, which is available on github .

Currently (2017-12), it supports languages

  • from,
  • java and
  • python.
+3
Jul 27 '17 at 10:57
source share

YouCompleteMe Plugin (YCM) (15K Stars on github)

:h RefactorRename-new-name

RefactorRename <new name>

In supported file types, this command attempts to semantically rename the identifier under the cursor. This includes renaming declarations, definitions, and using an identifier, or any other action that matches the language. The specific behavior is determined by the semantic mechanism used.

Like | FixIt |, this command applies automatic changes to your source files. Rename operations may include changes to several files that may or may not open in Vim buffers at this time. YouCompleteMe handles all this for you. The behavior is described in the next section.

Supported in file types: "javascript" (variables only), "typcript"

By default, no mapping. But, of course, you can define one if you want.

+2
Dec 06 '17 at 13:03 on
source share

I write a lot of C / C ++ code in vim. The most common refactoring I do is rename variables, class names, etc. I usually use :bufdo :%s/source/dest/g to search / replace in files, which is almost the same as the renaming provided by large IDEs.
However, in my case, I found that I usually rename similar objects recorded in different cases (for example, CamelCase, snake_case, etc.), so I decided to write a small utility to help with a similar search / replace, it is posted here . This is a command line utility, not a vim plugin, I hope you find it useful.

+1
Nov 02 '13 at 20:59
source share

For refactoring, if you use Unite (and you need it), you can use vim-qfreplace and make it extremely simple. Check out this video demonstrating how it works. Once your workflow is installed, you can do some mappings to optimize it (instead of printing most things like in the video).

0
Oct 17 '14 at 22:33
source share

A combination of two plugins: vim-ripgrep , for searching files and placing results in the quick fix window, and a quickfix reflector for saving changes directly in the quick fix window and automatically saving each change in all files.

0
Jul 18 '17 at 5:23
source share

I would like to consider using Emacs for Spacemacs. It uses the same modes and most keystrokes as Vim, but has many more add-ons due to its nature. If you want to program in C ++, just add a C ++ layer and most of the IDE is already set up for you. For other interpreted languages, such as python or bash, you do not need to leave spaces to use them. They even have a way to run blocks of code directly inside your text, which works great for literate programming or reproducible programming, where the code and data are in the same file. Both are made as text.

Spacemacs is much harder on boot, but the extra things you can do with it are a few seconds of startup cost. One layer of org mode is worth checking. This is the best planner, programmer, daily timer / task list I have ever used.

0
Apr 08
source share

Place the cursor on the name for refactoring and type

gd (or gD if you are refactoring a global variable).

then

cgn new_name esc

as well as

. one or more times to refactor the next occurrence

or

:% norm. to refactor all occurrences in the buffer at the same time.

-one
Nov 18 '18 at 0:12
source share



All Articles