Vim Tips Wiki

Move comment line to end of next line

Revision as of 23:49, September 29, 2008 by JohnBot (Talk | contribs)

1,619pages on
this wiki
Tip 1050 Printable Monobook Previous Next

created November 17, 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

Around Wikia's network

Random Wiki