1.5. Vim

1.5.1. Basic Vim

  • Insert text mode I

  • Save a file :w

  • Leave any mode Esc

  • To resource vimrc, set this in .bashrc:

export MYVIMRC="/home/andrew/.vimrc"
  • To resource (with tab completion): :so $MYVIMRC

1.5.2. .vimrc

Start with a basic .vimrc file:

set t_ut=                   " background color erase
set number                      " show line numbers
set textwidth=80            " line length
set colorcolumn=+1          " show colour colum at line length
set tabstop=4           " tabs that appear 4 spaces wide
set clipboard=unnamed   " auto yank to clipboard

" enable syntax highlighting:
    syntax on
" autodetect filetypes:
    filetype plugin indent on
" 256 colours in vim:
    set t_Co=256
" colorcoloum color is dark grey not red:
    highlight ColorColumn ctermbg=233 guibg=#808080

" put cursor right to the bottom or top when scrolling:
    set scrolloff=0
" fast terminal update
    set ttyfast xoria256

  • This file is located in ./vim/colors and is called by .vimrc

  • In my opinion the colours are quite good

  • This operates with (e.g.) all C++ files

" Vim color file
" Name:       xoria256.vim
" Version:    1.6
" Maintainer:       Dmitriy Y. Zotikov (xio) <xio@ungrund.org>
" Should work in recent 256 color terminals.  88-color terms like urxvt are
" NOT supported.
" Don't forget to install 'ncurses-term' and set TERM to xterm-256color or
" similar value.
" Color numbers (0-255) see:
" http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html
" For a specific filetype highlighting rules issue :syntax list when a file of
" that type is opened.
" TODO: link colours instead of setting values explicitly

" Initialization {{{
if &t_Co != 256 && ! has("gui_running")
  echoerr "Please use GUI or a 256-color terminal (which sets t_Co=256)."

set background=dark

hi clear

if exists("syntax_on")
  syntax reset

