Main Page | Report this Page
Computers Forum Index  »  Computer - Editors  »  join lines...
Page 1 of 1    

join lines...

Author Message
julia...
Posted: Sat Sep 05, 2009 11:35 pm
Guest
Is it possible to join, merge or move lines in vim that are not
adjacent ? Thanks

linelineline\newline (line1)
lineline\newline (line2)
linelinelineline\newline (line/)
&
rowrowrow\newline (line1)
rowrow\newline(line2)
row\newline(line3)

Result
linelineline&rowrowrow\newline
lineline&rowrow\newline
linelinelineline&row\newline
 
Geoff Clare...
Posted: Wed Sep 09, 2009 4:42 pm
Guest
julia wrote:

Quote:
Is it possible to join, merge or move lines in vim that are not
adjacent ? Thanks

linelineline\newline (line1)
lineline\newline (line2)
linelinelineline\newline (line/)
&
rowrowrow\newline (line1)
rowrow\newline(line2)
row\newline(line3)

Result
linelineline&rowrowrow\newline
lineline&rowrow\newline
linelinelineline&row\newline

The following macro will join a set of lines (marked using ma on
the first and mb on the last) onto the same number of lines
starting at the current line. I could have included the "&"
joining character in the macro, but I thought it would be more
generally useful if it just did a straight join. So to use it
for the above task you would first need to append "&" to the
first set of lines, then mark the second set, then go back to
the top and run the macro. E.g.:

go to the line before the "&"
type :1,.s/$/\&/ and press ENTER
go to the line after the "&"
type ma
go to the last line
type mb
go to the first line
type =j (or whatever name you used for the macro)
delete the "&" line

If the line marked with mb is the last line of the file, the
macro works but vim will report an error when it tries to move to
the next line. Either ignore the error or add a blank line to
the bottom first.

Here's the macro (copy and pasted from ":map" output in vim):

=j mx:'a,'bg/^/s/[\\&~?]/\\&/g|s/.*/s?$?&?/|d x|'x at (no spam) x|'x+ma x<CR>

To get it defined, add the following to your startup file:

map =j mx:'a,'bg/^/s/[\\&~?]/\\&/g^V|
s/.*/s?$?&?/^V|d x^V|'x at (no spam) x^V|'x+ma x^V^M

(It should all be one line - I had to split it for posting).

Again, this is how it looks on the screen when editing the
startup file in vim. To enter it, where there is ^V press
CTRL-V twice and where there is ^M press CTRL-V CTRL-M.
Choose a different macro name if you don't like "=j".

--
Geoff Clare <netnews at (no spam) gclare.org.uk>
 
 
Page 1 of 1    
All times are GMT
The time now is Sun Nov 29, 2009 1:47 am