How to map differently based on the file suffix

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

How to map differently based on the file suffix

Peng Yu

Hi,

I have the following map in my .vimrc. It won't tell the difference
between the file types.

map <F12> <ESC>:w<kEnter>:make<kEnter>:cope<kEnter>

I want to change it such that <F12> will execute the following command
if the file ends with the suffix .mp.
mptopdf -latex filename.mp

Could you help me on this?

Thanks,
Peng


--~--~---------~--~----~------------~-------~--~----~
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: How to map differently based on the file suffix

Michael F. Lamb

Peng Yu wrote:

> I have the following map in my .vimrc. It won't tell the difference
> between the file types.
>
> map <F12> <ESC>:w<kEnter>:make<kEnter>:cope<kEnter>
>
> I want to change it such that <F12> will execute the following command
> if the file ends with the suffix .mp.
> mptopdf -latex filename.mp
>
> Could you help me on this?

Create a file ~/.vim/ftplugin/mp.vim containing your map:

        " ~/.vim/ftplugin/mp.vim
        map <f12> <esc>:!mptopdf -latex %<cr>

You can find out more about ftplugins, :h ftplugin and :h ftplugins.

Since vim already recognizes .mp as an extension-based filetype, it will
find your ftplugin file. For unknown extensions there's a couple more
steps but that's explained in :h new-filetype.

--~--~---------~--~----~------------~-------~--~----~
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: How to map differently based on the file suffix

Larson, David
In reply to this post by Peng Yu

Hi Peng,

To fix this, move the mapping from .vimrc to .vim/ftplugin/mp.vim. This
file will only be sourced if the filetype is mp.

David

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
Behalf

> Of Peng Yu
> Sent: Wednesday, July 25, 2007 3:23 PM
> To: vim_use
> Subject: How to map differently based on the file suffix
>
>
> Hi,
>
> I have the following map in my .vimrc. It won't tell the difference
> between the file types.
>
> map <F12> <ESC>:w<kEnter>:make<kEnter>:cope<kEnter>
>
> I want to change it such that <F12> will execute the following command
> if the file ends with the suffix .mp.
> mptopdf -latex filename.mp
>
> Could you help me on this?
>
> Thanks,
> Peng
>
>
>

--~--~---------~--~----~------------~-------~--~----~
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: How to map differently based on the file suffix

Tony Mechelynck
In reply to this post by Peng Yu

Peng Yu wrote:

> Hi,
>
> I have the following map in my .vimrc. It won't tell the difference
> between the file types.
>
> map <F12> <ESC>:w<kEnter>:make<kEnter>:cope<kEnter>
>
> I want to change it such that <F12> will execute the following command
> if the file ends with the suffix .mp.
> mptopdf -latex filename.mp
>
> Could you help me on this?
>
> Thanks,
> Peng

I assume mptopdf is an external command. Then:

autocmd BufNewFile,BufReadPost *.mp map <buffer> <F12>
                                                \ :w| !mptopdf -latex %<CR>

(Vim automagically replaces % by the current file name).

The <buffer> argument is important to avoid clobbering the mapping for other
files.


If your special mappings are for a certain file "type" rather than a certain
file "extension" you can also create a filetype-after-plugin. Example for C:

----- ~/.vim/after/ftplugin/c.vim (Unix) -----
----- ~/vimfiles/after/ftplugin/c.vim (Windows) -----
inoremap <buffer>    [    []<Left>
inoremap <buffer>    (    ()<Left>
inoremap <buffer>    {    {<CR>}<Up><End>
inoremap <buffer>    /*   /*<Space><Space>*/<Left><Left><Left>
inoremap <buffer>    "    ""<Left>
" we intentionally don't map the single quote because we want to be able
" to use English apostrophes in comments
----- end after/ftplugin/c.vim -----


Best regards,
Tony.
--
Parkinson's Fourth Law:
        The number of people in any working group tends to increase
regardless of the amount of work to be done.

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