Skip to content

vim neovim Notes

Chris Jones edited this page Jun 21, 2018 · 57 revisions

Contents

General

To print a statement to messages

echom 'mr fancy message'

To clear messages in Vim / Neovim

The below command requires Vim 7.4.1735

:messages clear

Installing Vim using Linuxbrew with clipboard support

To get clipboard support working with Vim when installing Vim 8.x on Debian, make sure the following dependencies have been installed.

  1. libx11-dev
  2. libxtst-dev
apt-get install libx11-dev libxtst-dev

If Vim has already been installed via brew it can be reinstalled,

brew -v reinstall --build-from-source --with-client-server vim

To test if xclip is working within a remote ssh session

echo "copy me" | xclip -selection clipboard

File Structure General

To override default settings for specific filetypes use the after directory located within $HOME/.vim, ie. for specific settings related to python

$HOME/.vim/after/ftplugin/python.vim

Vim / Neovim allows one to create a new file / buffer within a directory that does not yet exist, so it can become problematic to save the newly created buffer to disk if the directory doesn't exist within the file system. Β―\(ツ)/Β―

To work around the above mentioned scenario use the command within the vimrc file present in the dotfiles repo.

:,md

To print all the values of all the global or local variables, ie. python_host_prog assigned within Vim or Neovim

:let

To copy all lines of an open buffer to the system clipboard

:%y+

To display a list of commands in Vim or Neovim

:Commands

To display the decimal, octal, and hex representation of a character in Vim

g + a

To repeat the previous command or last operation

.

To show the current running version of vim or Neovim

:version

To reload the current buffer

:edit

or

:source %

To start Vim / Neovim without loading a .vimrc configuration file

vim -u NONE

To get a high level overview of settings in Vim & Neovim

:browse options

To print out all the options / settings enbabled for the active buffer

:browse set

To check and see if python 3 support is working with Neovim

:echo has('python3')

To check to see if Vim or Neovim was compiled with a specific setting

:echo has('[mr-fancy-setting]')

ie.

:echo has('clipboard')

To check and see which particular clipboard is being used, ie. unnamed or unnamedplus

:set clipboard?

For more information see:

