Skip to content

vim neovim Notes

Chris Jones edited this page Dec 4, 2020 · 57 revisions

Contents

General πŸ”

To jump to / go to goto file under cursor (useful for working with web based projects)

gf

the above key binding while in normal mode will open the file under the cursor in a new buffer and bring the buffer to the foreground for viewing / editing learn more

To toggle / switch a word from UPPERCASE to lowercase in Vim, highlight the word (visual block works) and then from command mode press ~

~

Working with shell commands in a vim buffer

tags shell vim external sh command cmd vim buffer

An external shell command can modify the current contents of a Vim buffer. One such example is working with binary plist files on macOS, as the Info.plist files used to store settings for Xcode.app and Simulator.app are in a binary plist format.

To convert a binary plist file to a readable XML format from a CLI

plutil -convert xml1 /path/to/binary.plist

To convert a XML formatted plist file to binary using a CLI

plutil -convert binary1 /path/to/xml.plist

The above plutil -convert xml1 can be applied to the contents of a vim buffer thus allowing to properly modify the contents of a plist file that was saved in a binary format.

:%! plutil -convert xml1 /path/to/binary.plist

I experienced complications in converting an XML plist file back to binary from within vim so I converted it back to binary via a shell.

see for more detailed / automated process of working with binary plist files using Vim.

Working in Command mode

Command mode in Vim does not use typical normal mode movement motions in Vim, however to delete the previous word under the cursor in command mode

:control + w

For more command mode shortcuts see or,

:help ex-edit-index

has() checks for features

To get a list of features supported by Vim or Neovim

:h feature-list

That said, mkview is a command, and not a feature.

To check if Vim or Neovim suports a command in a Vimscript file, ie. .vim

exists(':mr42fancy') == 2

2 will do an exact literal string match for mr42fancy.

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

To set the syntax highlighting for a specific file, using a specific formatting

:set syntax=[LANGUAGE]

Ex

To set the stynax for a .babelrc file

:set syntax=json

To write to or save a file opened in Vim or Neovim without having proper permissions, ie. super user perms

:w !sudo tee > /dev/null %

For a deeper understanding of what the above command is doing see

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

Installing Vim using Linuxbrew 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

To print all variables and variable values 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 or Neovim without loading a .vimrc configuration file

vim -u NONE

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 and Neovim allow creating a new file / buffer within a directory that does not yet exist, so it can become problematic to save the newly created file / buffer to disk if the containing directory doesn't exist on the file system.

To work around the above mentioned scenario use the key binding within mappings.vim file present within this dotfiles repo.

leader mkd

or run the below command from within Vim

!mkdir -p %:p:h

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 a list of all scripts in the order in which they were loading at runtime

:scriptnames

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

❗️ If control+l has been remapped the Vim command can be manually invoked with

:redraw!

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 within Neovim

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

Working with buffers πŸ”

To reload all buffers, ie. will update code folds when new ones are added

:bufdo e

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!, thus slowing Vim & Neovim exponentially with each reload of a configuration file.

A proper way to define autocommands within a .vimrc

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

Searching For Patterns the_platinum_searcher πŸ”

To search for a pattern within all text or 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 Cursor color and shape πŸ”

Working with Splits πŸ”

Vim can create horizontal & vertical splits within a Vim session, ie. think how panes split the current window in tmux.

To create a vertical split within Vim

:vsplit

To create a horizontal split using a Vim command

:split

The above split commands will create a split, ie. a new buffer with the file from the previous buffer in the new split.

To create a horizontal split without the previous open buffers from the prior window

:sp 0

The above command will create a horizontal split with a buffer named 0 however the previously open buffer in the previos window will not be shown in the new split. Also, a split can also be created by specifying a path to a different file.

Ex

:sp ./path/to/README.md

With my current Vim configuration a horizontal split be created below the current buffer with the README.md file contained within the split.

To create a vertical split using key bindings

control + W then v

To create a horizontal split using key bindings

control + W then S

Yes, that is a capital S and not a lower case s

To close all splits other than the active split / window

:on

:on is short for :only

Buffers can be accessed across multiple splits. 🀘

To learn more about using splits within Vim

:h opening-window

Working with Italic fonts πŸ”

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

To get italic code comments 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 load / restore from a session file

