I'm using (a modified version) of the solution proposed in http://www.thegeekstuff.com/2008/12/vi-and-vim-autocommand-3-steps-to-add-custom-header-to-your-file/ to create and update headers for my source codes automatically.
As explained in the above-mentioned page, upon invoking a write
command in vim
, the following sequence of commands are executed:
This is fine, but there is a slightly annoying issue: Suppose we're editing a line close to the bottom of the window. If at that point we save the file, because of the cursor moves (for updating the header) the line we were editing jumps so that it is positioned in the middle of the window.
To my understanding 'a
moves the cursor to the place marked by mark a
and adjusts the window contents such that the current line appears in the middle of the window. I was wondering if there is a way to make "marks" remember also the exact relative position of the marked line in the window and maintain this position when jumping back to the mark?
It's in the docs: Restoring the cursor position.
:map <F2> msHmt…'tzt`s
(I skipped irrelevant parts with ellipsis).
ms store cursor position in the 's' mark
H go to the first line in the window
mt store this position in the 't' mark
Breaking up restoring the position:
't go to the line previously at the top of the window
zt scroll to move this line to the top of the window
`s jump to the original position of the cursor
The mark itself only stores the position itself; the view (what's shown in the current window) isn't part of that.
What you're looking for is the pair of winsaveview()
and winrestview()
functions. These store the cursor position (like marks, but without adapting automatically to changes in the buffer; something that you probably won't need for the updating of headers), and the details of what is currently shown in the window. Use of these is recommended over marks; in custom mappings or commands it also has the benefit of not clobbering a mark.
If you use :substitute
to update the header, you also may change the current search pattern (unless using a :function
), and the search history. Undoing all of that is hard; I know because I've written such a plugin (AutoAdapt plugin) myself. You may want to have a look at its implementation for further tips (or start using it altogether). (The plugin page also has links to various alternative plugins.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With