Skip to content

Commit f454d57

Browse files
ilyakharlamovmarijnh
authored andcommitted
[vim mode] #5753 Feature request: Support &/$0 in vim substitute replacements
1 parent 1fb5c61 commit f454d57

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

keymap/vim.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,7 +4073,7 @@
40734073
}
40744074

40754075
// Unescape \ and / in the replace part, for PCRE mode.
4076-
var unescapes = {'\\/': '/', '\\\\': '\\', '\\n': '\n', '\\r': '\r', '\\t': '\t'};
4076+
var unescapes = {'\\/': '/', '\\\\': '\\', '\\n': '\n', '\\r': '\r', '\\t': '\t', '\\&':'&'};
40774077
function unescapeRegexReplace(str) {
40784078
var stream = new CodeMirror.StringStream(str);
40794079
var output = [];
@@ -4871,7 +4871,7 @@
48714871
}
48724872
if (replacePart !== undefined) {
48734873
if (getOption('pcre')) {
4874-
replacePart = unescapeRegexReplace(replacePart);
4874+
replacePart = unescapeRegexReplace(replacePart.replace(/([^\\])&/g,"$1$$&"));
48754875
} else {
48764876
replacePart = translateRegexReplace(replacePart);
48774877
}

test/vim_test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,6 +3939,24 @@ testSubstitute('ex_substitute_forward_slash_regex', {
39393939
expectedValue: 'forward slash was here',
39403940
expr: '%s#\\/##g',
39413941
noPcreExpr: '%s#/##g'});
3942+
testVim("ex_substitute_ampersand_pcre", function(cm, vim, helpers) {
3943+
cm.setCursor(0, 0);
3944+
CodeMirror.Vim.setOption('pcre', true);
3945+
helpers.doEx('%s/foo/namespace.&/');
3946+
eq("namespace.foo", cm.getValue());
3947+
}, { value: 'foo' });
3948+
testVim("ex_substitute_ampersand_multiple_pcre", function(cm, vim, helpers) {
3949+
cm.setCursor(0, 0);
3950+
CodeMirror.Vim.setOption('pcre', true);
3951+
helpers.doEx('%s/f.o/namespace.&/');
3952+
eq("namespace.foo\nnamespace.fzo", cm.getValue());
3953+
}, { value: 'foo\nfzo' });
3954+
testVim("ex_escaped_ampersand_should_not_substitute_pcre", function(cm, vim, helpers) {
3955+
cm.setCursor(0, 0);
3956+
CodeMirror.Vim.setOption('pcre', true);
3957+
helpers.doEx('%s/foo/namespace.\\&/');
3958+
eq("namespace.&", cm.getValue());
3959+
}, { value: 'foo' });
39423960
testSubstitute('ex_substitute_backslashslash_regex', {
39433961
value: 'one\\two \n three\\four',
39443962
expectedValue: 'one,two \n three,four',

0 commit comments

Comments
 (0)