inkarkat / vim-mark

Highlight several words in different colors simultaneously.
http://www.vim.org/scripts/script.php?script_id=2666
266 stars 32 forks source link

Setting mark as part of vim modeline #31

Closed Ran4 closed 4 years ago

Ran4 commented 4 years ago

Motivation

I sometimes make custom marks when writing text files - highlighting certain words to make the text more readable. It would be neat to not lose this the next time I open the file.

Request and Purpose

Support setting marks through modelines. E.g. adding this to the top of a file:

# vim: mark1 /foo/

I could not find a way of doing this in the help file, and the string modeline is nowhere in the help file, so I don't think this is possible at the moment?

Alternatives

Saving the file as some custom file extension .foo, then :!echo "1Mark foo" > ~/.vim/ftplugin/foo.vim

This is a reasonable approach if you have many files with similar file extensions, but it creates a bunch of crap in your vim config folder if you have single files. It also forces you to use "wrong" file extensions.

inkarkat commented 4 years ago

Vim's built-in modelines are not extensible, so a separate parser would have to be developed. That would be a lot of effort for an ancillary feature. Fortunately, I already have a separate plugin that lets you execute arbitrary commands in extended modelines: ModelineCommands.vim.

Such a modeline would look like this:

# vimcommand: 1Mark! /foo/:

And you probably would want to extend the whitelist in g:ModelineCommands_ValidCommandPattern so that (frequently changing) :Mark commands will be accepted by the plugin without confirmation from the user. (For security reasons, only a safe subset of the powerful Vim commands are allowed by default; security is also the main argument against including such functionality within Vim itself.) Feel free to share such configuration here; I might even include it in the plugin's documentation then.

inkarkat commented 4 years ago

I don't like your alternative of using a custom file extension; even when using a compound filetype (e.g. script.foo.pl), this is very invasive, and a renaming often is not possible. Rather, I would define :autocmds on those file paths, maybe localized via a local vimrc plugin (e.g. my localrc fork). I also have an :autocmd that automatically sources a <filename>.<ext>.vim script next to a <filename>.<ext> file, for file-specific configuration (that is larger than my custom modelines would comfortably store, or where modelines are not possible).