簡體   English   中英

自動命令無法在Vim中工作,如何根據文件類型設置makeprg?

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

我使用Vim編輯器進行編程,這是我面臨的問題。

我使用多個選項卡同時編輯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\ %

即我想要的是當我切換到帶有Python文件的選項卡並運行:make ,它應該運行:!python3 % ,當我切換到帶有C ++文件的選項卡並運行時:make它應該運行:!g++ -Werror -Wextra -Wall -ansi -pedantic-errors -g %

然而它不起作用,每次我切換標簽並運行:make ,它會嘗試執行!g++ -Werror -Wextra -Wall -ansi -pedantic-errors -g %當我運行時:set ft? 在2個文件(即Python和C ++)上檢查文件類型是否已被正確識別,我得到了正確的結果,即python和cpp。

那為什么這個自動命令不起作用? 我錯過了什么嗎? 謝謝你的耐心

嘗試使用setlocal而不是set 例如:

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 ,您將看到該設置為“全局或本地緩沖全局 - 本地 ”。 這意味着您可以使用set來全局應用makeprg,或者使用setlocal來覆蓋單個緩沖區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM