Reputation: 23
I'm working with a large dataset and multiple lines need change on a certain parameter. Something like this for example:
funct(35, circle, square, triangle);
funct(42, sphere, cube, prism);
funct(74, disc, rhombus, rectangle);
needs to become:
funct(35, circle, my_square, other_triangle);
funct(42, sphere, my_cube, other_prism);
funct(74, disc, my_rhombus, other_rectangle);
How could I go about doing this?
Upvotes: 1
Views: 148
Reputation: 9417
I'm aware of multiple-cursors
, and they look really cool, but I've been to lazy to learn them. Here's a solution using only Emacs built ins.
(defun align-regexp-comma (beginning end)
"Align columns using comma as a delimiter."
(interactive "*r")
(align-regexp beginning end
",\\(\\s-*\\)" 1 1 t))
(global-set-key (kbd "C-<return>") 'cua-rectangle-mark-mode)
Then select the region you of interest, do M-x align-regexp-comma
, which should align the arguments, then you can use C-<return>
to start the rectangle mark, which lets you edit several lines at once. If you then want to remove the aligning spaces, use query-regexp-replace
(M-%
) with ", +" -> ", ".
Upvotes: 0
Reputation: 5544
I'll speak about emacs because I just love emacs !
to enter commands in the prompt : Alt + x
You can install the multiple-cursors
package. It's like the Ctrl+D
in SublimeText. Here is the doc.
You can use built-in package query-replace
. It will replace the string/regex you want with another. Documentation.
The advantage of this one is that you can choose if you want to replace or not each occurence.
The built-in packages replace-string
and replace-regexp
will replace the pattern of your choice with a string or another pattern.
replace-regexp
If you just want to rename some variables, multiples-cursors
, replace-string
and query-replace
are fine.
If you want to replace patterns in huge datasets, replace-regexp
is fun, but nothing's better than a script (bash, js, python ...).
There are amazing books about sed
and awk
bash commands (sed & awk, 2nd Edition)
Upvotes: 1
Reputation: 7679
An additional vim approach. Visually select all of the lines you would like to change, and then type this:
:norm 2Wimy_<C-v><esc>Wiother_
Note that <C-v>
and <esc>
are ctrl-v
and esc
, not text. Or, if you want to do this on every line, do this:
:%norm 2Wimy_<C-v><esc>Wiother_
You could also do this on lines in a certain range. For example, lines 3-100:
:3,100norm 2Wimy_<C-v><esc>Wiother_
Upvotes: 1
Reputation: 2703
Using vim's regex engine:
:%s/\v(%(\s*\w+,){PARAMETER_NUMBER})(\s+)(\w+)/\1\2NEW_PREFIX\3
Substitute PARAMETER_NUMBER
with the index (starting from 0) of the parameter you'd like to change and NEW_PREFIX
with the prefix you'd like to add to it.
For instance, running:
:%s/\v(%(\s*\w+,){2})(\s+)(\w+)/\1\2my_\3
Changes your example code into:
funct(35, circle, my_square, triangle);
funct(42, sphere, my_cube, prism);
funct(74, disc, my_rhombus, rectangle);
You can modify this to customize it in other ways by changing the order of the backreferences (the \1
, \2
and \3
) at the end of the command. \1
refers to the parameters before the one you're changing, \2
refers to the whitespace just before the parameter you're changing and \3
to the parameter itself.
Upvotes: 0
Reputation: 3086
With Vim:
:%s/\vfunct\(([^,]+,\s*){2}\zs/my_/
The 2
above is the number of arguments you want to skip.
The catch: this breaks if some parameters are themselves function calls. There are plugins that allows you to cope with that situation.
Upvotes: 0