The use case is shared by all (vim) developers around the world (IMHO) : we would like to update a tag and a timestamp on each write operations in the sources files. My attempt with the help of Barry Arthur is this :
Example of string to update on each writes
# 2013-01-09 01:04:31.0 +0100 / Me <me@domain.tld>
The vimrc code
" if not maped, :x don't call UpdateTimestamp()
map :x :wq
function! UpdateTimestamp()
let old_pos = getpos('.')
let old_search = histget("search", -1)
g/^\(#\|\/\/\)\s\+\d\{4\}-\d\{2\}-\d\{2\}\s\+\d\{2\}:\d\{2\}:\d\{2\}\.\d\+\s\++\d\{4\}\s\+\/\s\+Me <me@domain.tld>.*/s/.*/\="# " . strftime('%F %H:%M:%S.0 %z') . " \/ Me <me@domain.tld>"/
exe "normal /".old_search
call setpos('.', old_pos)
endfunction
au BufWrite * call UpdateTimestamp()
problems not solved
- the undo history should not be modified when updating the tag (if possible)
- the position of the screen change on writes
- there’s some not wanted errors displayed on the screen when the pattern is not found
question
Simple : how to solve these issues ?
You don’t need saving/restoring position at all, neither using
setpos()norwinrestview(): you can just not move the cursor. Neither you need tricks to save/restore search: usesearch()function in place of:gandsetline()in place ofs/.*/\=:. Note: there is still one difference between your and mine solution: here only one timestamp is updated. This issue can be fixed.