Skip to content

Commit 6523439

Browse files
benhormannmarijnh
authored andcommitted
[vim] substitute pattern ending in dollar
1 parent c58a7b7 commit 6523439

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

keymap/vim.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5097,9 +5097,11 @@
50975097
regexPart = new RegExp(regexPart).source; //normalize not escaped characters
50985098
}
50995099
replacePart = tokens[1];
5100-
if (regexPart && regexPart[regexPart.length - 1] === '$') {
5101-
regexPart = regexPart.slice(0, regexPart.length - 1) + '\\n';
5102-
replacePart = replacePart ? replacePart + '\n' : '\n';
5100+
// If the pattern ends with $ (line boundary assertion), change $ to \n.
5101+
// Caveat: this workaround cannot match on the last line of the document.
5102+
if (/(^|[^\\])(\\\\)*\$$/.test(regexPart)) {
5103+
regexPart = regexPart.slice(0, -1) + '\\n';
5104+
replacePart = (replacePart || '') + '\n';
51035105
}
51045106
if (replacePart !== undefined) {
51055107
if (getOption('pcre')) {
@@ -5128,11 +5130,9 @@
51285130
if (flagsPart) {
51295131
if (flagsPart.indexOf('c') != -1) {
51305132
confirm = true;
5131-
flagsPart.replace('c', '');
51325133
}
51335134
if (flagsPart.indexOf('g') != -1) {
51345135
global = true;
5135-
flagsPart.replace('g', '');
51365136
}
51375137
if (getOption('pcre')) {
51385138
regexPart = regexPart + '/' + flagsPart;

test/vim_test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4311,10 +4311,14 @@ testSubstitute('ex_substitute_multibackslash_replacement', {
43114311
value: 'one,two \n three,four',
43124312
expectedValue: 'one\\\\\\\\two \n three\\\\\\\\four', // 2*8 backslashes.
43134313
expr: '%s/,/\\\\\\\\\\\\\\\\/g'}); // 16 backslashes.
4314-
testSubstitute('ex_substitute_dollar_match', {
4314+
testSubstitute('ex_substitute_dollar_assertion', {
43154315
value: 'one,two \n three,four',
4316-
expectedValue: 'one,two ,\n three,four',
4316+
expectedValue: 'one,two ,\n three,four', // TODO: should match at end of doc.
43174317
expr: '%s/$/,/g'});
4318+
testSubstitute('ex_substitute_dollar_literal', {
4319+
value: 'one$two\n$three\nfour$\n$',
4320+
expectedValue: 'one,two\n,three\nfour,\n,',
4321+
expr: '%s/\\$/,/g'});
43184322
testSubstitute('ex_substitute_newline_match', {
43194323
value: 'one,two \n three,four',
43204324
expectedValue: 'one,two , three,four',

0 commit comments

Comments
 (0)