简体   繁体   中英

Makefile tabs when I have spaces in .vimrc

So in my .vimrc file, I have my tabs set to 2 spaces. I know that the Makefile doesn't like this and what I have been doing is temporarily deleting the .vimrc conditions I have and editing the Makefile and then adding everything back to the .vimrc file. Is there any way I can use vim on the Makefile edit it without having to do all the changes? My Makefiles are very simple. Like here below

all: main.cpp
  g++ -g -Wall main.cpp

Here is my .vimrc

set number
colorscheme ron 
set expandtab
: filetype on
: syntax on
: filetype indent on
set tabstop=2
set shiftwidth=2

You're missing out on Vim's :help filetype-plugin functionality. If you put

:filetype plugin on

(you can combine all filetype commands from your ~/.vimrc as one :filetype indent plugin on ), and assuming your Makefile has a regular name so that it is detected (or you manually :setfiletype make ), this will load buffer-local settings from $VIMRUNTIME/ftplugin/make.vim , which contains these indent settings:

" Make sure a hard tab is used, required for most make programs
setlocal noexpandtab softtabstop=0

If you explicitly don't want the filetype plugin feature (but be aware that any built-in option can be tweaked via user-configuration ( :help after-directory ), and the defaults are useful in general), you could emulate this setting via :autocmd in your ~/.vimrc , too:

autocmd FileType make setlocal noexpandtab softtabstop=0

I think that setting your editor properly is the correct answer. However I'll just point out that if you use GNU make 3.82 or higher you could change the recipe introduction character from a TAB to something else using .RECIPEPREFIX as in:

.RECIPEPREFIX = |

all: main.cpp
| g++ -g -Wall main.cpp

As per the docs the prefix must be a single character, it cannot be a string.

您可以在您的Makefile中添加vim modeline,作为第一行或最后一行:

# vim: set noexpandtab:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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