Search code examples
vimmarkdownrc

How do you mark specific text on file load with your vimrc?


Problem

I use vim and vimwiki and have files that have predictable headings like ## Planning and ## Todo.

I want to do something like this in my vimrc:

/## Todo
mt

which will happen on file load if there is a ## Todo in the file.

But I'm not mapping a command with map or imap. It's also not a setting that can be set with set. So I am looking for some vimscript keyword that autostarts on file load.

Question

How do you search and mark on file load in the vimrc?


Solution

  • This looks like something autocmds can help with (see :h autocmd). You can have commands run on many different events, including on loading a certain file type. You could therefore have an autocmd like:

    autocmd FileType vimwiki silent! /## Todo/ | mark t
    

    You should be able to modify the event to only run this on the files that you want. The silent! part is there so you don't get an error message if there is no ## Todo in the file. The | command allows for multiple commands to be run, and the second command isn't run if the first is not successful.