When I open a .task file I don't want to see all the comments at the top. I want the screen to be scrolled down... I can do that manually when vim's already running via zt and I want this to be done automatically when the file is loaded.

I have an autocommand but it won't act on zt or normal zt:

This works autocmd BufRead,BufNewFile task.*.task normal 23G22| but I'm not able to then get the screen to redraw.

I've tried:

  function GotoTaskLine()
    normal 23G22|
    normal zt
  autocmd BufRead,BufNewFile task.*.task call GotoTaskLine()

and I've tried these variations:

  autocmd BufRead,BufNewFile task.*.task normal 23G22| | zt
  autocmd BufRead,BufNewFile task.*.task normal 23G22| zt
  autocmd BufRead,BufNewFile task.*.task normal 23G22| | normal zt
  autocmd BufRead,BufNewFile task.*.task normal 23G22| normal zt

but none of them successfully do both: navigate to line23/column22 and redraw screen with cursor position at the top.

Is there another way zt can be invoked? In reading through :help redraw and :help zt I wasn't able to find anything that pointed me towards freedom. Can you shine any light here?

  • No need to use |, :h :bar is used to separate ex commands, not normal commands. Use normal! to avoid remap , use s: to restrict function to script scope. And group your autocmd in autogroup. – dedowsdi Apr 22 at 1:55
  • @dedowsdi can you provide an answer? – alec Apr 22 at 12:43
  • Just tried your example (not those variations), except I use *.task instead of task.*.task , it works fine. – dedowsdi Apr 22 at 12:58
  • @dedowsdi it does not when opening a new file, it seems. – Biggybi Apr 26 at 23:45
  • @Biggybi New file is blank, did you mean new buffer? new buffer works fine for me. – dedowsdi Apr 27 at 0:11

I got it:

augroup SkipTaskComments
    autocmd FileType taskedit autocmd! BufEnter,BufRead <buffer> silent!
        \ normal 12gg22|zt
augroup end

normal zt was wrong, should be zt only.

The augroup is for convenience. autocmd! will delete it's content if you source your vimrc, preventing pile-up.

I added an extra autocmd to check the buffer type, and apply your commands to the <buffer>. I think it's cleaner. silent! is totally optional, that's just how I usually write that type of autocmd.

You can replace <buffer> with your original regex if this is really needed

au FileType taskedit au! BufEnter,BufRead task*.*task silent!

Or even just remove the guilty normal from your code

autocmd BufEnter,BufRead task*.*task
    \ normal 12gg22|zt

You should keep the augroup, though.

| improve this answer | |
  • 2
    The | between 12gg22| and zt is a mistake, you don't need to use | to connect normal commands. – dedowsdi Apr 27 at 10:12
  • Oh yeah, sure, edited - thanks! – Biggybi Apr 27 at 12:57
  • Space is also significant. If I read that right, you'll end up in column 23 (or line 13 if 22 was last character on line 12) after that space there. Also, why 12gg instead of 12G? There was nothing wrong with using G here. In fact, it's even shorter. – filbranden Apr 27 at 13:10
  • Yup. It was on two lines when I copied it, hence the mistakes. gg vs G does not really matter, it's just the way I use that command so it looks more familiar to my eyes. – Biggybi Apr 27 at 15:40
  • 1
    Apparently the buffer events I had were wrong too, BufEnter was required and I deleted BufNewFile. Thanks for the explanations <3 – alec May 1 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.