-
Notifications
You must be signed in to change notification settings - Fork 5
vim neovim Notes
- 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
- Getting Italic font working
- 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 π
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
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 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.
libx11-devlibxtst-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
:letTo 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
: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: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
:pwdTo change the local working directory
:lcdTo 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: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 e
To 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 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
: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 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.
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
:onis short for:only
Buffers can be accessed across multiple splits. π€
To learn more about using splits within Vim
:h opening-window
Getting Italic font working π
- 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 viewoptions
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.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]
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
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-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.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 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 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. 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
:Vex
To create a new file within the current working directory followed by a filename
:% [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
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
:helpTo generate documentation for installed plugins
:helptags ALL
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
:scriptnamesVim & Neovim 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 name 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
.mddocument in the default browser.
minpac 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 %
- Then invoke the
cleanfunction from minpac
call minpac#clean()
I wrote a custom minpac binding to invoke the
cleanfunction, see below.
:PackClean
Minpac 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 ALL
When running minpac#update() tag files will be (re)generated.
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 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.
Working with Airline π
Working with Deoplete π
The below no longer applies to my setup because I switched to
pyenvto manage pythons.
If Deoplete whines about missing neovim python module / plugin, run the below command
pip3 install --upgrade neovim
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 o
To collapse all code folds z+shift then m
- 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
πΈ βοΈ Profiling startup time
Vim startup time profiling
- ~~~do some research on
vim-projectionist~~~
-
Linux and macOS Operation Notes
- β macOS Op Notes
- π§ Linux Op Notes
- Vim & Neovim Notes
- git Notes
- π fish shell Notes
- ECMAScript Tooling
- π₯§ Raspberry Pi Notes
- asdf version manager Notes
- Bind9 Notes
- Creating a custom motd on Debian Jessie
- ECMAScript Tooling
- Email client Notes
- Email Server Setup Notes Postfix & Dovecot
- Emoji side quest
- fish shell Notes
- π₯ π€ git it got it good Notes
- git Notes
- Graphics and Image Processing Notes
- GUI text editor Notes
- π»π§ Homebrew and Linuxbrew formula Notes
- Linux and macOS Administration Notes
- Linux and macOS Troubleshooting Notes
- MacBook Pro Late 2013 Notes
- Vim & Neovim Notes
- Video Production Notes
- Python Notes
- radare Notes
- Raspberry Pi Notes
- Terminal Emulators
- Tmux Notes
- Web Browser Notes
- Weechat Notes
- Microsoft Windows Notes