let colors_name = "xoria256"
" Colours {{{1
"" General {{{2
hi Normal       ctermfg=252 guifg=#d0d0d0 ctermbg=234 guibg=#1c1c1c cterm=none gui=none
hi Cursor                                 ctermbg=214 guibg=#ffaf00
hi CursorColumn                           ctermbg=238 guibg=#444444
hi CursorLine                             ctermbg=237 guibg=#3a3a3a cterm=none gui=none
hi Error        ctermfg=15  guifg=#ffffff ctermbg=1   guibg=#800000
hi ErrorMsg     ctermfg=15  guifg=#ffffff ctermbg=1   guibg=#800000
hi FoldColumn   ctermfg=247 guifg=#9e9e9e ctermbg=233 guibg=#121212
hi Folded       ctermfg=255 guifg=#eeeeee ctermbg=60  guibg=#5f5f87
hi IncSearch    ctermfg=0   guifg=#000000 ctermbg=223 guibg=#ffdfaf cterm=none gui=none
hi LineNr       ctermfg=247 guifg=#9e9e9e ctermbg=233 guibg=#121212
hi MatchParen   ctermfg=188 guifg=#dfdfdf ctermbg=68  guibg=#5f87df cterm=bold gui=bold
" hi MoreMsg
hi NonText      ctermfg=247 guifg=#9e9e9e ctermbg=233 guibg=#121212 cterm=bold gui=bold
hi Pmenu        ctermfg=0   guifg=#000000 ctermbg=250 guibg=#bcbcbc
hi PmenuSel     ctermfg=255 guifg=#eeeeee ctermbg=243 guibg=#767676
hi PmenuSbar                              ctermbg=252 guibg=#d0d0d0
hi PmenuThumb   ctermfg=243 guifg=#767676
hi Search       ctermfg=0   guifg=#000000 ctermbg=149 guibg=#afdf5f
hi SignColumn   ctermfg=248 guifg=#a8a8a8
hi SpecialKey   ctermfg=77  guifg=#5fdf5f
" hi SpellBad     ctermfg=160 guifg=fg      ctermbg=bg                cterm=underline               guisp=#df0000
hi SpellBad                               ctermbg=238                                               guisp=#df0000
hi SpellCap     ctermfg=189 guifg=#dfdfff ctermbg=bg  guibg=bg      cterm=underline gui=underline
hi SpellRare    ctermfg=168 guifg=#df5f87 ctermbg=bg  guibg=bg      cterm=underline gui=underline
hi SpellLocal   ctermfg=98  guifg=#875fdf ctermbg=bg  guibg=bg      cterm=underline gui=underline
hi StatusLine   ctermfg=15  guifg=#ffffff ctermbg=239 guibg=#4e4e4e cterm=bold gui=bold
hi StatusLineNC ctermfg=249 guifg=#b2b2b2 ctermbg=237 guibg=#3a3a3a cterm=none gui=none
hi TabLine      ctermfg=fg  guifg=fg      ctermbg=242 guibg=#666666 cterm=none gui=none
hi TabLineFill  ctermfg=fg  guifg=fg      ctermbg=237 guibg=#3a3a3a cterm=none gui=none
hi Title        ctermfg=225 guifg=#ffdfff
hi Todo         ctermfg=0   guifg=#000000 ctermbg=184 guibg=#dfdf00
hi Underlined   ctermfg=39  guifg=#00afff                           cterm=underline gui=underline
hi VertSplit    ctermfg=237 guifg=#3a3a3a ctermbg=237 guibg=#3a3a3a cterm=none gui=none
" hi VIsualNOS    ctermfg=24  guifg=#005f87 ctermbg=153 guibg=#afdfff cterm=none gui=none
" hi Visual       ctermfg=24  guifg=#005f87 ctermbg=153 guibg=#afdfff
hi Visual       ctermfg=255 guifg=#eeeeee ctermbg=96  guibg=#875f87
" hi Visual       ctermfg=255 guifg=#eeeeee ctermbg=24  guibg=#005f87
hi VisualNOS    ctermfg=255 guifg=#eeeeee ctermbg=60  guibg=#5f5f87
hi WildMenu     ctermfg=0   guifg=#000000 ctermbg=150 guibg=#afdf87 cterm=bold gui=bold

"" Syntax highlighting {{{2
hi Comment      ctermfg=244 guifg=#808080
hi Constant     ctermfg=229 guifg=#ffffaf
hi Identifier   ctermfg=182 guifg=#dfafdf                           cterm=none
hi Ignore       ctermfg=238 guifg=#444444
hi Number       ctermfg=180 guifg=#dfaf87
hi PreProc      ctermfg=150 guifg=#afdf87
hi Special      ctermfg=174 guifg=#df8787
hi Statement    ctermfg=110 guifg=#87afdf                           cterm=none gui=none
hi Type         ctermfg=146 guifg=#afafdf                           cterm=none gui=none

"" Special {{{2
""" .diff {{{3
hi diffAdded    ctermfg=150 guifg=#afdf87
hi diffRemoved  ctermfg=174 guifg=#df8787
""" vimdiff {{{3
hi diffAdd      ctermfg=bg  guifg=bg      ctermbg=151 guibg=#afdfaf
"hi diffDelete   ctermfg=bg  guifg=bg      ctermbg=186 guibg=#dfdf87 cterm=none gui=none
hi diffDelete   ctermfg=bg  guifg=bg      ctermbg=246 guibg=#949494 cterm=none gui=none
hi diffChange   ctermfg=bg  guifg=bg      ctermbg=181 guibg=#dfafaf
hi diffText     ctermfg=bg  guifg=bg      ctermbg=174 guibg=#df8787 cterm=none gui=none
""" HTML {{{3
" hi htmlTag      ctermfg=146  guifg=#afafdf
" hi htmlEndTag   ctermfg=146  guifg=#afafdf
hi htmlTag      ctermfg=244
hi htmlEndTag   ctermfg=244
hi htmlArg  ctermfg=182  guifg=#dfafdf
hi htmlValue        ctermfg=187  guifg=#dfdfaf
hi htmlTitle        ctermfg=254  ctermbg=95
" hi htmlArg        ctermfg=146
" hi htmlTagName    ctermfg=146
" hi htmlString     ctermfg=187
""" XML {{{3
hi link xmlTagName  Statement
" hi link xmlTag            Comment
" hi link xmlEndTag Statement
hi link xmlTag              xmlTagName
hi link xmlEndTag   xmlTag
hi link xmlAttrib   Identifier
""" django {{{3
hi djangoVarBlock ctermfg=180  guifg=#dfaf87
hi djangoTagBlock ctermfg=150  guifg=#afdf87
hi djangoStatement ctermfg=146  guifg=#afafdf
hi djangoFilter ctermfg=174  guifg=#df8787
""" python {{{3
hi pythonExceptions ctermfg=174
""" NERDTree {{{3
hi Directory      ctermfg=110  guifg=#87afdf
hi treeCWD        ctermfg=180  guifg=#dfaf87
hi treeClosable   ctermfg=174  guifg=#df8787
hi treeOpenable   ctermfg=150  guifg=#afdf87
hi treePart       ctermfg=244  guifg=#808080
hi treeDirSlash   ctermfg=244  guifg=#808080
hi treeLink       ctermfg=182  guifg=#dfafdf
""" rst #{{{3
hi link rstEmphasis Number

""" VimDebug {{{3
" you may want to set SignColumn highlight in your .vimrc
" :help sign
" :help SignColumn

" hi currentLine term=reverse cterm=reverse gui=reverse
" hi breakPoint  term=NONE    cterm=NONE    gui=NONE
" hi empty       term=NONE    cterm=NONE    gui=NONE

" sign define currentLine linehl=currentLine
" sign define breakPoint  linehl=breakPoint  text=>>
" sign define both        linehl=currentLine text=>>
" sign define empty       linehl=empty
""" vimHelp {{{3
hi link helpExample Number
hi link helpNumber String
hi helpURL ctermfg=110 guifg=#87afdf                           cterm=underline gui=underline
hi link helpHyperTextEntry helpURL

Update .vimrc

" use colors/xoria256:
    colorscheme xoria256 Pathogen

mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

Update .vimrc

execute pathogen#infect() vimextensionopenfoam

The other color mapping I have is for OpenFOAM dictionaries, supplied by Tobias Holzmann:

https://bitbucket.org/shor-ty/vimextensionopenfoam/src/master/ Vundle

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

put this in .vimrc

    set nocompatible              " be iMproved, required
    filetype off                  " required

" set the runtime path to include Vundle and initialize
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()

" let Vundle manage Vundle, required
    Plugin 'VundleVim/Vundle.vim'

" All of your Plugins must be added before the following line
    call vundle#end()

Load vim, run plugin installer and quit the file it shows

:q YouCompleteMe

My version of VIM = 7.4.1689 (laptop)

VIM version on desktop = 8.0

For autocompletion when typing


cd ~/.vim/bundle
git clone https://github.com/Valloric/YouCompleteMe.git
sudo apt install build-essential cmake python3-dev
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
./install.py --clangd-completer

Add to .vimrc in Vundle section:

Plugin 'Valloric/YouCompleteMe'

Install vim like for Vundle:


It wont find the clang interpreter, because it doesnt know

sudo apt-get install build-essential cmake
sudo apt-get install python-dev python3-dev
cd ~/.vim/bundle/YouCompleteMe
./install.py --clangd-completer

Install vim like for Vundle:


Also add the location of the flag configuration (i’m not using this one):

let g:ycm_global_ycm_extra_conf = '/home/andrew/.vim/bundle/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py'

There is an OpenFOAM completion version:

import os
import ycm_core

foam_app = os.environ['FOAM_APP']
foam_src = os.environ['FOAM_SRC']

# These are the compilation flags that will be used in case there's no
# compilation database set (by default, one is not set).

flags = [
    '-I' + foam_src + '/transportModels/',
    '-I' + foam_app + '/solvers/multiphase/twoPhaseEulerFoam/twoPhaseSystem/'\
    + 'lnInclude',
    '-I' + foam_src + '/transportModels/incompressible/lnInclude',
    '-I' + foam_src + '/finiteVolume/lnInclude',
    '-I' + foam_src + '/fvOptions/lnInclude',
    '-I' + foam_src + '/OpenFOAM/lnInclude',
    '-I' + foam_src + '/OSspecific/POSIX/lnInclude',

compilation_database_folder = ''

if compilation_database_folder:
    database = ycm_core.CompilationDatabase(compilation_database_folder)
    database = None

def DirectoryOfThisScript():
    return os.path.dirname(os.path.abspath(__file__))

def MakeRelativePathsInFlagsAbsolute(flags, working_directory):
    if not working_directory:
        return list(flags)
    new_flags = []
    make_next_absolute = False
    path_flags = ['-isystem', '-I', '-iquote', '--sysroot=']
    for flag in flags:
        new_flag = flag

        if make_next_absolute:
            make_next_absolute = False
            if not flag.startswith('/'):
                new_flag = os.path.join(working_directory, flag)

        for path_flag in path_flags:
            if flag == path_flag:
                make_next_absolute = True

            if flag.startswith(path_flag):
                path = flag[len(path_flag):]
                new_flag = path_flag + os.path.join(working_directory, path)

        if new_flag:
    return new_flags

def FlagsForFile(filename):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = database.GetCompilationInfoForFile( filename )
    final_flags = MakeRelativePathsInFlagsAbsolute(
      compilation_info.compiler_working_dir_ )

    # NOTE: This is just for YouCompleteMe; it's highly likely that your project
    # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
    # ycm_extra_conf IF YOU'RE NOT 100% YOU NEED IT.
      #final_flags.remove( '-stdlib=libc++' )
    #except ValueError:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return {
    'flags': final_flags,
    'do_cache': True

Add the Openfoam completion without diagnostics (because diagnostics doesnt really work):

" OpenFOAM completion (enabled) - without diagnostics, with one character for completion
let g:ycm_global_ycm_extra_conf = '/home/andrew/.vim/bundle/YouCompleteMe/openfoamConfig/.ycm_extra_conf.py'
let g:ycm_enable_diagnostic_signs = 0
let g:ycm_enable_diagnostic_highlighting = 0
let g:ycm_min_num_of_chars_for_completion = 1
  • Question: How to get rid of ID when it just recognises repeated words?

  • Question: This doesnt really recogonise the language as far as I can tell, it just looks up libraries and then is consistent. How to regognise the language? maybe ./install.sh --clang-completer --system-clang

  • Try this some other time:


cd ~/.vim/bundle
git clone https://github.com/Valloric/YouCompleteMe.git
cd YouCompleteMe
git submodule update --init --recursive
./install.sh --clang-completer --system-clang Riv

This is used for restructred text (DONT USE IT).

    Plugin 'Rykka/riv.vim'

For example, this command will create a quick html page:


I couldn’t get RST server to work (Plugin ‘Rykka/InstantRst’) NERDTree

git clone https://github.com/scrooloose/nerdtree.git ~/.vim/bundle/nerdtree

NERDTree commands:

  • :NERDTree - starts a new nerd tree window

  • :NERDTreeClose - closes NerdTree

From within NERDTree:

  • o - opens a file or directory

  • go - opens file but keeps you in NERDTree

  • x - closes a directory

  • C - change root to current directory

  • ? - Toggle help

Split screen:

  • Ctrl+ww cycle though all windows

  • Ctrl+wh takes you left a window

  • Ctrl+wj takes you down a window

  • Ctrl+wk takes you up a window

  • Ctrl+wl takes you right a window

  • i - open file with split horizontal pane

  • s - open file with split vertical pane AutoSave

” Autosave

Plugin ‘907th/vim-auto-save’

let g:auto_save = 1 ” enable AutoSave on Vim startup Silver Searcher

apt-get install silversearcher-ag Python docstring colours

Create a file:


Add this to file:

syn region Comment start=/"""/ end=/"""/ VIM Obsession

To enable saving of sessions within .tmux.conf (VIM 8.0 and NERDTree do not mix)

cd ~/.vim/bundle
git clone git://github.com/tpope/vim-obsession.git
vim -u NONE -c "helptags vim-obsession/doc" -c q VIM Sessions

To enable saving of sessions VIM Environment

Top is VIM:

  • Top left: NERDTree

  • Top middle: OpenFOAM code

  • Top right: RST code

Bottom is TMUX:

  • Bottom left: python -m http.server

  • Bottom middle: OpenFOAM Run Directory setBubble -noOverwrite

  • Bottom right, top: Compilation of OpenFOAM unicode wclean, wmake

  • Bottom right, bottom: Compilation of RST code make html


How to start TMUX session

  • Start a Terminal Window (maximise window)

  • tmux

  • To restore tmux session ctrl+a, ctrl+r

  • To save a tmux session ctrl+a, ctrl+s

  • To kill tmux server ctrl+a, :kill-server

  • Move left alt+h

  • Move down alt+j

  • Move up alt+k

  • Move right alt+l

  • Open split vertically alt+v

  • Open split horizontally alt+b

  • Close split alt+c

How to use VIM

  • (Save session) :SaveSession session saved to ~/Session.vim

  • Move left ctrl+h

  • Move down ctrl+j

  • Move up ctrl+k

  • Move right ctrl+l

  • Close file :q

  • To intent text: v, highlight text, >


  • Bookmark folder :Bookmark (Highlight the folder in NERDTree)

  • (Open file o will overrite any previous open file, opens to the left of open files)

  • Open file over current file select window and left click on file

  • (Horizontal split s opens to the left of open files)

  • (Vertical split i opens to the left and to the bottom on open files)

  • Remove bookmark cursor over bookmark, D, deletes bookmark

  • Make directory current C

To reach a bookmark: cursor over bookmark and return to enter, or maybe second time, double click bookmark

Startup process (without saved sessions)

$ tmux
ctrl + a ctrl + r
vim (will also start NERDTree)

Shutdown process

ctrl + a : kill-server

Can save locations using TMUX (makesure VIM is closed)

ctrl + a ctrl + s