1 of 32

VIM

WORKING VIM LIKE A LIGHTWEIGHT IDE

2 of 32

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

3 of 32

What if I don’t want to load anything?

vim -u NONE

vim -u NORC

4 of 32

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)

5 of 32

WHAT!?

runtimepath?

packages?

plugins?

�wtf is /pack/.../and/why/is/it/so/long

6 of 32

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:

  1. User Home .vim
  2. Sysadmin folder
  3. $VIMRUNTIME
  4. Sysadmin “after” folder
  5. User Home “after” folder

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)

7 of 32

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

8 of 32

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

9 of 32

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

10 of 32

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

11 of 32

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

12 of 32

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

13 of 32

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

14 of 32

Example - Compiling Typescript

We want to natively integrate Typescript’s build tool tsc into our vim edit-compile-edit cycle

15 of 32

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

16 of 32

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

17 of 32

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

18 of 32

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

19 of 32

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/

20 of 32

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)

21 of 32

Why packages when I have plugins?

22 of 32

syntax/

compiler/

ftdetect/

filetype.vim

scripts.vim

autoload/

ftplugin/

plugin/

colors/

doc/

Let’s say your $VIM/.vim looks like this:

23 of 32

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

24 of 32

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

25 of 32

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

26 of 32

DISADVANTAGES

  • Each suite of docs / settings / plugins cannot be independently managed�
  • There is no mechanism of marking packages as optional�
  • Updating packages becomes very tedious

27 of 32

Packages to the rescue

:h packages

:h packpath

28 of 32

~/.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

29 of 32

ADVANTAGES

  • Each suite of docs / settings / plugins can now be independently managed�
  • Packages can be marked as optional and only loaded on invocation of :packadd�
  • Packages can be git repositories, making them seamless to update

30 of 32

Example -

Loading some external packages

31 of 32

PORTABILITY - GNU STOW

Never worry about portability again

32 of 32

THANKS!

Do you have any questions?

leerenchang@gmail.com

leerenchang

leeren

CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.