简体   繁体   English

自动命令无法在Vim中工作,如何根据文件类型设置makeprg?

[英]Autocommand not working in Vim, how to set makeprg based upon the filetype?

I use Vim editor for programming and here's the problem that I am facing. 我使用Vim编辑器进行编程,这是我面临的问题。

I am using multiple tabs to edit C++ and Python files simultaneously and I have added the following in my .vimrc file 我使用多个选项卡同时编辑C ++和Python文件,我在.vimrc文件中添加了以下内容

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

ie all I want is that when I switch to a tab with a Python file and run :make , it should run :!python3 % , and when I switch to a tab with a C++ file and run :make it should run :!g++ -Werror -Wextra -Wall -ansi -pedantic-errors -g % 即我想要的是当我切换到带有Python文件的选项卡并运行:make ,它应该运行:!python3 % ,当我切换到带有C ++文件的选项卡并运行时:make它应该运行:!g++ -Werror -Wextra -Wall -ansi -pedantic-errors -g %

However it's not working, and everytime I switch the tab and run :make , it tries to execute !g++ -Werror -Wextra -Wall -ansi -pedantic-errors -g % and when I run :set ft? 然而它不起作用,每次我切换标签并运行:make ,它会尝试执行!g++ -Werror -Wextra -Wall -ansi -pedantic-errors -g %当我运行时:set ft? on the 2 files(ie Python and C++) to check whether the filetypes have been correctly identified or not, I get the correct result ie python and cpp. 在2个文件(即Python和C ++)上检查文件类型是否已被正确识别,我得到了正确的结果,即python和cpp。

Then why is this autocommand not working? 那为什么这个自动命令不起作用? Am I missing something? 我错过了什么吗? Thanks for your patience 谢谢你的耐心

Try using setlocal instead of set . 尝试使用setlocal而不是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\ %

If you read :help mp , you'll see that the setting is 'global or local to buffer global-local '. 如果您阅读:help mp ,您将看到该设置为“全局或本地缓冲全局 - 本地 ”。 This means that you can use set to apply a makeprg globally, or setlocal to override it for a single buffer. 这意味着您可以使用set来全局应用makeprg,或者使用setlocal来覆盖单个缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM