Ftplugin
As mentioned do put settings pertaining to a certain file type in ftplugin.
Separate into Files
Break up code into different files. For example where my vimrc file is, there is a sub init/
dir which contains other files I wish to load at startup. To load them I use the below code. <sfile>
evaulates to the current file (init.vim
or .vimrc
in this case), and :h
means the head of it (the dir it is within). Here I am loading the files env.vim
, then git.vim
, etc..
source <sfile>:h/init/env.vim
source <sfile>:h/init/git.vim
source <sfile>:h/init/myplugins.vim
source <sfile>:h/init/visual.vim
Folds
I used to separate my init.vim into many other files, but it makes startup a little bit (insignificantly?) slower, but the main thing is ones setup becomes very fragmented, and constantly have to switch buffers when working on config. I moved to using folds. Refer to :help usr_28
for help on folds. Search down in particular for the section *28.6* Folding with markers
I break apart my init file as shown below. It shows a level1 KEYMAPPINGS
fold expanded. There are many level2 subsections (folds) such as map
, map!
, function keys
, visual mode
within the main level1 section. In the image a level2 fold is expanded (Popup Menu
keymappings`).

Firstly to enable {{{
and style folds set these options:
set foldmethod=marker
set foldcolumn=1
I use the following code to highlight the Level1 and Level2 section headings of the folds. _FoldLevel1
and _FoldLevel2
are highlight groups defined next.
augroup match_folds
autocmd!
" VimEnter handles at start up, WinNew for each window created AFTER startup.
" Regex matches { { { with an empty group in the middle so that vim does
" not create a fold in this code, then either a 1 or 2 then a space. Then
" zs is the start of the match which is the rest of the line then ze is
" the end of the match. Refer to :help pattern-overview
autocmd VimEnter,WinNew * let w:_foldlevel1_id = matchadd('_FoldLevel1', '{{\(\){1\ \zs.\+\ze', -1)
autocmd VimEnter,WinNew * let w:_foldlevel2_id = matchadd('_FoldLevel2', '{{\(\){2\ \zs.\+\ze', -1)
augroup END
I style my folds with:
" {{{ Folds
hi Folded guifg=#FF9999 guibg=#005050 gui=bold,italic
hi FoldColumn guifg=#FF9999 guibg=#005050 gui=bold
hi _FoldLevel1 guifg=#005050 guibg=#FF9999 gui=bold,italic
hi _FoldLevel2 guifg=#003030 guibg=#CC8080 gui=bold,italic
" }}}
Now place {{{1
to Create a level 1 esction, {{{2
to create a level 2 section: (I trimmed the file down, to a skeleton, normally each section is about a screen long).
" {{{1 GUI computer
set mouse=a " Enable use of the mouse for all modes
set number " Display line numbers on the left
set relativenumber
" {{{1 KEY MAPPINGS
" Can show a list of mappings with :map
" Note!!! Comments on a separate line.
" {{{2 All modes
map ; :
map! <M-;> <Esc>:
" {{{2 Map
" Map backspace to other buffer
noremap <BS> <C-^>
noremap <BS> <C-^>
" }}}2 <- OPTIONAL way to force closure of a section, if you want to create a blank line before the next section is automatically made with {{{1 or {{{2 etc
" {{{1 ANOTHER HEADING
" More stuff
filetype
tag -- for now maybe? It makes readers think the questions is about specific file types, which is not the case.