VIM: Difference between revisions

4,570 bytes added ,  15 March
no edit summary
mNo edit summary
No edit summary
 
(31 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 =
= New developments with v8 =


With the dawn of Neovim in 2014, there were two major new developments: non-blocking, asynchronous execution, and terminal
With the dawn of Neovim in 2014, there were two major new developments: non-blocking, asynchronous execution, and terminal


= vimscript =
= vimscript =
Line 7: Line 21:
* [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.
=== 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
<syntaxhighlight lang="vim">
(highlight lines) :s/>\s*</>\r</g
</syntaxhighlight>
3) Set filetype
<syntaxhighlight lang="vim">
:set ft=html
</syntaxhighlight>
4) Indent
<syntaxhighlight lang="vim">
(highlight lines) =
</syntaxhighlight>
== Remove all HTML tags ==
<syntaxhighlight lang="vim">
:%s/<\_.\{-1,\}>//g
</syntaxhighlight>
== netrw ==
=== commonly used actions ===
* <code>d</code> create a new folder
* <code>%</code> create a new file


= .vimrc =
= .vimrc =
Line 15: Line 116:
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.
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.


<source lang="vim">
<syntaxhighlight lang="vim">
if &compatible
if &compatible
   set nocompatible
   set nocompatible
Line 31: Line 132:


syntax on  
syntax on  
</source>
</syntaxhighlight>


= Initial setup (deprecated) =
= Initial setup (deprecated) =
Line 38: Line 139:


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 104: 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 160: 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 209: Line 310:
:<nowiki>:</nowiki>syntax on
:<nowiki>:</nowiki>syntax on


= Plug-ins =
;Remove HTML tags
:<nowiki>:%s/<\_.\{-1,\}>//g</nowiki>
 
;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 ==
 
<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 ==


== Emmet ==
{{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.
Also known as Zen Coding previously.
Line 217: Line 374:
* [http://docs.emmet.io/cheat-sheet/ Emmet cheatsheet]
* [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 ===


== gitgutter ==
* [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>
* Activation (default on): <code>:GitGutterDisable</code>, <code>:GitGutterEnable</code>, <code>:GitGutterToggle</code>
Line 229: Line 402:
** Preview hunk's changes: <code><nowiki><Leader></nowiki>hp</code>
** Preview hunk's changes: <code><nowiki><Leader></nowiki>hp</code>


== CTAG ==
=== CTAG ===


''Possibly outdated by 2014''
''Possibly outdated by 2014''
Line 247: Line 420:
{{testedon|2013-08-10|VIM 7.4}}
{{testedon|2013-08-10|VIM 7.4}}


<source lang="vim">
<syntaxhighlight lang="vim">
:ve
:ve
</source>
</syntaxhighlight>


== Reload .vimrc without quitting ==
== Reload .vimrc without quitting ==
{{testedon|2013-08-10|VIM 7.4}}
{{testedon|2013-08-10|VIM 7.4}}


<source lang="vim">
<syntaxhighlight lang="vim">
:so ~/.vimrc
:so ~/.vimrc
</source>
</syntaxhighlight>


== Fixing an issue with Python version when using VIM with some packages ==
== Fixing an issue with Python version when using VIM with some packages ==
{{testedoni|xenial|Ubuntu 16.04 LTS}}
{{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.
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.


<source lang="console" highlight="1">
<syntaxhighlight lang="console" highlight="1">
$ vim
$ vim
UltiSnips requires py >= 2.7 or py3
UltiSnips requires py >= 2.7 or py3
Line 270: Line 443:
details.
details.
Press ENTER or type command to continue
Press ENTER or type command to continue
</source>
</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].
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].


<source lang="bash">
<syntaxhighlight lang="bash">
$ apt-get install vim-nox
$ apt-get install vim-nox
</source>
</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]