Vim Tips Wiki

Move comment line to end of next line

Redirected from VimTip1050

1,623pages on
this wiki
Add New Page
Add New Page Talk0
Tip 1050 Printable Monobook Previous Next

created 2005 · complexity basic · author matahijau · version 5.7

Let's say you have some C or Java that looks like this:

// get the age of the person
age = person.getAge();

If you want to compact your code a bit you might want to stick the comment at the end of the statement instead of on its own line. Essentially you want to "join up":

map <C-S-j> kddpkJ

Used on the example above by placing the cursor on the second line and typing ctrl-shift-j, you will now have:

age = person.getAge(); // get the age of the person

ddp is used to swap the lines before joining, so the comment line is at the end of the newly joined line.


A better way of swapping lines is to use


Hence, modifying "map <C-S-j> kddpkJ" and maintaining cursor position:

nmap <C-S-j> mz:m-2<CR>J`z

Also on Fandom

Random Wiki