Hi,

Jeri Raye wrote:
> 
> I have a function which removes double lines in my vhdl code when I
> save my file.
> It is like this:
> 
> au BufWritePre *.vhd :%s/^\n\{2,}/\r/ge
> 
> But I assume that this is also the cause that my cursor goes to the
> end of my file when I save my file.
> 
> What must I do to keep the cursor where it was when I did give the 
> save-command
> 
> Is this a save way?
> I just copied some code that seems to work, but I don't know what it does.
> function! Remove()
> exe "normal msHmtgg"

this first sets a mark 's', goes to the top of the screen, sets another
mark 't' and then goes to the start of the file.

> :%s/^\n\{2,}/\r/ge
> exe "normal 'tzt`s"

After the real work is done go to mark 't' and move the current line to
the top of the screen. You now have the same line at the top as before
(except if the former top line was deleted). Now all is left to do is
to go to mark 's' which is now guaranteed to be in the visible part of
the buffer, so no scrolling will take place.

> endfunction
> 
> au BufWrite *.vhd if ! &bin | call Remove() | endif

Another way might be

  function! Remove()
    let view = winsaveview()
    silent! %s/^\n\{2,}/\r/ge
    call histdel('/', -1)
    call winrestview(view)
  endfunction

This uses winsaveview()/winrestview() instead of the two :normal commands
in your example and also takes of removing the substitute pattern from
the search history.

Regards,
Jürgen


-- 
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to