Skip to content

Commit

Permalink
Always load defaults.vim
Browse files Browse the repository at this point in the history
This has always caused confusion, when defaults.vim is loaded and when
not. So let's always load defaults.vim even when a `.vimrc` files has
been found.

Users who don't want this slightly backwards compatible behaviour, can
opt-out of this by setting the $VIM_NO_SOURCE_DEFAULTS environment
variable

Signed-off-by: Christian Brabandt <[email protected]>
  • Loading branch information
chrisbra committed May 31, 2024
1 parent 490740d commit 10409fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
35 changes: 20 additions & 15 deletions runtime/doc/starting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1063,23 +1063,28 @@ giving the mapping.

Defaults without a .vimrc file ~
*defaults.vim* *E1187*
If Vim is started normally and no user vimrc file is found, the
$VIMRUNTIME/defaults.vim script is loaded. This will set 'compatible' off,
switch on syntax highlighting and a few more things. See the script for
details. NOTE: this is done since Vim 8.0, not in Vim 7.4. (it was added in
patch 7.4.2111 to be exact).

This should work well for new Vim users. If you create your own .vimrc, it is
recommended to add these lines somewhere near the top: >
If Vim is started normally, the $VIMRUNTIME/defaults.vim script is loaded.
This will set 'compatible' off, switch on syntax highlighting and a few more
things. See the script for details. NOTE: this is done since Vim 8.0, not in
Vim 7.4. (it was added in patch 7.4.2111 to be exact).

This should work well for new Vim users. If you create your own .vimrc,
defaults.vim will always be loaded before your own .vimrc, so that you
can revert settings that have been set. If you don't like some of the
defaults, you can still source defaults.vim and revert individual settings.
See the defaults.vim file for hints on how to revert each item.

Note: since patch 9.1.XXXX Vim will always load the defaults.vim file even
when a user vimrc file has been found. If you do not want this behaviour, you
can opt-out of sourcing defaults.vim by setting the environment variable
`$VIM_NO_SOURCE_DEFAULTS`.

Previously you had to explicitly source defaults.vim by adding those lines at
the top: >
unlet! skip_defaults_vim
source $VIMRUNTIME/defaults.vim
Then Vim works like before you had a .vimrc. Copying $VIMRUNTIME/vimrc_example
is way to do this. Alternatively, you can copy defaults.vim to your .vimrc
and modify it (but then you won't get updates when it changes).

If you don't like some of the defaults, you can still source defaults.vim and
revert individual settings. See the defaults.vim file for hints on how to
revert each item.
<
*skip_defaults_vim*
If you use a system-wide vimrc and don't want defaults.vim to change settings,
set the "skip_defaults_vim" variable. If this was set and you want to load
Expand Down
7 changes: 6 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3261,6 +3261,7 @@ source_startup_scripts(mparm_T *parmp)
/*
* Try to read initialization commands from the following places:
* - environment variable VIMINIT
* - defaults.vim unless $VIM_NO_SOURCE_DEFAULTS is set
* - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
* - second user vimrc file ($VIM/.vimrc for Dos)
* - environment variable EXINIT
Expand All @@ -3270,6 +3271,10 @@ source_startup_scripts(mparm_T *parmp)
*/
if (process_env((char_u *)"VIMINIT", TRUE) != OK)
{
int loaded_vimrc = NOTDONE;
if (mch_getenv("VIM_NO_SOURCE_DEFAULTS") == NULL)
loaded_vimrc = do_source((char_u *)VIM_DEFAULTS_FILE, FALSE,
DOSO_NONE, NULL);
if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
DOSO_VIMRC, NULL) == FAIL
#ifdef USR_VIMRC_FILE2
Expand All @@ -3295,7 +3300,7 @@ source_startup_scripts(mparm_T *parmp)
&& do_source((char_u *)USR_EXRC_FILE2, FALSE,
DOSO_NONE, NULL) == FAIL
#endif
&& !has_dash_c_arg)
&& !has_dash_c_arg && loaded_vimrc != OK)
{
// When no .vimrc file was found: source defaults.vim.
if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
Expand Down

0 comments on commit 10409fe

Please sign in to comment.