ctxrr
ctxrr

Reputation: 393

how does vim check file type?

My vimrc is as below:

nnoremap <buffer> <F9> :wa<CR> :exec '!python' shellescape(@%, 1)<cr>

nnoremap <buffer> <F10> :wa<CR> :!make && ./%< && make clean<CR>

nnoremap <buffer> <F11> :wa<CR> :!gcc % -o %< && ./%< && rm %<<CR>

So I can use <F9>to build python and <F11> to build C.

I wonder if I can add some statements into vimrc so Vim can check the file type automatically and I can use only one hotkey to build different file by if...else statements.

Upvotes: 1

Views: 303

Answers (2)

ctxrr
ctxrr

Reputation: 393

aha I found a way to solve this problem by using autocmd

autocmd BufRead *.py nnoremap <buffer> <F9> :wa<CR> :exec '!python' shellescape(@%, 1)<cr>
autocmd BufRead *.cpp nnoremap <buffer> <F9> :wa<CR> :!g++ % -o %< && ./%< && rm %<<CR>
autocmd BufRead *.c nnoremap <buffer> <F9> :wa<CR> :!gcc % -o %< && ./%< && rm %<<CR>

That is what I need !

Upvotes: 2

Ben Beirut
Ben Beirut

Reputation: 763

Create a file python.vim (the name does not matter) under the directory ~/.vim/after/ftplugin/python or equivalent on a different operating system.

Place the Python binding in it:

nnoremap <buffer> <F9> :wa<CR> :exec '!python' shellescape(@%, 1)<cr>

Do the same for C.

Upvotes: 1

Related Questions