Autocommand does not work in Vim, how to install makeprg based on file type?

I am using the Vim editor for programming, and here is the problem I am facing.

I use several tabs to edit C ++ and Python files at the same time, and I added the following to my .vimrc file

filetype plugin indent on
au filetype python set mp=python3\ %
au filetype cpp set mp=g++\ -Werror\ -Wextra\ -Wall\ -ansi\ -pedantic-errors\ -g\ %

those. all I want is that when I switch to the tab with the Python file and run :make, it should launch :!python3 %, and when I switch to the tab with the C ++ file and launch :make, it should run:!g++ -Werror -Wextra -Wall -ansi -pedantic-errors -g %

However, it does not work, and every time I switch the tab and launch :make, it tries to execute !g++ -Werror -Wextra -Wall -ansi -pedantic-errors -g %, and when I launch :set ft?in 2 files (i.e. Python and C ++) to check whether the file types have been correctly identified or not, I get the correct result, i.e. python and cpp.

Then why is this auto command not working? Am I missing something? Thank you for your patience.

+3
source share
1 answer

Try using setlocalinstead set. eg:.

filetype plugin indent on
au filetype python setlocal mp=python3\ %
au filetype cpp setlocal mp=g++\ -Werror\ -Wextra\ -Wall\ -ansi\ -pedantic-errors\ -g\ %

:help mp, , global-local '. , set makeprg setlocal .

+7

Source: https://habr.com/ru/post/1747134/


All Articles