I am quite new to using vim so please forgive me if this has been asked else where. I have tried Googling but am not really sure what to search for.

In my vimrc I have the follwing function that will search for (<.>), delete it and enter insert mode:

function! NextMark()
    execute "normal! /(<.>)\<cr>da("
    execute "startinsert"

This works as expected and I have the following mapping so that pressing double space in insert mode will run the function:

inoremap <space><space> <Esc>:call NextMark()<cr>

This works so I don't believe the problem is with the function.

When I create a new HTML file (and in the future this will be extended for other files), I would like to read in a template file and run the above function. Below is what I have so far but for some reason it isn't working:

augroup new_html
    autocmd BufNew *.html execute "read ~/Templates/html"
    autocmd BufNew *.html execute "normal! ggdd"
    autocmd BufNew *.html execute NextMark()
augroup END

This partly works, it reads my file and deletes the first (empty) line. For some reason though, it doesn't take me to the first (<.>) although it does put me into insert mode so I think the function is being run. For some reason, it isn't doing the search.

Thanks in advance for any help

Edit I have just noticed that the first marker is being deleted, I am just being put in insert mode in the wrong place.

Do the autocmds I specify get run one after the other? It is a bit like the second and third are being run at the same time.

Edit 2: Thank you @Naumann and @Tumbler41 for your suggestions. I now have a seperate function that reads in my template, deletes the first line then calls NextMark. Here s the function:

function! NewHtml()
    read ~/Templates/html
    execute "normal! ggdd"
    call NextMark()

and here is my revised autocmd block:

augroup new_html
    autocmd BufNew *.html call NewHtml()
augroup END

I am still being left with the first mark deleted but in inset mode at the begining of the document.

Any more suggestions?

Edit 3 I have just added BufNewFile to the autocmd so that it will work if i run vim newFile.html from the command line. This works. If, however, I already have vim open and I run :e newFile.html, it almost works but my cursor is at the begining of the file, not at the point of my first mark.

  • 2
    you don't want execute NextMark() but call NextMark() besides: all the other execute in your augroup can be dropped – Naumann Apr 21 '17 at 14:24
  • 1
    As far as I know, you can't be sure of what order autocmds will run when given the same event. You should combine all commands into one function that will run them sequentially, and then call the function with one autocmd. Try that and update your question with the results. – Tumbler41 Apr 21 '17 at 15:11

Your Answer

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

Browse other questions tagged or ask your own question.