1

I have a template being used like so:

autocmd BufNewFile ~/Documents/wiki/diary/*.mkd :silent 0r !vimwiki-diary-template.py

But earlier in my autocommands the cursor position is being set which annoyingly moves the cursor to the end of file in this vimwiki diary template. So I want to set it to a particular position.

Can I do something like :6<Enter> at the end of my BufNewFile command to then move the cursor to line#6?

Something like:

autocmd BufNewFile ~/Documents/wiki/diary/*.mkd :silent 0r !vimwiki-diary-template.py && exe :6<Enter>

or maybe

autocmd BufNewFile ~/Documents/wiki/diary/*.mkd :silent 0r !vimwiki-diary-template.py
   \ normal 6gg|zt

or something else?

Is it possible to combine these two actions together?


solution: adding | normal 6G instead of bash-like &&... at the end of the autocmd works. And it can even be simply done with | 6.

  • I find it odd that your template rule only applies to new files, so I'm wondering how the BufReadPost applies there... (Update: It's probably through the :filetype detect.) But, still, how will Vim have a location for the '" mark if the file is new? Perhaps setting the mark (with normal! m" or similar) from the BufNewFile would be a good approach. – filbranden Sep 22 at 0:46
  • Um... I'll add more code to explain... – alec Sep 22 at 0:49
  • Maybe BufReadPost does not apply here? I don't know why vim always puts the cursor at the end of the file here, I assumed it was because of how the template created the new file somehow emulated a cursor-position, but that's just my ignorant guess. – alec Sep 22 at 0:54
  • re: ""Perhaps setting the mark (with normal! m" or similar) from the BufNewFile would be a good approach"" can that be added in augroup templates... to the autocmd BufNewFile? How does that get done? I don't know who commands can be combined like that. – alec Sep 22 at 0:58
  • I don't think it has to do with the autocmd that restores the cursor... But simply after executing a :read command, Vim will leave the cursor at the end of the input that was read. Nevertheless, just setting the cursor after reading the input should be enough. Ben's solution below should work. – filbranden Sep 22 at 1:04
2

Vim’s command separator is |, but it cannot come after certain commands. In this case the simplest workaround is execute:

autocmd Event pattern execute '0read !cmd' | 6

Another solution is to use multiple autocommands, as they are executed in sequence:

autocmd Event pattern 0read !cmd
autocmd Event pattern 6
| improve this answer | |
  • 1
    Did you use execute? With the !, the bar is consumed as part of a shell program. – D. Ben Knoble Sep 22 at 1:11
  • 1
    @alec I included execute in the code snippet; perhaps I am missing something, but cmd is your python script (I’m on mobile, and copy/paste is a bit hard, so I got lazy) – D. Ben Knoble Sep 22 at 1:14
  • 1
    Why not put the error and the code in a comment? – D. Ben Knoble Sep 22 at 1:20
  • 2
    No escape necessary! | needs to be escaped when being used in an argument. But apparently works on its own equivalent to bash && in autocmd to give multiple commands in one line. – alec Sep 22 at 1:34
  • 2
    oh it was because I had the autocmd redundantly in my vimrc, so the script was being invoked twice, geez. Thank you both so much <3 – alec Sep 22 at 2:01

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.