:h version
:h has(
:h feature-list

To toggle VISUAL mode

v

To toggle invisible characters

:set list!

To exit PASTE mode

:set nopaste

To change the mode to visual-block or v-block

control+v

To switch between an open and closing parentheses ( or curly brace { press % in NORMAL mode.

To inspect the runtimepath

:set rtp?

To print the current working directory

:pwd

To change the local working directory

:lcd

To print the $VIMRUNTIME path

echo $VIMRUNTIME

To redraw the UI elements, ie. User Interface

control + l

To print / return the value of a setting

:set option?

ie.

:set autoindent?

To see where a particular setting is being set ie. which file and line is issuing the setting.

:verbose set option?

ie.

:verbose set termguicolors?

To display the current key mappings for normal, visual, insert

:nmap
:vmap
:imap

To display all key mappings within a Neovim session

:map

To display a value of an environment variable within a Neovim session

:echo [$MR_FANCY_ENV_VAR_01, $MR_FANCY_ENV_VAR_02]

ie. to display the values of the below environment variables with Neovim

:echo [$LANG, $LC_CTYPE, $LC_ALL]

Working with buffers

To display a list of open buffers

:ls

To switch to the previous buffer

:bp

To switch to the new buffer

:bN

Working with autocommands, ie. autocmd

Protip when working with autocommands, and expiermenting with new features in .vimrc or init.vim make certain to enclose autocmd! within an augroup block or the autocmd! will be reloaded, and NOT flush out the previously defined autocmd!.

A proper way to define autocommands within a .vimrc

augroup customColorscheme
  " Clear the group
autocmd!
autocmd ColorScheme * call CustomHighlights()
augroup END

Searching For Patterns with the_platinum_searcher

To search for a pattern within all text / source code files within the current working directory

pt [mr-fancy-expression-to-search-for]

The above command is quite useful for finding syntax errors that are scattered through a directory structure, and the specific file / line is not specified within standard error output.

Working with Splits

Splits within Vim can be launched horizontally & vertically within a Vim session.

To a create vertical split within Vim

:vsplit

To close all splits other than the active split / window

:on

Buffers can be accessed across multiple splits. 🀘

To learn more about using splits within Vim

:h opening-window

Working with Cursor Color and Shape

Getting Italic font working

  • First, make sure the terminal can support italics
echo -e "\e[3m foo \e[23m"

To get italic support working with Neovim the below line is required πŸŽ–

highlight Comment gui=italic

Working with Sessions and Views

TL;DR

  • session files can store the state of all open buffers and much more.
  • views can store the state of code folds and cursor position within a buffer.
:h sessionoptions
:h viewoptions

Working with Sessions and Views Manually Invoking

To manually create a session file for the current Vim session

:mks ~/.vim/tmp/sessions/[mr-fancy-session.vim]

To manually create a view file that preserves code fold for the current session

:mkview ~/.vim/tmp/views/[mr-fancy-view.vim]

If an existing session.vim or view.vim file exists the ! option can be specified when running mks or mkview

ie.

To update a session file

:mks! ~/.vim/tmp/sessions/[mr-fancy-session.vim]

Python Support

Python Support Uninstalling anaconda

To uninstall anaconda on macOS follow the instructions provided by the below link. Official anaconda uninstall

Python Support Setting Up Python 2/3

🚨 Make certain that a Homebrew / Linuxbrew python is not the $PATH when trying to install a particular version of Python using pyenv, especially on a Linux box.

To setup different versions of Python for a particular user, use pyenv

pyenv can be installed via homebrew or cloning the github repo pyenv/pyenv I chose the latter because I was running into issues using the homebrew version of pyenv.

To install pyenv on macOS

git clone https://github.com/pyenv/pyenv $code/python/pyenv

To update the list of available pythons to install

cd $PYENV_ROOT; git pull

To get virtualenv support with python install pyenv-virtualenv

git clone https://github.com/pyenv/pyenv-virtualenv $PYENV_ROOT/plugins/pyenv-virtualenv

Once those two packages have been installed, one can begin installing different versions of python.

On Debian some additional packages will need to be installed in order for python 2.7.14 to be installed, ie. libbz2-dev and

To get verbose output when building python using pyenv

pyenv install -v 2.7.14
pyenv install 2.7.14

To setup a virtualenv named neovim2 for the python-neovim provider packages

pyenv virtualenv 2.7.14 neovim2

To activate the neovim2 virtual environment

pyenv activate neovim2

To install the neovim python module for Neovim within the neovim2 virtualenv

pip install neovim

The above command pip install neovim works for both neovim2 and neovim3 virtual environments.

To print the path for the python executable for the neovim2 env

pyenv which python

🚨 when providing the path for the python executable in init.vim MAKE CERTAIN to use the full path, ie. DON'T use the $HOME env var or else :checkhealth will fail.

Add the above python path to init.vim

To activate the neovim2 virtualenv

pyenv activate neovim2

To exit out of either virtualenv

pyenv deactivate

To remove a virtualenv created with pyenv and pyenv-virtualenv

pyenv uninstall [name-of-virtualenv]

To check and see if vim is compiled with either python2 or python3 support

:version

To get detailed help information about the python providers for Neovim

:help provider-python

Vim only supports python 2 or python 3, but not both at the same time.

Neovim can support both Python 2 and Python 3 at the same time.

To check and see if Neovim supports Python 3

:py3 print('hello')

Useful Links Python Support

Working with Tabs

Each vim tab contains it's own unique splits.

To create a new tab within a session

:tabnew

To focus on the next tab
g+t
To focus on the previous tab
g+T

To move to the next tab g + t

To move to the previous tab g + T

Working with wildmenu

When the wildmenu is present for navigating between files and one wants to cd into a directory use the ↓ down arrow on the keypad to go into the selected directory.

Working with netrw ie. file explorer

To toggle a vertical file explorer, ie. netrw to left of the current buffer

:Vex

To create a new file within the current working directory

:%

then input filename.

To create a new directory using netrw d

To rename the current highlighted directory using netrw R

To close a netrw buffer,

  1. find the buffer number with :ls
  2. close the buffer with :bd[#], ie. :bd42

Spell Checking

To enable spell checking in vim

set spell

A dictionary file may need to be downloaded before spell checking can be enabled.

To add a "misspelled" word to a local dictionary file z then g

To remove a "misspelled" word from a local dictionary file z then w

Visual Block Mode

To insert into visual block mode ctrl+v

Then select which lines you would like the repeated text to appear on.

Then go into visual insert mode with shift+i

Then type out what you want repeated and it will appear in the buffer after exiting insert mode.

When working in visual block mode to toggle between opposing corners when drawing a rectangular region. o

Documentation

To launch the help system within nvim

:help

To generate documentation for installed plugins

:helptags ALL

To write to / save a file opened in nvim without having proper permissions

:w !sudo tee > /dev/null %

For a deeper understanding of what the command is doing see

see πŸ™ˆ for more info.

To adjust the indentation of a particular line within a file

>>

To auto indent a line in vim

==

To paste code from the clipboard with indentation

:set paste

Neovim profiling

To record the startup time of vim / nvim and load the startup time in a file

nvim --startuptime /tmp/nvim.log 

Plugins

Plugin Definitions

  • minpac - is a lightweight plugin manager written around the new packages and jobs features introduced in Vim 8.x and Neovim 0.2.x
  • vim-projectionist - a handy plugin for opening a source file's counterpart.
  • vim-multiple-cursors - the name kind of says it all.
  • limelight.vim - grey out code blocks that don't have focus
  • ALE - a useful plugin for linting various source code files
  • Deoplete - inline completion for Neovim.
  • vim-markdown-composer - preview the current .md document in the default browser.

Working with minpac

minpac builds on top vim's job-control features introduced in Vim 8.

To print a log of messages from running PackUpdate

:messages

To remove a plugin installed with minpac

  1. Remove the call minpac#add('author/plugin-name') from your .vimrc
  2. Save the .vimrc
source %
  1. Then invoke the clean function from minpac
call minpac#clean()

I wrote a custom minpac binding to invoke the clean function with the below command.

:PackClean

Minpac stores plugins downloaded from git repositories in the following directories

$dots/editors/vim/vim/pack/minpac/start

or

$HOME/.vim/pack/minpac/start/

To generate help tag files for plugins maintained by minpac

helptags ALL

When running minpac#update() tag files will be generated.

Working with Plugins

To print all loaded vim scripts on a seperate line

:scriptnames

Working with fzf within Vim & Neovim

TODO flesh commands that can be used with fzf and Vim to search for specific contents within a project, ie. the source files that comprise a project.

In the meantime, use the_platinum_searcher to search for specific terms within source files, ie.

To search for a miss spelling of ProTypes within a React project

pt ProTypes

The above command will search through the entire project for the syntax error and print the offending file along with the line the error occurs on to STDOUT.

Airline

Useful Links airline

Deoplete

Deoplete troubleshooting

The below no longer applies to my setup because I switched to pyenv to manage pythons.

If Deoplete whines about missing neovim python module / plugin, run the below command

pip3 install --upgrade neovim

Plugins Useful Links

Working with Vimscript

To get some help writing Vimscripts

:help usr_40.txt
:help function-list

To do string equality on a variable using Vimscript

if actor ==? 'Michael Keaton'
  echom 'I am Batman!'
else
  echom 'No sir ree bob'
endif

==? performs case insensitive pattern matching ==# peforms case sensitive pattern matching

When writing a function in Vimscript make sure the function name begins with a captial letter.

function MrFancyFucntion()
  echo "Hey, There"
endfunction

Find and Replace

To replace a mr-fancy with mr-fancy-pants

:s/mr-fancy/mr-fancy-pants

To replace all occurances of mr-fancy with mr-fancy-pants

:s/mr-fancy/mr-fancy-pants/g

Useful Links Vimscript

Writing Custom Code Folds

To get the current fold method being used in a vim buffer

:set foldmethod?

If the fold method is set to expr :set foldexpr? will print the current expr

To print the current fold expression for a particular file / buffer

:set foldexpr?

To get native vim help for fold expressions

:help fold-expr

To expand all code folds z+shift then o

To collopase all code folds z+shift then m

Useful Links Code Folding

Troubleshooting

If Vim or Neovim is getting sluggish when starting up, profile the start time

vim --startuptime ~/logs/nvim-boot-time.log

To see example an example a bad / good startup time / performance

πŸ“Έ ⌚︎ Profiling startup time

Vim startup time profiling

Useful Links

TODOs

  • ~~~do some research on vim-projectionist~~~

Clone this wiki locally