3

I have skeleton template files for various filetypes, and when I create a new file based on these templates, the cursor always starts at the top of the file. It would be more convenient if it started at the bottom.

Can I use a ftplugin or an autocmd to change the cursor's initial position?

8
  • Could give us more indication about your template files? Which autocmd trigger them such that we can propose you ways to ammend these commands? Nov 6, 2023 at 5:22
  • 1
    What did you try?
    – romainl
    Nov 6, 2023 at 6:05
  • @romainl I tried looking in the manual.
    – paradroid
    Nov 6, 2023 at 7:12
  • 1
    @NormanGray That works with existing files, but my question regards new files which start with a skeleton template. For example, you could start a bash script with the hashbang already present. vimtricks.com/p/vim-file-templates
    – paradroid
    Nov 7, 2023 at 14:20
  • 1
    @Friedrich Sometimes, yes, but most of the time vi is actually nvi (version 2.2.0 is what's currently on FreeBSD). Which is why, on platforms where vi is actually Vim, I have it aliased to vi -C, to avoid confusing myself. That, or ed, of course... Nov 8, 2023 at 9:44

1 Answer 1

4

If you associate a template to a file extension using the following autocmd:

autocmd! BufNewFile *.foo 0r ~/vimfiles/template/template.foo

The autocmd import the ~/vimfiles/template/template.foo when you create a new buffer that matches the *.foo pattern.

Then you could amend it using like:

autocmd! BufNewFile *.foo 0r ~/vimfiles/template/template.foo | $

Where the :$ command moves the cursor at the end of the buffer.

2
  • Great, thanks. I couldn't find anything about it.
    – paradroid
    Nov 6, 2023 at 7:13
  • Thank for the feedback :-) Nov 6, 2023 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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