diff --git a/runtime/doc/insert.txt b/runtime/doc/insert.txt index 27b28624f8..48fd442b7e 100644 --- a/runtime/doc/insert.txt +++ b/runtime/doc/insert.txt @@ -1907,6 +1907,16 @@ These commands are used to start inserting text. You can end insert mode with . See |mode-ins-repl| for the other special characters in Insert mode. The effect of [count] takes place after Insert mode is exited. +The following commands insert text, but stay in normal mode: + + *]* +] Insert an empty line below the cursor without leaving + Normal mode, repeat [count] times. + + *[* +[ Insert an empty line above the cursor without leaving + Normal mode, repeat [count] times. + When 'autoindent' is on, the indent for a new line is obtained from the previous line. When 'smartindent' or 'cindent' is on, the indent for a line is automatically adjusted for C programs. diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 3a8277f566..df794596b8 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -178,6 +178,7 @@ DEFAULTS • |[t|, |]t|, |[T|, |]T|, |[CTRL-T|, |]CTRL-T| navigate through the |tag-matchlist| • |[a|, |]a|, |[A|, |]A| navigate through the |argument-list| • |[b|, |]b|, |[B|, |]B| navigate through the |buffer-list| + • |[|, |]| add an empty line above and below the cursor • Snippet: • `` in Insert and Select mode maps to `vim.snippet.jump({ direction = 1 })` diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index 10816ec358..8fa94a2601 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -163,6 +163,7 @@ of these in your config by simply removing the mapping, e.g. ":unmap Y". - |[t|, |]t|, |[T|, |]T|, |[CTRL-T|, |]CTRL-T| - |[a|, |]a|, |[A|, |]A| - |[b|, |]b|, |[B|, |]B| +- |[|, |]| - Nvim LSP client defaults |lsp-defaults| - K |K-lsp-default| diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index d3b7bda871..f399360f1e 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -363,6 +363,19 @@ do cmd({ cmd = 'blast' }) end end, { desc = ':blast' }) + + -- Add empty lines + vim.keymap.set('n', '[', function() + local repeated = vim.fn['repeat']({ '' }, vim.v.count1) + local linenr = vim.api.nvim_win_get_cursor(0)[1] + vim.api.nvim_buf_set_lines(0, linenr - 1, linenr - 1, true, repeated) + end, { desc = 'Add empty line above cursor' }) + + vim.keymap.set('n', ']', function() + local repeated = vim.fn['repeat']({ '' }, vim.v.count1) + local linenr = vim.api.nvim_win_get_cursor(0)[1] + vim.api.nvim_buf_set_lines(0, linenr, linenr, true, repeated) + end, { desc = 'Add empty line below cursor' }) end end diff --git a/test/functional/editor/defaults_spec.lua b/test/functional/editor/defaults_spec.lua index 70f12ab475..9786bfeaac 100644 --- a/test/functional/editor/defaults_spec.lua +++ b/test/functional/editor/defaults_spec.lua @@ -152,6 +152,54 @@ describe('default', function() ]], }) end) + + describe('[', function() + it('adds an empty line above the current line', function() + n.clear({ args_rm = { '--cmd' } }) + n.insert([[first line]]) + n.feed('[') + n.expect([[ + + first line]]) + end) + + it('works with a count', function() + n.clear({ args_rm = { '--cmd' } }) + n.insert([[first line]]) + n.feed('5[') + n.expect([[ + + + + + + first line]]) + end) + end) + + describe(']', function() + it('adds an empty line below the current line', function() + n.clear({ args_rm = { '--cmd' } }) + n.insert([[first line]]) + n.feed(']') + n.expect([[ + first line + ]]) + end) + + it('works with a count', function() + n.clear({ args_rm = { '--cmd' } }) + n.insert([[first line]]) + n.feed('5]') + n.expect([[ + first line + + + + + ]]) + end) + end) end) end) end)