vim neovim Notes - ipatch/dotfiles GitHub Wiki
- General
- Install Vim & Neovim via brew
- File Structure
- Working with buffers
- Working with autocommands
- Searching for Patterns the platinum searcher
- Working with Cursor color and shape
- Working with Splits
- Working with Italic Fonts
- Working with Sessions and Views
- Python Support
- Working with Cursor color & shape
- Working with Tabs
- Working with wildmenu
- Working with
netrw - Spell Checking
- Visual Block mode
- Working with Documentation in Vim
- Neovim Profiling
- Vim & Neovim Plugins
- Working with Vimscript
- Troubleshooting
- Useful Links
- TODOs
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 ~
~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.plistTo convert a XML formatted plist file to binary using a CLI
plutil -convert binary1 /path/to/xml.plistThe 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.plistI 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.
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-listThat 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
2will do an exact literal string match formr42fancy.
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 clearTo 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=jsonTo 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 pasteInstalling 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.
libx11-devlibxtst-dev
apt-get install libx11-dev libxtst-devIf Vim has already been installed via brew it can be reinstalled,
brew -v reinstall --build-from-source --with-client-server vimTo test if xclip is working within a remote ssh session
echo "copy me" | xclip -selection clipboardTo print all variables and variable values ie. python_host_prog assigned within Vim or Neovim
:letTo copy all lines of an open buffer to the system clipboard
:%y+To display a list of commands in Vim or Neovim
:CommandsTo 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
:versionTo reload the current buffer
:editor
:source %To start Vim or Neovim without loading a .vimrc configuration file
vim -u NONEFile 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.vimVim 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:hTo get a high level overview of settings in Vim & Neovim
:browse optionsTo print out all the options / settings enbabled for the active buffer
:browse setTo 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-listTo toggle VISUAL mode
v
To toggle invisible characters
:set list!To exit PASTE mode
:set nopasteTo 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
:scriptnamesTo print the current working directory
:pwdTo change the local working directory
:lcdTo print the $VIMRUNTIME path
echo $VIMRUNTIMETo 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:imapTo display all key mappings within a Neovim session
:mapTo 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 eTo display a list of open buffers
:lsTo switch to the previous buffer
:bpTo switch to the new buffer
:bNWorking 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 ENDSearching 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
:vsplitTo create a horizontal split using a Vim command
:splitThe 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 0The 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.mdWith 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
:onis short for:only
Buffers can be accessed across multiple splits. π€
To learn more about using splits within Vim
:h opening-windowWorking 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- Credit: StackOverflow Franey's comment
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 viewoptionsWorking 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.vimTo 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.vimorview.vimfile exists use the!option to replace the existing file
ie.
To update a session file
:mks! ~/.vim/tmp/sessions/[mr-fancy-session.vim]To install universal-ctags on macOS
brew install --HEAD universal-ctags/universal-ctags/universal-ctagsPolluting 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
$PATHwhen trying to install a particular version of Python usingpyenv, 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/pyenvI 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/pyenvTo update the list of available pythons to install as well as pulling the latest version of pyenv itself
cd $PYENV_ROOT; git pullTo get virtualenv support with python install pyenv-virtualenv
git clone https://github.com/pyenv/pyenv-virtualenv $PYENV_ROOT/plugins/pyenv-virtualenvOnce 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.15To setup a virtualenv named py2neovim for the python-neovim provider packages
pyenv virtualenv 2.7.15 py2neovimTo activate the neovim2 virtual environment
pyenv activate py2neovimTo install the neovim python module for Neovim within the neovim2 virtualenv
pip install neovimTo upgrade the neovim provider package if it just needs updating
pip install --upgrade neovimTo upgrade the pip package manager itself
pip install --upgrade pipThe above command
pip install neovimworks 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.vimMAKE CERTAIN to use the full path, ie. DO NOT use the$HOMEenv var or else:checkhealthwill fail.
Add the above python path to init.vim
To activate the py2neovim virtualenv
pyenv activate py2neovimTo exit out of either virtualenv
pyenv deactivateTo 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
:versionNeovim 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-pythonVim 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
:tabnewTo 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
:VexTo 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,
- find the buffer number with
:ls - close the buffer with
:bd[#], ie.:bd42
To rename the current buffer or file in vim / Neovim
:E
RR is pressed in the context of the netrw explorer
Spell Checking π
To enable spell checking in vim
set spellA 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
:helpTo generate documentation for installed plugins
:helptags ALLNeovim Profiling π
To record verbose startup time of Vim or Neovim into a file
nvim --startuptime /tmp/nvim.logVim & Neovim Plugins π
To print all loaded vim scripts on a separate line
:scriptnamesminpac builds on top Vim's job-control features introduced in Vim 8.
To print or view a log of messages from running PackUpdate
:messagesTo remove a plugin installed with minpac
-
Remove the
call minpac#add('author/plugin-name')from your.vimrc -
Save the
.vimrc -
Source the file
source %- Then invoke the
cleanfunction from minpac
call minpac#clean()I wrote a custom minpac binding to invoke the
cleanfunction, see below.
:PackCleanMinpac stores plugins downloaded from git repositories in the following directories
$dots/editors/vim/vim/pack/minpac/startor
$HOME/.vim/pack/minpac/start/To generate help tag files for plugins maintained by minpac
helptags ALLWhen 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-extensioncoc 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.jsonPackages 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-jsonAnother alternative way to remove a COC extension is to call CocAction
:call CocAction('uninstallExtension', 'EXTENSION_NAME')When updating Coc using the below commands
:PackClean
:PackUpdateIt 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 ProTypesThe 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-listTo 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"
endfunctionFind and Replace π
To replace mr-fancy with mr-fancy-pants
:s/mr-fancy/mr-fancy-pantsTo replace all occurrences of mr-fancy with mr-fancy-pants
:s/mr-fancy/mr-fancy-pants/gUseful 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-exprTo expand all code folds z+shift then R
To collapse all code folds z+shift then m
Useful Links Code Folds π
- vimcasts.org setup custom markdown code folding expr
- YouTube wincent
- stackexchange.com code folding
- learnvimscriptthehardway Advanced Folding
Troubleshooting π
If Vim or Neovim is getting sluggish when starting up, profile the start time
vim --startuptime ~/logs/nvim-boot-time.logTo see an example of bad or good startup time & performance
