Refactoring in Vim

前端 未结 15 1944
醉梦人生
醉梦人生 2021-01-29 17:44

Of course the fact that you can refactor on IDEs is priceless for many, I hardly ever do it when I am coding but I may try to do it when editing some one else\'s source. How do

相关标签:
15条回答
  • 2021-01-29 18:12

    Plugin YouCompleteMe (YCM) (20k stars on github)

    http://ycm-core.github.io/YouCompleteMe/#the-refactorrename-new-name-subcommand

    :h RefactorRename-new-name

    In supported file types, this command attempts to perform a semantic rename of the identifier under the cursor. This includes renaming declarations, definitions and usages of the identifier, or any other language-appropriate action. The specific behavior is defined by the semantic engine in use.

    Similar to FixIt, this command applies automatic modifications to your source files. Rename operations may involve changes to multiple files, which may or may not be open in Vim buffers at the time. YouCompleteMe handles all of this for you. The behavior is described in the following section.

    Supported in filetypes: c, cpp, objc, objcpp, cuda, java, javascript, typescript, rust, cs

    By default there is no mapping.

    0 讨论(0)
  • 2021-01-29 18:13

    I would consider using the spacemacs version of emacs. It is uses the same modes and most keystrokes as Vim but has many more add-on because of it's lisp nature. If you want to program in C++ you just add the c++ layer and most of the IDE is just set up for you already. For other interpreted languages like python or bash you do not need to leave spacemacs to use them. They even have a way to run blocks of code directly within your text which works fantastic for literate programming or reproducible programming where the code and the data are in the same file. Both done as text.

    Spacemacs is much more heavy handed in it's initial load but the additional stuff you can do with it is worth the few seconds of startup cost. One layer org-mode is worth checking it out. It is the best outliner, programmer, day timer / todo list I have ever used.

    0 讨论(0)
  • 2021-01-29 18:16

    C-Family

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

      Suggested mapping by Clighter is

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

      Note, the successor plugin clighter8 has removed the renaming functionality in the commit 24927db42.

    2. If you use neovim, you can take a look at the plugin clamp. It suggests

       nmap <silent> <Leader>r :call ClampRename()<CR>
      
    0 讨论(0)
  • 2021-01-29 18:16

    Place cursor at name to refactor and type

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

    Then

    cgn new_name esc

    and

    . one or more times to refactor next occurrence(s)

    or

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

    0 讨论(0)
  • 2021-01-29 18:17

    I wrote this plugin for generic refactoring. It still requires many improvements. Sometime in the future I'll try to abandon ctags in favour of clang for C&C++ refactorings.

    0 讨论(0)
  • 2021-01-29 18:17

    A combination of two plugins: vim-ripgrep, to find across files and put the results in the quickfix window, and quickfix-reflector to save the changes right in the quickfix window and have it automatically save each change across the files.

    0 讨论(0)
提交回复
热议问题