:source /path/to/[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 use the ! option to replace the existing file

ie.

To update a session file

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

Working with tags, ie. ctags and universal-ctags

To install universal-ctags on macOS

brew install --HEAD universal-ctags/universal-ctags/universal-ctags

Polluting a git repo with a tags file isn't exactly the most kosher 🐷 thang. That said, a tags file can be generated for a git repo and stored within the .git directory of the repo, and should not get checked into source control, and one shouldn't have to get on their soap box πŸ“¦ to get a PR merged for updating a .gitignore file is a popular repo, ...pretty much a bipartisan tags file for a popular project.

To generate a tags file for a git repo and store the tags file within the .git directory.

cd /path/to/git/repo/root
ctags -R -f ./.git/tags .

For more details regarding ctags

:help tags
:help CTRL-]

Python Support πŸ”

Python Support 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 or Linuxbrew python is not in 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 as well as pulling the latest version of pyenv itself

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.X to be installed, ie. libbz2-dev

To get verbose output when building python using pyenv

pyenv install -v 2.7.15

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

pyenv virtualenv 2.7.15 py2neovim

To activate the neovim2 virtual environment

pyenv activate py2neovim

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

pip install neovim

To upgrade the neovim provider package if it just needs updating

pip install --upgrade neovim

To upgrade the pip package manager itself

pip install --upgrade pip

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. DO NOT use the $HOME env var or else :checkhealth will fail.

Add the above python path to init.vim

To activate the py2neovim virtualenv

pyenv activate py2neovim

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

Neovim does not provide a way to compile in Python support because it is automatically compile in no matter what.

To get detailed help information about the python providers for Neovim, and the below command is worth it's weight in πŸ₯‡

: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')

To check and verify Python 2.x is working

:python print('hello')

Useful Links Python Support πŸ”

Working with Tabs πŸ”

Each vim tab contains it's own unique splits. Think of a tab in Vim as a window in tmux.

To create a new tab within a Vim 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 followed by a filename

:e [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

To rename the current buffer or file in vim / Neovim

:E
R

R is pressed in the context of the netrw explorer

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

To correct a misspelled word

z + =

The above commands must be run from command or normal mode

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

Working with Documentation in Vim πŸ”

To launch the help system within nvim

:help

To generate documentation for installed plugins

:helptags ALL

Neovim Profiling πŸ”

To record verbose startup time of Vim or Neovim into a file

nvim --startuptime /tmp/nvim.log

Vim & Neovim Plugins πŸ”

To print all loaded vim scripts on a separate line

:scriptnames

Working with minpac πŸ”

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

To print or view 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

  3. Source the file

source %
  1. Then invoke the clean function from minpac
call minpac#clean()

I wrote a custom minpac binding to invoke the clean function, see below.

: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 (re)generated.

Working with Conqure of Completion

coc is a great Neovim plugin for managing language support for code completion. See my vimrc file for how I setup coc. I also installed denite.nvim for managing extensions installed via coc.

To list installed coc extensions using denite

🚨 TODO: FIXME: As of December 4, 2020 the below denite cmd is not working

Denite coc-extension

coc extensions can be installed, uninstalled, and toggled using denite

Personally I use coc-html, coc-css, coc-json, and coc-tsserver for doing web development with Neovim.

Another way to list installed coc extensions

cat ~/.config/coc/extensions/package.json

Packages can me added and removed by manually editing the above package.json file using yarn if using denite is not preferred, or they call be installed via Neovim by running;

CocInstall [PLUGIN_NAME]

Ex

CocInstall coc-html coc-css coc-json

Another alternative way to remove a COC extension is to call CocAction

:call CocAction('uninstallExtension', 'EXTENSION_NAME')

Troubleshooting CoC

When updating Coc using the below commands

:PackClean
:PackUpdate

It may be required to reinstall / build Coc after the update.

:call BuildCoc()

🚨 If Neovim python modules have been updating in a virtualenv because :CheckHealth reported that they were outdated, then Coc will have to be reinstalled / built again.

The error message I was seeing after updating the python provider packages for Neovim was

no matching autocommands

Working with ALE Asynchronous Lint Engine

ALE can operate as a linter, and a Language Server Protocol client

Working with fzf in Vim & Neovim πŸ”

TODO flesh out 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 PropTypes within a React project

pt ProTypes

The above command will search through an entire project for ProTypes and print the offending file along with the line number of the error on STDOUT.

When using ctrl + p fzf prints the file names realitve to the cwd within in Vim.

Working with Vimscript πŸ”

To get help writing Vimscript

: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 ==# performs 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 mr-fancy with mr-fancy-pants

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

To replace all occurrences 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 R

To collapse all code folds z+shift then m

Useful Links Code Folds πŸ”

Troubleshooting πŸ”

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

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

To see an example of bad or good startup time & performance

πŸ“Έ ⌚︎ Profiling startup time

Vim startup time profiling

Useful Links

TODOs

Clone this wiki locally