diff --git a/src/nvim/shada.c b/src/nvim/shada.c index 784a27d294..71ce4ce9ee 100644 --- a/src/nvim/shada.c +++ b/src/nvim/shada.c @@ -2417,19 +2417,21 @@ static ShaDaWriteResult shada_write(FileDescriptor *const sd_writer, // Initialize substitute replacement string SubReplacementString sub; sub_get_replacement(&sub); - wms->replacement = (PossiblyFreedShadaEntry) { - .can_free_entry = false, - .data = { - .type = kSDItemSubString, - .timestamp = sub.timestamp, + if (sub.sub != NULL) { // Don't store empty replacement string + wms->replacement = (PossiblyFreedShadaEntry) { + .can_free_entry = false, .data = { - .sub_string = { - .sub = sub.sub, - } - }, - .additional_data = sub.additional_data, - } - }; + .type = kSDItemSubString, + .timestamp = sub.timestamp, + .data = { + .sub_string = { + .sub = sub.sub, + } + }, + .additional_data = sub.additional_data, + } + }; + } } // Initialize global marks diff --git a/test/functional/shada/history_spec.lua b/test/functional/shada/history_spec.lua index 1eccff5162..84d5415af8 100644 --- a/test/functional/shada/history_spec.lua +++ b/test/functional/shada/history_spec.lua @@ -239,9 +239,10 @@ describe('ShaDa support code', function() nvim_command('silent! /«/') expect_exit(nvim_command, 'qall!') reset() - fn.setline('.', { '\171«' }) - nvim_command('~&') - eq('\171', fn.getline('.')) + fn.setline('.', { 'foo', '\171«' }) + nvim_feed('gg0n') + eq({ 0, 2, 2, 0 }, fn.getpos('.')) + eq('\171«', fn.getline('.')) eq('', fn.histget('/', -1)) end) @@ -250,9 +251,10 @@ describe('ShaDa support code', function() nvim_command('silent! /\171/') expect_exit(nvim_command, 'qall!') reset() - fn.setline('.', { '\171«' }) - nvim_command('~&') - eq('«', fn.getline('.')) + fn.setline('.', { 'foo', '\171«' }) + nvim_feed('gg0n') + eq({ 0, 2, 1, 0 }, fn.getpos('.')) + eq('\171«', fn.getline('.')) eq('', fn.histget('/', -1)) end) @@ -277,4 +279,14 @@ describe('ShaDa support code', function() nvim_command('wshada') assert_alive() end) + + it('does not dump empty replacement string', function() + expect_exit(nvim_command, 'qall!') + reset() + fn.setline('.', { 'foo', 'bar' }) + nvim_command('language C') + nvim_feed('/f\n') + local err = pcall_err(nvim_command, '~&') + eq('Vim(~):E33: No previous substitute regular expression', err) + end) end)