Reputation: 31178
How I can select all the text start with foo_list starting from line 4 (see. below code) and rename them with list_values or any other preferred name ? Please note, I don't want to change in the first line.
Thanks in advance !
foo_list = [5, 2, 3, 1, 4]
def reverse_list_1():
foo_list=[0,10,20,40]
for i in reversed(foo_list):
print i,
foo_list=[0,10,20,40]
print foo_list[::-1]
for i in reversed(foo_list):
print i,
length = len(foo_list)
for i in range(length):
print foo_list[length-i-1],
Preferable solution: key map in the .vimrc or .gvimrc file, don't want to use any plugin.
Upvotes: 0
Views: 138
Reputation: 1137
:substitute is the 'correct' way, but if you're only making a few changes, and you are not very experienced with ex commands, sometimes it takes longer to think through the command than to bounce through the list of changes you want to make using motions.
If you start with your cursor on the first instance of foo_list, hit '*' to jump to the next occurrence in the file. Hit 'ce' to delete to the end of the word and enter insert mode. Type in your new variable name and return to normal mode. Now you can jump through the rest of the file using 'n' to jump to the next occurrence (or 'N' to go back), and '.' to repeat your last edit action.
Upvotes: 0
Reputation: 45117
For small changes like this I like to use the gn
motion. The gn
motion visually selects the current search pattern. This makes for a powerful search/replace method when combining the gn
motion with the change, c
, operator and the repeat command, .
.
Basic steps:
foo_list
your search pattern. e.g. /foo_list
or via *
c
and gn
to change the first foo_list
. e.g. cgnbar_list<esc>
.
n
to advance to the next search results. (Hit n
twice to skip an occurrence)n
and .
until doneThere is a nice Vimcasts episode on this topic: Operating on search matches using gn
For more information see:
:h gn
Upvotes: 0
Reputation: 3500
:%s/foo_list/list_values/gc
This command says to replace the word foo_list
with list_values
in the whole document, asking for confirmation each time. Then for the first occurrence of foo_list on line 1, press n
(to indicate NO) , and press y
(to indicate YES) for all further occurrences to replace them. This solution works when you have to replace a few words. You can read the command as follows:
In the whole document (%
), substitute (s
) the word foo_list
with list_values
and do this globally (g
), asking for confirmation (c
) each time. For more options in the substitute command type :help :s
in vim.
Solution 2 :
When there are thousands of words to replace, you surely don't want to type a y/n confirmation each time (which is enabled by the c
flag in the end in the above command).
Take your cursor to line 4 and run
:.,$s/foo_list/list_values/g
Read the above command as from here (.
) to the end of file (,$
)
replace (s
) the word foo_list
with list_values
globally (g
).
Upvotes: 0
Reputation: 172590
That's a job for :substitute
. You can specify the range with explicit line numbers (here: 4
to end of buffer $
, or maybe next empty line /^$/
):
:4,$substitute/\<foo_list\>/list_values/g
You can also first move to the first line and use the .,$
range.
Since that's still a lot of typing, you can pull in the current word (assuming you first position the cursor on the foo_list
occurrence in line 4) into the command line via <C-R><C-W>
.
Or, for a plugin solution, my ChangeGlobally plugin provides a mapping that avoids the use of :s
.
Upvotes: 1