Say I want to begin letters like this:
Dear world,
hello.
What's the simplest way to define a mapping that opens a new line preserving the cursor's horizontal position?
An easy way to achieve this using no vimscript is the following:
nnoremap <leader>o yyp^v$r A
This assumes you want to start the mapping in normal mode (otherwise just add <Esc>
to the beginning of your insert mode mapping).
It copies the current line with yy
, pastes it on the line below (p
), selects the pasted text in visual mode (^v$
), replaces the selection with whitespaces (r
followed by a space) and jumps to the end, putting you in insert mode (A
).
printf '%s\v%s\n' hello there
(at a command prompt).