Reputation: 55273
Is it possible to do this with a Vim command?
[] = Cursor Normal Mode
[ = Cursor Insert Mode
Before
Text []
After
Text
[
Before
Text []
After
[
Text
Upvotes: 5
Views: 3692
Reputation: 1
In case someone wants a solution in Lua for Neovim:
local new_lines = function(command, opposite)
if vim.v.count <= 1 then
return command
end
return ":<C-u><CR>" .. command .. ".<Esc>m`" .. vim.v.count - 1 .. opposite .. "<Esc>g``s"
end
vim.api.nvim_set_keymap("n", "o", function() return new_lines("o", "O") end, { noremap = true, silent = true, expr = true })
vim.api.nvim_set_keymap("n", "O", function() return new_lines("O", "o") end, { noremap = true, silent = true, expr = true })
Upvotes: 0
Reputation: 172580
I've changed the [count]
behavior of o
/ O
with the following mapping. I think this does what you want:
" o/O Start insert mode with [count] blank lines.
" The default behavior repeats the insertion [count]
" times, which is not so useful.
function! s:NewLineInsertExpr( isUndoCount, command )
if ! v:count
return a:command
endif
let l:reverse = { 'o': 'O', 'O' : 'o' }
" First insert a temporary '$' marker at the next line (which is necessary
" to keep the indent from the current line), then insert <count> empty lines
" in between. Finally, go back to the previously inserted temporary '$' and
" enter insert mode by substituting this character.
" Note: <C-\><C-n> prevents a move back into insert mode when triggered via
" |i_CTRL-O|.
return (a:isUndoCount && v:count ? "\<C-\>\<C-n>" : '') .
\ a:command . "$\<Esc>m`" .
\ v:count . l:reverse[a:command] . "\<Esc>" .
\ 'g``"_s'
endfunction
nnoremap <silent> <expr> o <SID>NewLineInsertExpr(1, 'o')
nnoremap <silent> <expr> O <SID>NewLineInsertExpr(1, 'O')
Upvotes: 11
Reputation: 195059
do these two mapping help?
nnoremap <leader>O O<ESC>O
nnoremap <leader>o o<cr>
the first by pressing <leader>O
will add two empty lines above current line, and bring you to INSERT mode. The 2nd one by pressing <leader>o
will add two lines after your current.
Upvotes: 6