Monday, December 14, 2009

Re: J oining lines without cursor movement

You could use the command

:s/\n//

But note you may not get exactly the same result as J. Since the
(example) line ends with a period, when I join those two lines, I get
two blanks between the '.' and the 'T' (see 'joinspaces').

Also, it moves the cursor to the beginning of the line (vs. the join
point); so if your cursor was on, say, the 'i' on the first line, it
would be in the first column afterwards.

If you after doing that sort of thing ins a script, that's why I came
up with SaveLoc() and RestoreLoc():

"
=============================================================================

" FUNCTION: SaveLoc
" Save the current cursor position (using the given token, if any) so
" that the same view can be restored (i.e., not just the line/column
of
" the cursor, but also which line is at the top of the window).

function! SaveLoc(...)
let token = (a:0 == 1) ? a:1 : 'saved'

let b:save_{token}_line = line(".")
let b:save_{token}_col = virtcol(".")
normal! H
let b:save_{token}_top = line(".")
let b:save_{token}_winline = winline()
let b:save_{token}_adj = winline() - 1 - &scrolloff

" Since we just moved the cursor (using H), put it back; we
cannot use ``
" since the H in the script doesn't go on the "jump stack".
"
exe b:save_{token}_line
exe "normal! " . b:save_{token}_col . "|"
endfunction

"
=============================================================================

" FUNCTION: RestoreLoc
" Restore the last (given token's, if any) view saved with SaveLoc
" (above); i.e., not just the line/column of the cursor, but also
which
" line is at the top of the window.

function! RestoreLoc(...)
let token = (a:0 == 1) ? a:1 : 'saved'

" Put the saved line at the top of the window.
exe b:save_{token}_top
normal! zt
let wl = b:save_{token}_winline
let adjust = b:save_{token}_adj
if winline() != wl && adjust > 0
exe 'normal! ' . adjust . "\<C-Y>"
endif

" Put the cursor at the saved column on the saved line.
exe b:save_{token}_line
exe "normal! " . b:save_{token}_col . "|"
endfunction

"
=============================================================================

Hope that helps.

-gary

On Dec 13, 8:32 pm, sc <tooth...@swbell.net> wrote:
> On Sunday 13 December 2009 10:16:20 pm meino.cra...@gmx.de wrote:
>
> > Suppose I have two lines like this (indentation only for
> >  makeing it more readable here...)
>
> >     This is the first line.
> >     This is the second line.
>
> > The cursor is in the beginning of the first line.
>
> > Now I want to join the lines with "J" but without moving the
> >  cursor away from where it is before joining.
>
> > Is that possible without scripting?
>
> is using J0 (that's a zero after the J) too much typing for you?  
> or maybe unsatisfactory because the cursor moves then moves back?
>
> sc

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: