Intro to Registers
The Vim commands you are looking for are delete/cut, dd
and put/paste, p
. Each of these commands by default use the unnamed register, ""
. So dd
will delete a line and put the freshly deleted line into the unnamed register. p
will put the contents from the unnamed register into your current buffer.
Vim has more than just the unnamed register, it also has named registers. These registers are a
-z
.
- To see what your registers are set to you can execute
:registers
.
- To use a named register prefix your command with quote and a lowercase letter, e.g.
"add
- Upper case letters will append instead of replace the contents of a register, e.g
"Add"
The Vim Way
"add
the first line, then append the next line via "Add
. For repeated deletions use .
- Use a text object. e.g.
dap
for delete a paragraph. See :h text-objects
- Delete text via some motion. e.g.
d}
is delete till end of paragraph
- Delete to a regex patter. e.g.
d/foo<cr>
- Lastly would be using visual mode,
V6jd
. Nothing wrong with using visual mode
Additionally you want to stay out of insert mode unless you are inserting text. You only want to be in insert move for short burst at a time. Most of the time you should be in Normal mode, hence the name.
For a nice post on the Vi/Vim way see this StackOverflow post: Your problem with Vim is that you don't grok vi.
Alternatives
If none of the standard Vim tricks satisfy your needs you may want to look into plugins that support line exchanging like Unimpaired or LineJuggler.
However if you really do want something similar this nano/pico features you can use the following by putting it in your ~/.vimrc
file:
nnoremap Q :<c-u>call <SID>DeleteAppend()<cr>
function! s:DeleteAppend()
let save = @a
let @a = @@
let reg = get(g:, 'append_tick', -1) == b:changedtick ? 'A' : 'a'
execute 'normal! "' . reg . 'dd'
let g:append_tick = b:changedtick
let @@ = @a
let @a = save
endfunction
The Q
normal command will now delete a line and append additional deleted lines until a another command is executed. Example usage: QQjjQQQp
For more help please see
:h d
:h p
:h "
:h registers
:h :reg
:h motion
:h text-objects
:h .
:h visual-mode