On Sep 14, 8:17 am, "J.A.J. Pater" <[email protected]> wrote:
> Since not all commands are allowed in a modeline.
> I guess inoremap is one of them.
>
> How could I use such a command only for a specific file
> (not for a filetype)?
>
> Vim help gives the hint:
>
>  >If you would like to do something else than setting an option, you could
>  >define an autocommand that checks the file for a specific string.  For
>  >example:
>  >>    au BufReadPost * if getline(1) =~ "VAR" | call SetVar() | endif
>
> Could I use something like this in my .vimrc?
>
> au BufReadPost * if getline(1) =~ "% inoremap ' x" | inoremap ' x |
> endif
>
> Are better options available?
> For instance if I want to execute a lot of commands not allowed from
> modeline?
>
> I know about the localvimrc plugin and the (less safe) dirsettings
> plugin.
> But they work directory specific and not file specific.

I don't think there's really any "safe" way to execute arbitrary Vim
commands in arbitrary files in a modeline-fashion. Modelines only
allow setting options, and not even all of those, for this reason. You
could execute some commands using :sandbox to make them safer, but
inoremap is not one of them.

If by "specific file" you mean a VERY specific file of a given name
(or list of names) then it would be a little safer to structure your
autocmd to only file for those specific files:

au BufReadPost file1,file2,file3 echomsg "doing the command!"

See :help autocmd-patterns

As for the command itself, I would do as it says in the help: look for
a specific string. Pick a string that you will not use for anything
else, like [:DO_A_VIM_COMMAND:] and use THAT as your comparison using
getline. Then, use the substitute() function on the string to get rid
of everything but the command you wish to execute, and finally execute
it with the :execute command.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to