I have a bunch of local variable references in a Python script that I want to pull from a dictionary instead. So, I need to essentially change foo
, bar
, and others into env['foo']
, env['bar']
and so on. Do I need to write a regular expression and match each variable name to transform, or is there a more direct approach that I could just repeat with the .
command?
相关问题
- Emacs shell: save commit message
- How to change the first two uppercase characters o
- Insert text into current buffer from function
- Hot reload on save
- Substituting zero-width match in vim script
相关文章
- 如何让 vim 支持 .cshtml 文件的代码高亮
- Auto-save in VIM as you type
- How can I use gcc's -I command to add recursiv
- Vim: overloaded mapping for multiple modes
- vi editor is not responding
- How to use relative line numbering universally in
- How to copy the value of a vim option to a registe
- How to specify bold font in VS Code editor?
I figured out one way to do what I need. Use
q{0-9a-zA-Z"}
to record key strokes into a buffer. Position the cursor at the begging of the variable name, thencw
and typeenv['']
. Next move the cursor back one space to the last quote and paste the buffer filled from thecw
command withP
. Finally, reuse the recording with@{0-9a-z".=*}
for each variable.you could write a function that would do this pretty well, add this to your .vimrc file:
This will surround every occurance of the word currently under the cursor.
If you wanted to specify what went before and after each instance you could use this:
If you only want to confirm each instance of the variable you could use this:
There's an easier way - you can use a regex search and replace. Go into cmdline mode by typing a colon and then run this command:
Replacing
foo
,bar
, andbaz
with whatever your actual variable names are. You can add as many additional variables as you'd like, just be sure to escape your OR pipes with a backslash. Hope that helps.You can use a macro: type these commands in one go (with spacing just to insert comments)
then, when you're on the next word, just hit
@a
to replay the macro (or even@@
to repeat the last replay after that).