Hello James, * On Fri, Mar 09, 2007 at 10:22:22AM +0100, James Kanze <[EMAIL PROTECTED]> wrote: > I'm having trouble automatically positionning the cursor when > opening an existing file. > > Basically, all of my files have a standard header (copyright > blurb, etc.), and I'd like to have the cursor positionned after > it at the start of editing. When opening a new file, I have the > following in my .vimrc: > > [...] > > I'd like something similar when I open the file, but > > autocmd BufReadPost *.cc,*.hh,*.sh,*.mk,GNUmakefile :1 > autocmd BufReadPost *.cc,*.hh,*.sh,*.mk,GNUmakefile /^$/ > > leaves the cursor at the top.
I have no idea why BufReadPost is not triggered. Could it be that your autocommands are in the global scope, and that they are reset by other autocommands which are triggered just before yours ? You can try the following code to fix this possible situation augroup JK_template au! au BufReadPost your auto-command aug END > Changing the event to BufWinEnter or BufEnter works well when I open > the file, but causes the cursor position to be lost if I leave the > window, then later come back to it. If that case, you can set a buffer-local variable that tells you to not reset the cursor position. -> function s:GotoLastEmptyLine() if ! exist('b:JK_file_already_opened') normal! G let l = search('^\s$$','b') if l==0 " do whatever you want endif let b:JK_file_already_opened =1 endif endfunction au BufEnter *.cc call s:GotoLastEmptyLine() > From the documentation, I'd expect BufReadPost to work, but it > doesn't. > > Also, while I'm at it: is there any reason why 1G doesn't work, but :1 > does, to start from a known point. Auto-commands are expecting a command, not a normal mode sequence. Try ``:normal! 1G'' instead of ``1G'' HTH, -- Luc Hermitte http://hermitte.free.fr/vim/