What is the simplest way to rename variables in Vim, without adding any new packages? Is there a way without using regexps, too?

  • What do you mean by refactor? Based on your self-answer, you appear to mean rename, which is not what refactoring code is. – Herb Wolfe Nov 18 at 4:10
  • @HerbWolfe Renaming ⊈ refactoring? – Geremia Nov 18 at 4:25
  • A related question is stackoverflow.com/questions/8781975/refactoring-in-vim which also considers renaming part of refactoring. Anyhow, the stackoverflow answers (incl. the marked one) are more or less exclusively about renaming which the OP is interested in. – Hotschke Nov 18 at 9:29
up vote 6 down vote accepted

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 local
            variable, this command will jump to its declaration.
            First Vim searches for the start of the current
            function, just like "[[".  If it is not found the
            search stops in line 1.  If it is found, Vim goes back
            until a blank line is found.  From this position Vim
            searches for the keyword under the cursor, like with
            "*", but lines that look like a comment are ignored
            (see 'comments' option).
            Note that this is not guaranteed to work, Vim does not
            really check the syntax, it only searches for a match
            with the keyword.  If included files also need to be
            searched use the commands listed in |include-search|.
            After this command |n| searches forward for the next
            match (not backward).
            {not in Vi}

gD          Goto global Declaration.  When the cursor is on a
            global variable that is defined in the file, this
            command will jump to its declaration.  This works just
            like "gd", except that the search for the keyword
            always starts in line 1.  {not in Vi}


c (change) + gn new_name esc

gn         Search forward for the last used search pattern, like
            with `n`, and start Visual mode to select the match.
            If the cursor is on the match, visually selects it.
            If an operator is pending, operates on the match.
            E.g., "dgn" deletes the text of the next match.
            If Visual mode is active, extends the selection
            until the end of the next match.


. (repeat) one or more times to rename next occurrence(s)


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

  • Good instructions, but missing the relevant explanations. – D. Ben Knoble Nov 18 at 15:45
  • @D.BenKnoble What sort of explanations are you looking for? – Geremia Nov 19 at 17:00
  • Well newer users might not understand the sequence of commands you provided; i just think it would flesh out the answer so that one understands what’s happening without just rote mechanical repetition. I tend to cite :help for example, next to own re-worded explanation. – D. Ben Knoble Nov 19 at 17:10
  • That is, learning require understanding beyond memorization. – D. Ben Knoble Nov 19 at 17:10
  • @D.BenKnoble I've implemented your suggestion. thanks – Geremia Nov 19 at 18:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.