VIM: Difference between revisions

5,943 bytes added ,  15 March
no edit summary
→‎Miscellaneous: add reload .vimrc without quitting
No edit summary
 
(44 intermediate revisions by the same user not shown)
Line 1: Line 1:
I have a separate page for [[Neovim]].
= Keyboard shortcuts =
* <code>Ctrl + w t</code> <code>Ctrl + w K</code> - change split windows from vertical to horizontal split
* <code>Ctrl + w t</code> <code>Ctrl + w H</code> - change split windows from horizontal to vertical split
* <code>" + f + p</code> - paste the saved macro bound to f
* <code>" + 2 + p</code> - paste from a past yank at register #2
= With highlighted text =
* <code>~</code> - 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 =
= vimscript =


* [http://learnvimscriptthehardway.stevelosh.com/ Learn the Vimscript Hard Way]
* [http://learnvimscriptthehardway.stevelosh.com/ Learn the Vimscript Hard Way]


= Adding comments to a block =
 
= Instructions =
 
== add line numbers ==
 
Visual highlight and then start with number 42
 
<syntaxhighlight lang="vim">
:'<,'>s/^/\=(line('.')-line("'<")+42).' --> '/
</syntaxhighlight>
 
== beautify json ==
 
<syntaxhighlight lang="vim">
:%!python -m json.tool
</syntaxhighlight>
 
 
== Save a useful macro and retrieve for later use ==
 
In ~/.vimrc file, add as follows.  Macro content can be retrieved by pressing <code>"xp</code> where x is the bound key.
 
<syntaxhighlight lang="vim">
let @q = 'macro contents'
</syntaxhighlight>
 
 
== Adding comments to a block ==
 
=== Method 1 ===


Grab lines with visual block and then <code>:norm i#</code> to comment, and then <code>:norm x</code> or <code>:norm ^x</code> with indentation.
Grab lines with visual block and then <code>:norm i#</code> to comment, and then <code>:norm x</code> or <code>:norm ^x</code> with indentation.


= Emmet =


Also known as Zen Coding previously.
=== 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
 
<syntaxhighlight lang="vim">
(highlight lines) gJ
</syntaxhighlight>
 
 
2) Break tags to new lines


* [http://docs.emmet.io/cheat-sheet/ Emmet cheatsheet]
<syntaxhighlight lang="vim">
(highlight lines) :s/>\s*</>\r</g
</syntaxhighlight>


= Miscellaneous =


== Reload .vimrc without quitting ==
3) Set filetype
* ''Last tested on VIM 7.4 (2013 Aug 10)''


<syntaxhighlight lang="vim">
<syntaxhighlight lang="vim">
:so ~/.vimrc
:set ft=html
</syntaxhighlight>
</syntaxhighlight>


== Fixing an issue with Python version when using VIM with some packages ==
* ''Last tested on Ubuntu 16.04 LTS (xenial) | easy | less than five minutes''


When running <code>vim</code> with Powerline package on Ubuntu 16.04, I get this error message every time I execute it from the CLI.
4) Indent
 
<syntaxhighlight lang="vim">
(highlight lines) =
</syntaxhighlight>
 
== Remove all HTML tags ==


<syntaxhighlight lang="bash">
<syntaxhighlight lang="vim">
$ vim
:%s/<\_.\{-1,\}>//g
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
</syntaxhighlight>
</syntaxhighlight>


I'm not sure what this does, but this seems to solve the problem. Thanks to Sergey Khaylov for the answer at [http://stackoverflow.com/questions/20160902/how-to-solve-requires-python-2-x-support-in-linux-vim-and-it-have-python-2-6-6 Stackoverflow].


<syntaxhighlight lang="bash">
== netrw ==
$ apt-get install vim-nox
 
=== commonly used actions ===
 
* <code>d</code> create a new folder
* <code>%</code> create a new file
 
= .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.
 
<syntaxhighlight lang="vim">
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
</syntaxhighlight>
</syntaxhighlight>


= Initial setup =
= Initial setup (deprecated) =


Install [https://github.com/Shougo/neobundle.vim Shougo/neobundle.vim] from Github. It's a new package management tool for Vim that utilizes the Github repository.
Install [https://github.com/Shougo/neobundle.vim 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.
Here is my entire ~/.vimrc file.
<source lang="vim" enclose="div">
<syntaxhighlight lang="vim" enclose="div">
set nocompatible
set nocompatible
set noexpandtab
set noexpandtab
Line 113: Line 205:


autocmd FileType php setlocal omnifunc=phpcomplete_extended#CompletePHP
autocmd FileType php setlocal omnifunc=phpcomplete_extended#CompletePHP
</source>
</syntaxhighlight>


<code>set nocompatible</code> - forget about making Vim behave like Vi.
<code>set nocompatible</code> - forget about making Vim behave like Vi.
Line 137: Line 229:
<code>set ruler</code> - information such as line number and current position in the file are displayed on the status line
<code>set ruler</code> - information such as line number and current position in the file are displayed on the status line


<code>set foldmethod=indent</code> - another foldmethod I use regularly is marker ({{{ & }}}) when documenting code, but indent is a good start for reading through long lines of code
<code>set foldmethod=indent</code> - another foldmethod I use regularly is marker <nowiki>({{{ & }}})</nowiki> when documenting code, but indent is a good start for reading through long lines of code


<code>set foldnestmax=15</code> - the nest level for the code folding
<code>set foldnestmax=15</code> - the nest level for the code folding
Line 169: Line 261:
<code>NeoBundleFetch 'Shougo/neobundle.vim'</code> - as required by NeoBundle
<code>NeoBundleFetch 'Shougo/neobundle.vim'</code> - as required by NeoBundle


</code>filetype plugin indent on</code> - allow plugins to change the indent behavior (??)
filetype plugin indent on - allow plugins to change the indent behavior (??)


All those NeoBundle 'owner/package' lines are for installation of packages
All those NeoBundle 'owner/package' lines are for installation of packages
Line 218: Line 310:
:<nowiki>:</nowiki>syntax on
:<nowiki>:</nowiki>syntax on


= Possibly outdated by 2014 =
;Remove HTML tags
:<nowiki>:%s/<\_.\{-1,\}>//g</nowiki>
 
;Replace all tabs with 4 spaces
:%retab


== CTAG ==
;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 ==
 
<syntaxhighlight lang="vim">
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
</syntaxhighlight>
 
== Installing vim-plug ==
 
{{testedon|2019-12-06|Windows 10 v1903 (build#18362.449),VIM 8.1.1657}}
 
<syntaxhighlight lang="doscon">
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"
  )
)
</syntaxhighlight>
 
== Ultisnips requires py >= 2.7 or py3 ==
 
{{testedon|2019-12-06|Windows 10 v1903 (build#18362.449),VIM 8.1.1657,Python 3.8}}
 
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:
 
<syntaxhighlight lang="doscon">
C:\Python38> copy python38.dll python37.dll
</syntaxhighlight>
 
== Plug-ins ==
 
=== Hangeul ===
 
* [http://mearie.org/projects/vim-hangeul/ hangeul.vim | 메아리 프로젝트 (mearie.org)]
 
=== Emmet ===
 
Also known as Zen Coding previously.
 
* [http://docs.emmet.io/cheat-sheet/ Emmet cheatsheet]
 
=== Conquer of Completion ===
 
* [https://github.com/neoclide/coc.nvim neoclide/coc.nvim]
<syntaxhighlight lang="vim">
: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
</syntaxhighlight>
 
=== NERDTree ===
 
* [https://github.com/preservim/nerdtree preservim/nerdtree]
 
==== Shortcuts ====
 
<code>Shift + i</code> Toggle showing of the hidden files
 
=== gitgutter ===
 
* Activation (default on): <code>:GitGutterDisable</code>, <code>:GitGutterEnable</code>, <code>:GitGutterToggle</code>
* Signs toggle (default on): <code>:GitGutterSignsDisable</code>, <code>:GitGutterSignsEnable</code>, <code>:GitGutterSignsToggle</code>
* Highlights toggle (default off): <code>:GitGutterLineHighlightsDisable</code>, <code>:GitGutterLineHighlightsEnable</code>, <code>:GitGutterLineHighlightsToggle</code>
* Hunks
** Jump between hunks: <code>[c</code>, <code>]c</code>
** Stage hunk: <code><nowiki><Leader></nowiki>hs</code>
** Undo changes: <code><nowiki><Leader></nowiki>hu</code>
** Preview hunk's changes: <code><nowiki><Leader></nowiki>hp</code>
 
=== CTAG ===
 
''Possibly outdated by 2014''


;Rebuild a ctag file from a current project (assuming extensions are abc & def)
;Rebuild a ctag file from a current project (assuming extensions are abc & def)
Line 229: Line 413:
;http://runpaint.org/
;http://runpaint.org/
:vim and other tech books
:vim and other tech books
= Miscellaneous =
== Check VIM version ==
{{testedon|2013-08-10|VIM 7.4}}
<syntaxhighlight lang="vim">
:ve
</syntaxhighlight>
== Reload .vimrc without quitting ==
{{testedon|2013-08-10|VIM 7.4}}
<syntaxhighlight lang="vim">
:so ~/.vimrc
</syntaxhighlight>
== Fixing an issue with Python version when using VIM with some packages ==
{{testedon|xenial|Ubuntu 16.04 LTS}}
When running <code>vim</code> with Powerline package on Ubuntu 16.04, I get this error message every time I execute it from the CLI.
<syntaxhighlight lang="console" highlight="1">
$ 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
</syntaxhighlight>
I'm not sure what this does, but this seems to solve the problem. Thanks to Sergey Khaylov for the answer at [http://stackoverflow.com/questions/20160902/how-to-solve-requires-python-2-x-support-in-linux-vim-and-it-have-python-2-6-6 Stackoverflow].
<syntaxhighlight lang="bash">
$ apt-get install vim-nox
</syntaxhighlight>
= Compiling with python supports =
{{testedon|2023-10-03|RHEL 8.8}}
<syntaxhighlight lang="bash">
$ ./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
</syntaxhighlight>
= Links =
[https://dev.to/jovica/the-vim-trick-which-will-save-your-time-and-nerves-45pg The VIM trick which will save your time and nerves]