Vim Inoremap for Specific Filetypes

Vim inoremap for specific filetypes

Use autocmd FileType:

autocmd FileType php,c,java inoremap ( ()<Esc>i

Run a setting in VIM for specific filetypes only

The .vimrc is read only once on startup. So it's a right place to define a highlight (without adding any conditional):

highlight OverLength ctermbg=red ctermfg=white guibg=#FFD9D9

But if you want to make a match each time you open a file (with or without a conditional), you have to wrap your :match command inside an autocommand (in your .vimrc), for example:

au BufReadPost * if &ft=='python' || &ft!='r' | match OverLength /\%81v.\+/ | endif

Change the mapping of F5 on the basis of specific file type

There are problems with both given answer and original mapping. First of all, for buffer-local mappings there is *map <buffer>. Second, with <buffer> you don’t need to use BufEnter events and can instead use Filetype which are launched only once. Third, you have one error (2.), one potential problem (1.) and one place that can be optimized in original mappings:

  1. you should not be using imap, it makes it very easy to accidentally break old mappings when adding new ones
  2. !python % will break once file contain a special symbol (space, semicolon, quot, dollar, …)
  3. using :update instead of :write avoids useless writes in some cases

My variant:

autocmd Filetype c,cpp  inoremap <buffer> <F5> <C-o>:update<Bar>execute '!make '.shellescape(expand('%:r'), 1)<CR>
autocmd Filetype python inoremap <buffer> <F5> <C-o>:update<Bar>execute '!python '.shellescape(@%, 1)<CR>
autocmd Filetype java inoremap <buffer> <F5> <C-o>:update<Bar>execute '!javac '.shellescape(@%, 1)<CR>

Is there a better way to map command call based on filetype

You need to do 2 things:

  • Create a mapping local to a specific buffer by using the <buffer> option for noremap.
  • Load the mappings for just a specific filetype.

This can be done via an autocmd and FileType event in your .vimrc like so:

autocmd FileType perl noremap <buffer> <F5> :!perl %:p<cr>

The other way option is by creating a filetype plugin. (see :h ftplugin for more details)

A simple example is do create a file named, ~/.vim/ftplugin/perl.vim and place your mappings inside like so:

nnoremap <buffer> <F5> :!perl %:p<cr>

I personally lean more towards the ftplugin approach but having a everything in your .vimrc file can be nice.

For more help see:

:h :au
:h FileType
:h map-local
:h ftplugin

Making commands work only for chosen filetype(s)

You have two methods…

  • Create a self-clearing group in your vimrc and add as many autocommands as needed:

    augroup Tex
    autocmd!
    autocmd FileType tex nnoremap <buffer> <F2> :Latexmk<cr>
    autocmd FileType tex nnoremap <buffer> <F3> :LatexView<cr>
    autocmd FileType tex inoremap <buffer> <F2> <Esc>:Latexmk<cr>a
    autocmd FileType tex inoremap <buffer> <F3> <Esc>:LatexView<cr>a
    augroup END
  • Use a ftplugin:

    Put the following in after/ftplugin/tex.vim:

    nnoremap <buffer> <F2> :Latexmk<cr>
    nnoremap <buffer> <F3> :LatexView<cr>
    inoremap <buffer> <F2> <Esc>:Latexmk<cr>a
    inoremap <buffer> <F3> <Esc>:LatexView<cr>a

The second method is recommended because it is a lot cleaner and less expensive than the first.

vim: would like it to turn settings on only for certain file types

My answer to that question still applies:

Put autocmd commands based on the file suffix in your ~/.vimrc

autocmd BufRead,BufNewFile   *.txt set wrap linebreak

As Luc says, you might prefer to

autocmd BufRead,BufNewFile   *.txt setlocal wrap linebreak

if you're likely to open txt and non-txt files at the same time.

Vim remap only if source file is in LaTeX

You want Fietype specific autocmds:

autocmd Filetype tex,latex inoremap ½ \frac{1}{2}

Try something like that. This depends on the Filetype option being on (see here). You can set it with set Filetype on

You can also use an autocmd that checks for the filename when reading or creating a new file:

autocmd BufNewFile,BufRead *.latex inoremap ½ \frac{1}{2}

Also, you should confirm that hitting the 1/2 key on your keyboard actually inputs the 1/2 symbol, otherwise obviously this isn't going to work. Regardless, you can figure out what code the 1/2 key actually inserts by (in insert mode) hitting ctrl-v and then the physical key.

How does Vim declare the filetype for Js or other types

As shown in the article linked in the comments, you can do this using an autocmd in your vimrc. However, I find it can be cleaner to instead leverage vim's runtime path structure to set filetype-specific settings.

TL;DR

Put your filetype specific commands such as inoremap ,f ()... in ~/.vim/after/ftplugin/javascript.vim. If any of these directories don't exist, create them. You can do similar things for other languages (e.g. put html bindings in ~/.vim/after/ftplugin/html.vim, etc).

Explanation

The vim runtime path is just where vim looks for configuration files and related things when it starts up (just like how it reads ~/.vimrc). First it will read some system-wide things from /etc, and then it will read the contents of ~/.vim. The after directory, as the name suggests, will be read after other settings, and though it is probably not necessary to put this type of thing in after, it won't hurt (it makes sure it won't get overwritten by system-wide configuration). Within after is the ftplugin directory, which is where filetype-specific config will go. You could also put an ftplugin directory directly in ~/.vim, and it would do the same thing (except it would get read earlier. The files you put in this directory will work similar to how the autocmd would work. I just prefer to do things this way, because I find it neater than having a bunch of autocmds in my vimrc. For more detailed info about the vim runtime path and how you can use it, check out this video. The whole thing isn't about the runtime path, but it does have a very good explanation in there.

Of course, you can still use autocmds if you would like to (and I think your only problem there is that your autocmd must say Filetype javascript instead of Filetype js).



Related Topics



Leave a reply



Submit