Renaming variables











up vote
3
down vote

favorite












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










share|improve this question
























  • 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
3
down vote

favorite












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










share|improve this question
























  • 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
3
down vote

favorite









up vote
3
down vote

favorite











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










share|improve this question















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







refactor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 4:34

























asked Nov 18 at 4:03









Geremia

1687




1687












  • 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


















  • 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
















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




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






@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




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










1 Answer
1






active

oldest

votes

















up vote
5
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}


Then



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.


and



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



or



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






share|improve this answer























  • 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
    2 days ago










  • 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
    2 days ago










  • That is, learning require understanding beyond memorization.
    – D. Ben Knoble
    2 days ago










  • @D.BenKnoble I've implemented your suggestion. thanks
    – Geremia
    2 days ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "599"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f18004%2frenaming-variables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
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}


Then



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.


and



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



or



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






share|improve this answer























  • 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
    2 days ago










  • 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
    2 days ago










  • That is, learning require understanding beyond memorization.
    – D. Ben Knoble
    2 days ago










  • @D.BenKnoble I've implemented your suggestion. thanks
    – Geremia
    2 days ago















up vote
5
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}


Then



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.


and



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



or



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






share|improve this answer























  • 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
    2 days ago










  • 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
    2 days ago










  • That is, learning require understanding beyond memorization.
    – D. Ben Knoble
    2 days ago










  • @D.BenKnoble I've implemented your suggestion. thanks
    – Geremia
    2 days ago













up vote
5
down vote



accepted







up vote
5
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}


Then



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.


and



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



or



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






share|improve this answer














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}


Then



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.


and



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



or



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







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered Nov 18 at 4:04









Geremia

1687




1687












  • 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
    2 days ago










  • 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
    2 days ago










  • That is, learning require understanding beyond memorization.
    – D. Ben Knoble
    2 days ago










  • @D.BenKnoble I've implemented your suggestion. thanks
    – Geremia
    2 days ago


















  • 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
    2 days ago










  • 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
    2 days ago










  • That is, learning require understanding beyond memorization.
    – D. Ben Knoble
    2 days ago










  • @D.BenKnoble I've implemented your suggestion. thanks
    – Geremia
    2 days ago
















Good instructions, but missing the relevant explanations.
– D. Ben Knoble
Nov 18 at 15:45




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
2 days ago




@D.BenKnoble What sort of explanations are you looking for?
– Geremia
2 days ago












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
2 days ago




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
2 days ago












That is, learning require understanding beyond memorization.
– D. Ben Knoble
2 days ago




That is, learning require understanding beyond memorization.
– D. Ben Knoble
2 days ago












@D.BenKnoble I've implemented your suggestion. thanks
– Geremia
2 days ago




@D.BenKnoble I've implemented your suggestion. thanks
– Geremia
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f18004%2frenaming-variables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre