3

Without being in insert mode, from a certain line, I'd like to create a blank line above and below that line in staying on the same line. Could anyone have an idea how to do it?

To clarify what I'm looking for, if I have three glued lines and I'm on the second line, I want to insert a blank line between the first and second line and another blank line between the second and the third line. I want to do that in staying on the second line and in interactive mode (not insert mode).

Thanks in advance!

  • 1
    While I understand roughly what you're looking for, I'm having trouble understanding exactly what you're looking for. Perhaps you could clarify by providing an example? e.g. as in this question. – Martin Tournoij Jan 8 '17 at 21:53
1

Use marks. For example put this into your .vimrc:

nnoremap <silent><A-j> :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap <silent><A-k> :set paste<CR>m`O<Esc>``:set nopaste<CR>

Alt+j and Alt+k inserts blank line above and below current line in normal mode respectively.

Here a nice addition for deleting a blank line below/above only if any exists (using regular expressions).

nnoremap <silent><A-S-j> m`:silent +g/\m^\s*$/d<CR>``:noh<CR>
nnoremap <silent><A-S-k> m`:silent -g/\m^\s*$/d<CR>``:noh<CR>

Shift+Alt+j deletes empty line below and Shift+Alt+k deletes empty line above.

I find these mappings intuitive and confortable. Enjoy :) *Sometimes there problems with alt/meta key, so use what works fine for you.

Read more (inter alia code explanation) in the source: http://vim.wikia.com/wiki/Quickly_adding_and_deleting_empty_lines

4

Your question looks like an almost duplicate of this question on SO: https://stackoverflow.com/questions/3170348/insert-empty-lines-without-entering-insert-mode/3171023 and of this one on SU: https://superuser.com/questions/147715/vim-insert-empty-line-above-current-line-not-open-i-e-without-entering-inser

I though it was also answered here, but I cannot find any trace of it.

Any way, append() is the best way to insert line(s) without moving the cursor nor altering any thing but the current buffer. It's indeed cumbersome to use on the fly, but for scripting it's the right tool.

Which gives:

nnoremap µ :<c-u>call append('.', '')<cr>:call append(line('.')-1, '')<cr>

(Note that I don't see a solution that doesn't require to leave the NORMAL mode to enter the COMMAND mode. It respects the requirement of not entering the INSERT mode though)

Note that when there is no objective of writing a plugin with no side effect, and if the "no insert mode" is just a useless requirement imposed for no good reason, we usually sacrifice a mark (as in :h mark) ->

mxO<esc>`xo<esc>`x

We can, of course, put this in a mapping, but it will alter the x mark, which can be problematic if we use it to bookmark a position in the text/code we're editing. I use marks a lot when I'm editing texts, that's why I avoid them as much as possible in mappings. I don't want the mappings I use to mess with what I'm doing.

0

To create blank line below current one use o, for line above O.

  • No, with those commands, we change line and I'll be change mode (interactive - inserting), but I don't want that. In fact, with three glued lines, if I am on the second line, I want to create space between the first and second line and the second and third line in staying on the second line (in interactive mode). – user10766 Jan 8 '17 at 21:10
  • Use those commands in conjunction with some others to achieve the effect you want. – Antony Jan 8 '17 at 21:28
  • Sorry, but how could I do that? – user10766 Jan 8 '17 at 21:30
  • 1
    If you use :nnoremap <leader>s o<esc>kO<esc>j this should produce what you're looking for right? Maybe not the nicest solution, but it should work. – Octaviour Jan 9 '17 at 8:32
  • 1
    @Octaviour. This moves the cursor. – Luc Hermitte Jan 9 '17 at 9:20
0

I call it "isolate the line" - and this mapping does it:

nnoremap <leader>i O<c-o>j<c-o>o<c-o>k<esc>

It leaves the cursor in the current line. Ctrl-o means "switch to normal mode for one command"

  • 1
    But the cursor is moved. – Luc Hermitte Jan 13 '17 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy