VIM
I have a separate page for Neovim.
Keyboard shortcuts
Ctrl + w t
Ctrl + w K
- change split windows from vertical to horizontal splitCtrl + w t
Ctrl + w H
- change split windows from horizontal to vertical split" + f + p
- paste the saved macro bound to f
With highlighted text
~
- reverse capitalization
New developments with v8
With the dawn of Neovim in 2014, there were two major new developments: non-blocking, asynchronous execution, and terminal
vimscript
Instructions
beautify json
:%!python -m json.tool
Save a useful macro and retrieve for later use
In ~/.vimrc file, add as follows. Macro content can be retrieved by pressing "xp
where x is the bound key.
let @q = 'macro contents'
Adding comments to a block
Method 1
Grab lines with visual block and then :norm i#
to comment, and then :norm x
or :norm ^x
with indentation.
Method 2
Commenting a block
- Ctrl-v and select multiple lines to comment
- Shift-I
- Add comment characters to add and then press Esc
Uncommenting a block
- Ctrl-v and select the block to uncomment
- press x
Auto-format HTML
1) Join all the lines
(highlight lines) gJ
2) Break tags to new lines
(highlight lines) :s/>\s*</>\r</g
3) Set filetype
:set ft=html
4) Indent
(highlight lines) =
Remove all HTML tags
:%s/<\_.\{-1,\}>//g
.vimrc
With the introduction of internal package management system, there is no longer a logical need to install a third-party package management tool. Here's a refresh of my .vimrc as of mid-2018 with VIM v8.
if &compatible
set nocompatible
endif
if has('packages')
packloadall
else
" Use vim-plug
filetype off
source $HOME/.vim/plugged/vim-plug/plug.vim
call plug#begin('~/.vim/plugged')
call plug#end()
endif
syntax on
Initial setup (deprecated)
Install Shougo/neobundle.vim from Github. It's a new package management tool for Vim that utilizes the Github repository.
Here is my entire ~/.vimrc file.
set nocompatible
set noexpandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set smartindent
set autoindent
set encoding=utf-8
set number
set autowrite
set ruler
set foldmethod=indent
set foldnestmax=15
set nofoldenable
set foldlevel=1
set timeoutlen=500
set hidden
set t_Co=256
set statusline=%F\ \%{fugitive#statusline()}\ \%=\ \%y\ -\ %l/%L
set laststatus=2
set runtimepath+=~/.vim/bundle/neobundle.vim/
let g:phpcomplete_index_composer_command="composer"
let g:Powerline_symbols = "fancy"
let g:gitgutter_sign_added = '++'
let g:gitgutter_sign_modified = 'MM'
let g:gitgutter_sign_removed = '--'
let g:gitgutter_sign_modified_removed = 'M-'
call neobundle#rc(expand('~/.vim/bundle/'))
NeoBundleFetch 'Shougo/neobundle.vim'
filetype plugin indent on
NeoBundle 'Shougo/vimproc', {
\ 'build' : {
\ 'windows' : 'make -f make_mingw32.mak',
\ 'cygwin' : 'make -f make_cygwin.mak',
\ 'mac' : 'make -f make_mac.mak',
\ 'unix' : 'make -f make_unix.mak',
\ },
\ }
NeoBundle 'Shougo/unite.vim'
NeoBundle 'Shougo/vimshell.vim'
NeoBundle 'airblade/vim-gitgutter'
NeoBundle 'Lokaltog/powerline',{'rtp': 'powerline/bindings/vim/'}
NeoBundle 'tpope/vim-fugitive'
NeoBundle 'kien/ctrlp.vim'
NeoBundle 'vim-scripts/sudo.vim'
NeoBundle 'm2mdas/phpcomplete-extended'
NeoBundle 'm2mdas/phpcomplete-extended-laravel'
NeoBundle 'chriskempson/vim-tomorrow-theme'
NeoBundleCheck
filetype plugin indent on
colorscheme torte
highlight LineNr ctermfg=darkgrey
highlight SignColumn ctermbg=black
highlight Folded ctermfg=11 ctermbg=23
autocmd FileType php setlocal omnifunc=phpcomplete_extended#CompletePHP
set nocompatible
- forget about making Vim behave like Vi.
set noexpandtab
- do not expand tabs into spaces; use the tab character
set tabstop=4
- uses 4 character spaces for a tab
set shiftwidth=4
- move 4 spaces for reindent operation (<< or >> shortcut keys)
set softtabstop=4
- number of spaces a tab uses in INSERT mode
set smartindent
- use smart indent mode -- i.e. entering after a opening bracket would automatically indent and in other similar situations
set autoindent
- use the same indentation as the last line
set encoding=utf-8
- set encoding
set number
- turn on line numbers (set nonumber
to turn it off)
set autowrite
- automatically save when changing between buffers
set ruler
- information such as line number and current position in the file are displayed on the status line
set foldmethod=indent
- another foldmethod I use regularly is marker ({{{ & }}}) when documenting code, but indent is a good start for reading through long lines of code
set foldnestmax=15
- the nest level for the code folding
set nofoldenable
- you don't want Vim to fold your code by default on initial load of a file
set foldlevel=1
- when executing close command for code folding everything will be folded down to level 1, or the top level
set timeoutlen=500
- keycode delay to be used; default is 1000, but this reduces the polling time when you press on a Esc to change modes
set hidden
- allows loading of a buffer in a window that has a modified buffer
set t_Co=256
- allow Vim to understand that the terminal is using 256 colors
set statusline=%F\ \%{fugitive#statusline()}\ \%=\ \%y\ -\ %l/%L
- this is the default for the fugitive installation
set laststatus=2
- turn the status line on
set runtimepath+=~/.vim/bundle/neobundle.vim/
- required for neobundle installation
let g:phpcomplete_index_composer_command="composer"
- by default, it is "php composer.phar", so this is according to how you've customized your development environment
let g:Powerline_symbols="fancy"
- I haven't confirmed this yet, but this probably allows Lokaltog/powerline plugin to use Private Use Area of the Unicode of fonts specifically patched for Powerline characters
let g:gitgutter_sign_added = '++'
- I like my GitGutter to occupy two characters for better visibility
...
call neobundle#rc(expand('~/.vim/bundle/'))
- I think this sets this directory as NeoBundle's target bundle directory; required by NeoBundle
NeoBundleFetch 'Shougo/neobundle.vim'
- as required by NeoBundle
filetype plugin indent on - allow plugins to change the indent behavior (??)
All those NeoBundle 'owner/package' lines are for installation of packages
NeoBundle 'Shougo/vimproc'...
- vimproc allows real-time update of certain components that uses this package
NeoBundle 'Shougo/unite.vim'
- similar to ctrlp, allows extensive searching features and UI
NeoBundle 'Shougo/vimshell.vim'
- allows bash shell like interface within vim
NeoBundle 'airblade/vim-gitgutter'
- Git diff marks are displayed somewhat realtime
NeoBundle 'Lokaltog/powerline',{'rtp': 'powerline/bindings/vim/'}
- one of the most popular status line plugins
NeoBundle 'tpope/vim-fugitive'
- run Git commands within Vim
NeoBundle 'kien/ctrlp.vim'
- extensive searching tool
NeoBundle 'vim-scripts/sudo.vim'
- installed to get rid of the annoying msg while running Vim as a sudo
NeoBundle 'm2mdas/phpcomplete-extended'
- command completion similar to ones found in many GUI IDEs
NeoBundle 'm2mdas/phpcomplete-extended-laravel'
- this is add-on allows completion for Laravel 4
NeoBundle 'chriskempson/vim-tomorrow-theme'
- I sometimes use this theme
NeoBundleCheck
- NeoBundle requires this
colorscheme torte
- Use this color scheme -- a default one I like
highlight LineNr ctermfg=darkgrey
- color of the line numbers
highlight SignColumn ctermbg=black
- color of the gutter used by GitGutter
highlight Folded ctermfg=11 ctermbg=23
- the default folded code color is an eyesore, so replace it
autocmd FileType php setlocal omnifunc=phpcomplete_extended#CompletePHP
- this completes the phpcomplete_extended plugin installation
Useful commands
- Turn number lines
- :set number
- Wordwrap off
- :set nowrap
- Syntax on
- :syntax on
- Remove HTML tags
- :%s/<\_.\{-1,\}>//g
- Replace all tabs with 4 spaces
- %retab
- Set tab to 4 spaces
- set expandtab ts=4 sw=4 ai
- Remove trailing whitespaces
- %s/\s\+$//e
gvim on Windows 10
Setting GUI font in .vimrc
if has("gui_running")
if has("gui_gtk2")
set guifont=Inconsolata\ 12
elseif has("gui_macvim")
set guifont=Menlo\ Regular:h14
elseif has("gui_win32")
set guifont=Consolas:h11:cANSI
endif
endif
Installing vim-plug
- Last tested on Windows 10 v1903 (build#18362.449),VIM 8.1.1657 (2019-12-06)
PS C:\> $uri = 'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
(New-Object Net.WebClient).DownloadFile(
$uri,
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(
"~\vimfiles\autoload\plug.vim"
)
)
Ultisnips requires py >= 2.7 or py3
- Last tested on Windows 10 v1903 (build#18362.449),VIM 8.1.1657,Python 3.8 (2019-12-06)
This may not be the best method, so it's more of a workaround. Install Python3 (e.g. via Chocolatey) and make a copy of pythonXX.dll under the python folder (e.g. \Python38) to the version that vim is looking for. For example, if Python 3.8 was installed, but vim is compiled with Python 3.7, then you would make a copy as follows:
C:\Python38> copy python38.dll python37.dll
Plug-ins
Emmet
Also known as Zen Coding previously.
Conquer of Completion
:CocInstall coc-tsserver coc-json coc-html coc-git coc-css coc-phpls coc-blade coc-docker coc-emmet coc-powershell coc-sql coc-tailwindcss coc-xml coc-yaml
:CocInstall @yaegassy/coc-laravel @yaegassy/coc-nginx
NERDTree
Shortcuts
Shift + i
Toggle showing of the hidden files
gitgutter
- Activation (default on):
:GitGutterDisable
,:GitGutterEnable
,:GitGutterToggle
- Signs toggle (default on):
:GitGutterSignsDisable
,:GitGutterSignsEnable
,:GitGutterSignsToggle
- Highlights toggle (default off):
:GitGutterLineHighlightsDisable
,:GitGutterLineHighlightsEnable
,:GitGutterLineHighlightsToggle
- Hunks
- Jump between hunks:
[c
,]c
- Stage hunk:
<Leader>hs
- Undo changes:
<Leader>hu
- Preview hunk's changes:
<Leader>hp
- Jump between hunks:
CTAG
Possibly outdated by 2014
- Rebuild a ctag file from a current project (assuming extensions are abc & def)
- find . -type f -name \*.abc -o -name \*.def | xargs ctags --language-force=php
- (to add more extensions) find . -type f -name \*.ghi | xargs ctags -a --language-force=;http://www.thegeekstuff.com/2009/04/ctags-taglist-vi-vim-editor-as-sourece-code-browser/
- ctags and taglist
- http://runpaint.org/
- vim and other tech books
Miscellaneous
Check VIM version
- Last tested on VIM 7.4 (2013-08-10)
:ve
Reload .vimrc without quitting
- Last tested on VIM 7.4 (2013-08-10)
:so ~/.vimrc
Fixing an issue with Python version when using VIM with some packages
- Last tested on Ubuntu 16.04 LTS (xenial)
When running vim
with Powerline package on Ubuntu 16.04, I get this error message every time I execute it from the CLI.
$ vim
UltiSnips requires py >= 2.7 or py3
You need vim compiled with Python 2.6, 2.7 or 3.2 and later support
for Powerline to work. Please consult the documentation for more
details.
Press ENTER or type command to continue
I'm not sure what this does, but this seems to solve the problem. Thanks to Sergey Khaylov for the answer at Stackoverflow.
$ apt-get install vim-nox
Compiling with python supports
- Last tested on RHEL 8.8 (2023-10-03)
$ ./configure --with-features=huge --enable-pythoninterp --with-python-config-dir=/usr/lib64/python2.7/config --enable-python3interp --with-python3-config-dir=/usr/lib64/python3.6/config-3.6m-x86_64-linux-gnu --prefix=/usr --enable-fail-if-missing