I am starting to learn programming and saw that it is possible to configure vim through the .vimrc file. But I'm having trouble creating more than one setting. I want a configuration for java and another for C, but I do not know how to only enable a configuration when programming in a particular language.
I found the following solution to my problem:
If it's okay to configure the local exceptions centrally, you can put such
autocmds into your~/.vimrc:
:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4(How to load different .vimrc file for different working directory?)
But I did not understand that solution.
I figured I should do the following:
I wanted to create a C language setting on the Desktop. So I put in Shell (I'm using Mac OS) the following command
vim ~/Desktop/.vimrcand put the desired configuration in this file.Then I put the following command in the file
~/.vimrc:autocmd BufRead,BufNewFile ~/Desktop/.vimrc setlocal ts=4 sw=4Then I went into the Shell and created a C file
vim ~/Desktop/myprogram.c, but I realized that my setup had not worked.
Obviously I did something wrong, but I could not find the error, because I'm still noob.
For that you need 3 files:
~/.vimrcand two scripts let's sayC-settings.vimandjava-settings.vimIn the first file, the
~/.vimrc, you need to include these autocommands:In the other files (
C-settings.vim&Java-settings.vim) you put the settings that you need for every type of file*.cand*.javaExample:
C-settings.vim
Java-settings.vim
Whenever you open a file with vim the latter will check the filetype first and the settings will be automatically configured.
Note: if the setting files are not in the same directory you can rename them
.exrc.