Possible Duplicate:
Replace while keeping certain “words” in vi/vim
Let’s say I have several lines like
$adv_id;
$am_name;
$campaign_ids;
$repeat_on;
$opt_days;
$opt_time;
$am_or_pm;
Let’s say I use visual mode to select all the lines.. how can I add characters at the start and the end of each line so it looks something like
$_REQUEST($adv_id);
$_REQUEST($am_name;
$_REQUEST($campaign_ids);
$_REQUEST($repeat_on;
$_REQUEST($opt_days);
$_REQUEST($opt_time);
$_REQUEST($am_or_pm);
Pretty similar to your other question, so the explanation there should help you to understand this substitute. With the lines selected as a visual block, use this substitute command:
As before, the
'<,'>will be auto-filled for you in the command-line if you have a visual selection.The difference here is that we’re using
\(\)to make a capturing group, which will capture part of the regex, and use it again in the replacement, using\1, which refers to the first capturing group.Also, since this regex uses
$literally to position the replacement, it needs to be escaped:\$, since it has a special meaning in the regex: end of line.If you’ll need to have multiple replacements on a single line, you’ll need to add the
gflag, and you may want to remove the semicolon:The reverse regex, e.g. replacing
$_REQUEST($adv_id);with$adv_id;, is pretty similar:Here we capture everything between the parens in a
$_REQUEST(...);in a capturing group, and that capturing group is the entire replacement.