Reman
Reman

Reputation: 8109

How do I Join the line above after current line?

I know these commands in Vim:

J : Join line below after current line
-J : Join current line after line above

but how do I join the line above after current line?

Upvotes: 10

Views: 8286

Answers (3)

sathishvj
sathishvj

Reputation: 1464

I added the below lines to my .vimrc. Now, in normal mode, I can either press @j or < leader >j. Leader for me is space. I've also seen people set it to ,.

" join with previous line with @j
let @j="kJ"
nnoremap <leader>j @j

If you haven't set leader yet, you can set it to space like this:

let mapleader = " "
let g:mapleader = " "

Upvotes: 0

doubleDown
doubleDown

Reputation: 8398

You can also use the ex-command

:m-2|j
  • m-2 has the effect of moving the current line to 2 lines above its current position; this switches the position of the current line and the line above.
  • j joins the current line and the line above, inserting a space between the two. Use j! if you don't want the space.
  • | separates the 2 ex-commands

This ex-command is a short way of writing the following

:move .-2
:join  

Upvotes: 7

romainl
romainl

Reputation: 196476

There are many ways to do it. One would be… deleting the line above and appending it to the end of the line below:

k move up one line
^ move to the first printable character
y$ yank to the end of the line
"_d get rid of the now useless line by deleting it into the black hole register
$ move to the end of the line
p put the deleted text

Upvotes: 1

Related Questions