VIM
WORKING VIM LIKE A LIGHTWEIGHT IDE
Initialization
01
:h vimrc
Execute Ex commands �(usu. from a vimrc file)
First, initialize (if defined) the system vimrc (see :version)�
$VIM/vimrc
Then, initialize the first found of the following user vimrc configurations:�
$VIMINIT (this is an environment variable)
$HOME/.vimrc
$HOME/.vim/vimrc
$EXINIT (this is an environment variable)
$HOME/.exrc
$VIMRUNTIME/defaults.vim
What if I don’t want to load anything?
vim -u NONE
vim -u NORC
Plugin / Package Scripts
02
:h load-plugins
Load plugin scripts
First, plugin scripts are loaded, which is equivalent to running
:runtime! plugin/**/*.vim (EXCEPT FOR rtp directories ending “after” - more on that later)
�Usually, this is $HOME/.vim/plugin/**/*.vim
Then, packages are loaded…
Usually, this is $HOME/.vim/pack/foo/start/bar/plugin/**/*.vim �(Whatever is in red is completely arbitrary - put any string there and it would load as a package)
�
WHAT!?
runtimepath?
packages?
plugins?
�wtf is …/pack/.../and/why/is/it/so/long
runtimepath
:h runtimepath
For Unix: runtimepath=$HOME/.vim,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,$HOME/.vim/after
“A List of directories which will be � searched for runtime files”
syntax/
compiler/
ftdetect/
filetype.vim
scripts.vim
autoload/
ftplugin/
plugin/
pack/
colors/
doc/
import/
keymap/
lang/
menu.vim
print/
spell/
tutor
indent/
Search order:
Each of the runtimepath directories (default 5) has this dir structure:
You’ll usually only add to 1. and 5., but will often examine 3. (system runtime)
syntax
:h syntax
“Enable customized syntax highlighting”
�When invoking :set syntax=xxx,
syntax file(s) named xxx.vim are sourced �from the runtimepath. Usually, an �autocommand does this if the filetype
can be inferred.
syntax/
compiler/
ftdetect/
filetype.vim�scripts.vim
autoload/
ftplugin/
plugin/
pack/
colors/
doc/
import/
keymap/
lang/
menu.vim
print/
spell/
tutor
indent/
System defaults usually defined in $VIMRUNTIME/syntax
EXAMPLE - �SSH known_hosts
The system runtimepath provides no syntax highlighting for SSH known_host files - �let’s create our own in syntax/sshknownhosts.vim
ftdetect/ + filetype.vim +
scripts.vim
:h new-filetype
“Filetype detection”
syntax/
compiler/
ftdetect/
filetype.vim
scripts.vim
autoload/
ftplugin/
plugin/
pack/
colors/
doc/
import/
keymap/
lang/
menu.vim
print/
spell/
tutor
#1 Override filetype AFTER default filetype detection | #2 Only create a new filetype AFTER default filetype detection | #3 Define filetype BEFORE default filetype detection (detectable by name) | #4 Define filetype BEFORE default filetype detection (detectable by content) |
Add :set filetype=file autocommand to a file in ftdetect/ | Add :setfiletype file autocommand to a file in ftdetect/ | Add :setfiletype file for all files you want to define in filetype.vim | Add :setfiletype file for all files you want to define in scripts.vim |
SO MANY WAYS OF DETECTING FILE TYPES:
System defaults usually defined in $VIMRUNTIME/filetype.vim
Example - SSH known_hosts
Get vim to automatically infer the file type so our custom syntax rules can be applied immediately upon opening the file
autoload
:h autoload
“Scripts loaded only when required”
�When you issue a call filename#funcname(), Vim searches in the autoload directory of runtimepath for a file called filename.vim and calls funcname()
syntax/
compiler/
ftdetect/
filetype.vim
scripts.vim
autoload/
ftplugin/
plugin/
pack/
colors/
doc/
import/
keymap/
lang/
menu.vim
print/
spell/
tutor
indent/
System defaults usually defined in $VIMRUNTIME/autoload
EXAMPLE - autoload
Define a script for uploading the script you are currently editing in vim to a predefined bucket in GCS, but only load this script when you want to do the upload
compiler
:h compiler
“Sets options for using a specific compiler”
�When invoking :compiler xxx,
compiler specific options from xxx.vim
are sourced to set a makeprg and
errorformat for vim-native compilation
syntax/
compiler/
ftdetect/
filetype.vim
autoload/
scripts.vim
ftplugin/
plugin/
pack/
colors/
doc/
import/
keymap/
lang/
menu.vim
print/
spell/
tutor
indent/
System defaults usually defined in $VIMRUNTIME/compiler
Example - Compiling Typescript
We want to natively integrate Typescript’s build tool tsc into our vim edit-compile-edit cycle
ftplugin
:h ftplugin
“Defines Vim scripts loaded for particular � files (applies to current buffer)
syntax/
compiler/
ftdetect/
filetype.vim�scripts.vim
autoload/
ftplugin/
plugin/
pack/
colors/
doc/
import/
keymap/
lang/
menu.vim
print/
spell/
tutor
indent/
System defaults usually defined in $VIMRUNTIME/ftplugin
Example - Automate Typescript Compilation
We now want to define a mechanism for easily triggering our Typescript builds without having to manually specifying compilation options every time
plugin
:h ftplugin
“Defines global plugins �automatically loaded when vim starts”
syntax/
compiler/
ftdetect/
filetype.vim�scripts.vim
autoload/
ftplugin/
plugin/
pack/
colors/
doc/
import/
keymap/
lang/
menu.vim
print/
spell/
tutor
indent/
System defaults usually defined in $VIMRUNTIME/plugin
Example - �Create Plugin for OSC 52 Yanks
We want to create a plugin that allows us to directly yank all text to our local clipboard by utilizing an xterm-compatible terminal’s OSC-52 escape sequence support
TIP (for future use, add to ~/.bashrc or ~/.zshrc): �alias vi='vi -c "let g:tty='\''$(tty)'\''"'
NOTE: THIS REMAINS THE MOST USEFUL TOOL I’VE EVER COME ACROSS -
PAY ATTENTION BECAUSE THIS WILL CHANGE YOUR LIFE
pack/
:h plugin
“Defines packages, directories that
contain 1+ plugins”
syntax/
compiler/
ftdetect/
filetype.vim�scripts.vim
autoload/
ftplugin/
plugin/
pack/
colors/
doc/
import/
keymap/
lang/
menu.vim
print/
spell/
tutor
indent/
Plugin Scripts (AGAIN)
02
:h load-plugins
Load plugin scripts
First, plugin scripts are loaded, which is equivalent to running
:runtime! plugin/**/*.vim (EXCEPT FOR rtp directories ending “after” - more on that later)
�Usually, this is $HOME/.vim/plugin/**/*.vim
Then, packages are loaded…
Usually, this is $HOME/.vim/pack/foo/start/bar/plugin/**/*.vim �(Whatever is in red is completely arbitrary - put any string there and it would load as a package)
�
Why packages when I have plugins?
syntax/
compiler/
ftdetect/
filetype.vim
scripts.vim
autoload/
ftplugin/
plugin/
colors/
doc/
…
Let’s say your $VIM/.vim looks like this:
syntax/
compiler/
ftdetect/
filetype.vim
scripts.vim
autoload/
ftplugin/
plugin/
|________ A.vim
colors/
doc/
|________ A.txt
…
Now, you want to install a plugin ‘A’, which comes with some documents
syntax/
compiler/
ftdetect/
filetype.vim
scripts.vim
autoload/
ftplugin/
plugin/
|________ A.vim� |________ B.vim
colors/
doc/
|________ A.txt
|________ B.txt
…
Next, you want to install a plugin ‘B’, which also comes with some documents
syntax/
compiler/
ftdetect/
filetype.vim
scripts.vim
autoload/
|________ C.vim
ftplugin/
plugin/
|________ A.vim� |________ B.vim� |________ C.vim
colors/
doc/
|________ A.txt
|________ B.txt
|________ C.txt
…
Now you want to install an even larger collection of co-related settings, C
DISADVANTAGES
Packages to the rescue
:h packages
:h packpath
~/.vim
|___pack/
| |____plugins/
| |______start/
| | |____A/
| | | |______plugin/
| | | | |_____ A.vim
| | | |_______…
| | | |_______doc/
| | | |_____ A.vim
| | |____B/
| | | |______plugin/
| | | | |_____ B.vim
| | | |_______…� | | |
| | |____C/
| | |_______…
| |_______opt/
|__syntax/
|__compiler/
|__…
Files in opt are not loaded automatically, and require using :packadd
Files in start are loaded automatically
All directories under packpath (default set �to runtimepath) are scanned for plugins under pack/*/start, which then get added to your runtimepath!
This name can be anything
ADVANTAGES
Example -
Loading some external packages
PORTABILITY - GNU STOW
Never worry about portability again