Skip to content

Commit 9dc509a

Browse files
committed
vimrc: Refactor for sustainability and portability
1 parent 3a440a2 commit 9dc509a

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

vimrc

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"------------------------------------------------------------------------------
2-
" Basic Recommended Settings (optional)
2+
" Basic Recommended Settings
33
"------------------------------------------------------------------------------
44
if v:version >= 800
55
set nocompatible
66
syntax on
77
filetype plugin indent on
88
endif
99

10-
" For older Vim versions (if needed):
10+
"------------------------------------------------------------------------------
11+
" Fallback for older Vim (comment out if unnecessary)
12+
"------------------------------------------------------------------------------
1113
" set nocompatible
1214
" syntax enable
1315
" filetype plugin on
@@ -16,11 +18,19 @@ endif
1618
"------------------------------------------------------------------------------
1719
" Helper Function for Conditional Sourcing
1820
"------------------------------------------------------------------------------
19-
function! SourceIfExists(file) abort
20-
if filereadable(expand(a:file))
21-
execute 'source ' . fnameescape(a:file)
22-
endif
23-
endfunction
21+
if has('vim9script')
22+
def SourceIfExists(file: string)
23+
if filereadable(expand(file))
24+
execute 'source ' .. fnameescape(file)
25+
endif
26+
enddef
27+
else
28+
function! SourceIfExists(file) abort
29+
if filereadable(expand(a:file))
30+
execute 'source ' . fnameescape(a:file)
31+
endif
32+
endfunction
33+
endif
2434

2535
"------------------------------------------------------------------------------
2636
" Session / Directory Locations
@@ -50,8 +60,7 @@ set nowritebackup
5060
set noswapfile
5161

5262
" Disable matchparen plugin (avoid glitchy behavior)
53-
let g:loaded_matchparen = 1
54-
" or use `:NoMatchParen`
63+
let g:loaded_matchparen = 1 " or use `:NoMatchParen`
5564

5665
" Automatically cd to directory of opened file
5766
set autochdir
@@ -68,8 +77,11 @@ endif
6877
"------------------------------------------------------------------------------
6978
" Netrw Tweak
7079
"------------------------------------------------------------------------------
71-
" Fix netrw so that its buffer is hidden when leaving
72-
autocmd FileType netrw setlocal bufhidden=delete
80+
augroup MyNetrwFix
81+
autocmd!
82+
" Fix netrw so that its buffer is hidden on exit
83+
autocmd FileType netrw setlocal bufhidden=delete
84+
augroup END
7385

7486
"------------------------------------------------------------------------------
7587
" Truecolor Support (especially under tmux)
@@ -81,31 +93,30 @@ if exists('+termguicolors')
8193
endif
8294

8395
"------------------------------------------------------------------------------
84-
" Source Shared/Plugin Files
96+
" Source Additional Files (Plugins, Settings, etc.)
8597
"------------------------------------------------------------------------------
86-
call SourceIfExists("$HOME/.vim/plugin_loader.vim")
98+
call SourceIfExists('$HOME/.vim/plugin_loader.vim')
8799
call plugin_loader#PlugInit()
88100
call settings#LoadSettings()
89-
call SourceIfExists("$HOME/.vim/settings/highlight.vim")
101+
call SourceIfExists('$HOME/.vim/settings/highlight.vim')
90102

91103
"------------------------------------------------------------------------------
92104
" Color Schemes - Fallback Logic
93105
"------------------------------------------------------------------------------
106+
" Using an assumed function lib#ColorSchemeExists() to check if a scheme is present
94107

95-
" Typically, you'll have a function or autoload script lib#ColorSchemeExists()
96-
" to check if a color scheme is installed. Keeping your existing logic:
97-
if has('nvim') && lib#ColorSchemeExists("tokyonight-moon")
108+
if has('nvim') && exists('*lib#ColorSchemeExists') && lib#ColorSchemeExists('tokyonight-moon')
98109
colorscheme tokyonight-moon
99-
elseif lib#ColorSchemeExists("tokyonight")
110+
elseif exists('*lib#ColorSchemeExists') && lib#ColorSchemeExists('tokyonight')
100111
colorscheme tokyonight
101-
elseif lib#ColorSchemeExists("catppuccin_mocha")
112+
elseif exists('*lib#ColorSchemeExists') && lib#ColorSchemeExists('catppuccin_mocha')
102113
colorscheme catppuccin_mocha
103-
elseif lib#ColorSchemeExists("gruvbox")
114+
elseif exists('*lib#ColorSchemeExists') && lib#ColorSchemeExists('gruvbox')
104115
colorscheme gruvbox
105-
elseif lib#ColorSchemeExists("gruvbox-material")
116+
elseif exists('*lib#ColorSchemeExists') && lib#ColorSchemeExists('gruvbox-material')
106117
let g:gruvbox_material_disable_italic_comment = 1
107118
colorscheme gruvbox-material
108-
elseif lib#ColorSchemeExists("everforest")
119+
elseif exists('*lib#ColorSchemeExists') && lib#ColorSchemeExists('everforest')
109120
if has('termguicolors')
110121
set termguicolors
111122
endif
@@ -114,7 +125,7 @@ elseif lib#ColorSchemeExists("everforest")
114125
let g:everforest_transparent_background = 2
115126
let g:everforest_disable_italic_comment = 1
116127
colorscheme everforest
117-
elseif lib#ColorSchemeExists("desert-warm-256")
128+
elseif exists('*lib#ColorSchemeExists') && lib#ColorSchemeExists('desert-warm-256')
118129
colorscheme desert-warm-256
119130
else
120131
colorscheme desert
@@ -123,4 +134,4 @@ endif
123134
"------------------------------------------------------------------------------
124135
" Local Customizations
125136
"------------------------------------------------------------------------------
126-
call SourceIfExists("$HOME/.vimrc.local")
137+
call SourceIfExists('$HOME/.vimrc.local')

0 commit comments

Comments
 (0)