Skip to content

Commit 1fb5c61

Browse files
ilyakharlamovmarijnh
authored andcommitted
[vim mode] match vim char escape substitution behavior
1 parent beab8ed commit 1fb5c61

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

keymap/vim.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4861,6 +4861,9 @@
48614861
var global = false; // True to replace all instances on a line, false to replace only 1.
48624862
if (tokens.length) {
48634863
regexPart = tokens[0];
4864+
if (getOption('pcre') && regexPart !== '') {
4865+
regexPart = new RegExp(regexPart).source; //normalize not escaped characters
4866+
}
48644867
replacePart = tokens[1];
48654868
if (regexPart && regexPart[regexPart.length - 1] === '$') {
48664869
regexPart = regexPart.slice(0, regexPart.length - 1) + '\\n';
@@ -4899,7 +4902,11 @@
48994902
global = true;
49004903
flagsPart.replace('g', '');
49014904
}
4902-
regexPart = regexPart.replace(/\//g, "\\/") + '/' + flagsPart;
4905+
if (getOption('pcre')) {
4906+
regexPart = regexPart + '/' + flagsPart;
4907+
} else {
4908+
regexPart = regexPart.replace(/\//g, "\\/") + '/' + flagsPart;
4909+
}
49034910
}
49044911
}
49054912
if (regexPart) {

test/vim_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3934,6 +3934,11 @@ testSubstitute('ex_substitute_or_word_regex', {
39343934
expectedValue: 'five|five \n three|four',
39353935
expr: '%s/(one|two)/five/g',
39363936
noPcreExpr: '%s/\\(one\\|two\\)/five/g'});
3937+
testSubstitute('ex_substitute_forward_slash_regex', {
3938+
value: 'forward slash \/ was here',
3939+
expectedValue: 'forward slash was here',
3940+
expr: '%s#\\/##g',
3941+
noPcreExpr: '%s#/##g'});
39373942
testSubstitute('ex_substitute_backslashslash_regex', {
39383943
value: 'one\\two \n three\\four',
39393944
expectedValue: 'one,two \n three,four',

0 commit comments

Comments
 (0)