Executing a command when opening file

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Executing a command when opening file

J.A.J. Pater

Dear list members,

Since not all commands are allowed in a modeline.
I guess inoremap is one of them.

How could I use such a comment 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.

Thanks in advance,

Adriaan.


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Executing a command when opening file

J.A.J. Pater

Sorry for my bad spelling.
Correction:
> Not all commands are allowed in a modeline, I guess inoremap is one of
> them.
>
> How could I use such a command ... (not comment ...)


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Executing a command when opening file

Benjamin Fritz
In reply to this post by J.A.J. Pater



On Sep 14, 8:17 am, "J.A.J. Pater" <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: Executing a command when opening file

J.A.J. Pater

Ben, thanks for your answer!

I think your answer is the way to go.

 > 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!"

I see, but I'd be often editing my .vimrc e.g. when changing the
name of a file.
So I thought about having vim check for a kind of per file vimrc file
(e.g. .example.txt.fvimrc for example.txt) so I could automatically
execute some functions, commands etc.  without having to put anything
inside the file (example.txt).

But in order to pursue that lead I'll have to read a little more about
programming vim I guess.

Another question tied in with my question is if (and if yes: how)
I can keep mappings (like inoremap) or functions, local
(constrained to one buffer or file).

But maybe I should start a new thread for that?

Thanks,

Adriaan.


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Executing a command when opening file

Benjamin Fritz



On Sep 16, 8:14 am, "J.A.J. Pater" <[hidden email]> wrote:

> Ben, thanks for your answer!
>
> I think your answer is the way to go.
>
>  > 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!"
>
> I see, but I'd be often editing my .vimrc e.g. when changing the
> name of a file.
> So I thought about having vim check for a kind of per file vimrc file
> (e.g. .example.txt.fvimrc for example.txt) so I could automatically
> execute some functions, commands etc.  without having to put anything
> inside the file (example.txt).
>
> But in order to pursue that lead I'll have to read a little more about
> programming vim I guess.
>

The filereadable() function may be useful to determine if there is a
file of a given name on your disk, if you make sure that the "per-
file .vimrc" is named in a very predictable way so that you can build
the name from the name of the file you're editing. The :source command
will run it for you (along with :execute so that you can use a
variable name in the :source command).

> Another question tied in with my question is if (and if yes: how)
> I can keep mappings (like inoremap) or functions, local
> (constrained to one buffer or file).
>
> But maybe I should start a new thread for that?
>

Yes, you should start new threads for new questions or you stand the
chance of it being missed.

For this particular question, see :help :map-local. If that doesn't
solve it, start a new thread, don't reply here on this new topic.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---