Emacs Rename Variable

那年仲夏 提交于 2019-12-03 08:26:46

问题


How do I rename a variable in emacs? Eclipse has a neat "rename" refactoring action which lets you rename a variable in a scoping-aware way, which can be much easier to use than doing localized replace-strings, especially if the variable name is a character like e. Does emacs have a similar functionality built in?


回答1:


iedit was made for this kind of thing.




回答2:


New Emacs has M-s . to select symbol under cursor, then you can C-M-% and it will use currently selected symbol to perform replacements. NOTE This is just plain string replacement, not like the IDE 'rename variable' feature.




回答3:


You can use narrowing to only show part of a buffer, and search/replace will only operate in the narrowed region. For example, you could use C-x n d to narrow to the current function, or select the region you want and do C-x n n. Do your search/replace, then widen back with C-x n w. For a single letter variable like e, do a query-replace-regexp with C-M-% and use a regexp like \be\b so it will only work on individual e's instead of ones inside other words.

Edit: Just thought of another thing. If you select a region, search/replace only works in that area. So you could just select the scope you want to replace in, then do the query-replace-regexp thing.




回答4:


In Python, this is more or less doable with the Rope refactoring library, for which I advise to use emacs-traad, in MELPA (straightforward to install and easy to use).

After installation we have the function M-x traad-rename which renames a variable in the project.

  • Rope documentation

For simpler search&replace, we have the aforementioned Iedit and also Projectile's projectile-replace.




回答5:


I'm not sure what your source code language is. Because you mentioned about Eclipse, I assume that it is Java. One option is to use tags-query-replace functionality. Use Excuberant Ctags with -e switch to generate etgas style tags and invoke tags-query-replace.




回答6:


As well as considering the already-suggested iedit, you can also consider multiple-cursors package. Check out an article about it, with animation of the live edition.




回答7:


Since you asked for a Eclipse feature, Iedit wont cut it. Its not that smart, what if you got two variables with the same name on different scopes? It would change both of them. This does not happen on eclipse! You will need language specific tool if you expect that kind of awareness.

With typescript you can use tide. With golang you can use go-doctor. Specifically with Java, I could not find anything, but I use meghanada, which is great. But refactoring is on its TODO list! You use also use emacs as a client for eclipse with eclim.




回答8:


Support for this kind of modern IDE feature is slowly being absorbed into Mesozoic editors such as Emacs. For Scala you can get this through Ensime.

http://ensime.github.io/editors/emacs/userguide/#rename




回答9:


(defun replace-var (new)
  "Replace the variable on the cursor"
  (interactive (list
            (read-string (format "Rename %s to: " (thing-at-point 'symbol)))))
  (let ((old (thing-at-point 'symbol)))
    (mark-defun)
    (replace-string old new)))

(defun replace-old-var (old new)
  "Input the old and new name"
  (interactive "sFrom: \nsTo: ")
  (mark-defun)
  (replace-string old new))

(global-set-key (kbd "C-c r o") 'replace-old-var)
(global-set-key (kbd "C-c r v") 'replace-var)


来源:https://stackoverflow.com/questions/9920884/emacs-rename-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!