1. Information
-
Note: When using an unconfigured vim to copy and paste text into a file it may auto indent the pasted text or the right mouse click may not paste the text at all.
-
Type
:set compatibleand press Enter in vim before pasting any text.
-
-
Configuration of Vim can be done in several different ways and is a huge topic.
-
To figure out what configuration files are loaded and when use the following commands.
:echo $VIM " /usr/share/vim " C:\Program Files\Vim :echo $VIMRUNTIME " /usr/share/vim/vim91 " C:\Program Files\Vim\vim91 :scriptnames " List loaded scripts. :set runtimepath? " List all runtime directories. :version
1.1. Linux
For global or system wide Vim configuration use the /etc/vim/vimrc.local file that is sourced by the /etc/vim/vimrc file. Put folders used for syntax highlighting in the /etc/vim folder. Set the Vim backup folder to $HOME/.vim/backup, backups will persist across reboots and only be accessible to you.
1.2. Windows
For global or system wide Vim configuration use the C:\Program Files\Vim\vimrc file. Put folders used for syntax highlighting in the C:\Program Files\Vim\vimfiles folder. Set the Vim backup folder to $TEMP\, backups will persist across reboots and only be accessible to you.
2. Linux
2.1. Installation
-
Enter the following commands at a Command Line.
sudo apt-get install vim sudo apt-get install ctags sudo apt-get install vim-gnome # When having a graphical user interface.
2.2. Configuration
-
Enter the following commands at a Command Line.
vi --version ls -al ~/.vim sudo ls -al ~root/.vim # Add vim backup folder for root user. folder="/root/.vim/backup" sudo mkdir -p "${folder}" # Get local user accounts. users="$(awk -F: '($3>=1000)&&($7!="/bin/false")&&($7!="/usr/sbin/nologin"){print $1}' /etc/passwd)" # Add vim backup folder for all users. for user in "${users}"; do folder="/home/${user}/.vim/backup" sudo mkdir -p "${folder}" sudo chown "${user}:${user}" "/home/${user}/.vim" sudo chown "${user}:${user}" "/home/${user}/.vim/backup" done
2.2.1. Global Configuration File
-
Make sure to create backup folder ($HOME/.vim/backup) for each user.
-
Contents of the /etc/vim/vimrc.local file.
if exists('${DISPLAY}') " Running under X11. " Set default background color after detecting terminal background color. set background& else " Running on console. set background=dark endif autocmd BufEnter * :syntax sync fromstart autocmd BufNewFile,BufWinEnter * setlocal cinoptions= autocmd BufNewFile,BufWinEnter * setlocal formatoptions-=c autocmd BufNewFile,BufWinEnter * setlocal formatoptions-=o autocmd BufNewFile,BufWinEnter * setlocal formatoptions-=r autocmd BufNewFile,BufWinEnter * setlocal formatoptions-=t autocmd BufNewFile,BufWinEnter * setlocal indentkeys= autocmd BufNewFile,BufRead * setlocal mouse= autocmd BufNewFile,BufWinEnter * setlocal nocindent autocmd BufNewFile,BufWinEnter * setlocal nosmartindent "colorscheme industry " Custom asciidoctor syntax highlighting. highlight link asciidocListingBlock Type highlight link asciidocLiteralBlock Statement highlight link asciidocLiteralParafraph Statement " See https://vimdoc.sourceforge.net/htmldoc/term.html " Fix cursor in Windows Terminal after using vim. " t_SI Start insert mode " t_SR Start replace mode " t_EI End insert or replace mode " Cursor settings: " 1 -> blinking block " 2 -> solid block " 3 -> blinking underscore " 4 -> solid underscore " 5 -> blinking vertical bar " 6 -> solid vertical bar let &t_SI = "\<Esc>[1 q" let &t_SR = "\<Esc>[1 q" let &t_EI = "\<Esc>[1 q" " Prevent ^[[>0;136;0c garbage characters or escape codes from showing. "set t_RV= " Prevent ^[[2;2R garbage characters or escape codes from showing. "set t_u7= " The .bash_aliases file is sourced from .bashrc, but only for interactive shells. " The vim :! command is used to run external shell commands, but only within a non-interactive, non-login shell. " Make the aliases file is actually read each time an external shell command is run from within vim. let $BASH_ENV = "~/.bash_aliases" " set compatible set encoding=utf-8 set hlsearch set ignorecase set modeline set noautoindent set printoptions=paper:a4 set title set viminfo='100,<500,s10,h set wrap " Save backup, swap and undo files in your home folder. " Backups will persist across reboots and only be accessible to you. " The double slash (//) tells Vim to use the full path of the file being edited to create the backup filename, " which prevents filename collisions when having files with the same name in different directories. " Vim will still use /tmp and the current directory as fallback options if the primary folder is not writable. set backup set backupdir=$HOME/.vim/backup//,/tmp//,. set directory=$HOME/.vim/backup//,/tmp//,. set undodir=$HOME/.vim/backup//,/tmp//,.
2.2.2. Make Vim the default editor
-
Enter the following commands at a Command Line.
sudo update-alternatives --set editor /usr/bin/vim.basic
2.2.3. Syntax Highlighting
-
Install Git.
2.2.3.1. Ansible
-
Enter the following commands at a Command Line.
cd /tmp git clone https://github.com/pearofducks/ansible-vim.git sudo cp -a ansible-vim/ftdetect /etc/vim sudo cp -a ansible-vim/ftplugin /etc/vim sudo cp -a ansible-vim/indent /etc/vim sudo cp -a ansible-vim/syntax /etc/vim -
The following plugin displays thin vertical lines at each indentation level for code indented with spaces.
-
Enter the following commands at a Command Line.
cd /tmp git clone https://github.com/Yggdroot/indentLine.git sudo cp -a indentLine/after /etc/vim sudo cp -a indentLine/doc /etc/vim sudo cp -a indentLine/glyph /etc/vim
2.2.3.2. Asciidoc
Vim 9 includes built-in support for AsciiDoc syntax highlighting, which is automatically activated for files with the .asciidoc or .adoc extensions. Some customization can be done in the /etc/vim/vimrc.local file.
2.2.3.3. PowerShell
-
Enter the following commands at a Command Line.
cd /tmp git clone https://github.com/PProvost/vim-ps1.git sudo cp -a vim-ps1/ftdetect /etc/vim sudo cp -a vim-ps1/ftplugin /etc/vim sudo cp -a vim-ps1/indent /etc/vim sudo cp -a vim-ps1/syntax /etc/vim
3. Windows
3.1. Installation
-
Uninstall any previous version.
-
Run the gvim_9.1.1825_x64.exe file with administrative privileges.
-
Select English.
-
Click OK.
-
Click Next.
-
Check I accept the terms of the License Agreement.
-
Click Next.
-
Check Create .bat files.
-
Expand Create icons for Vim.
-
Uncheck On the Desktop.
-
Expand Create Plugin Directories.
■ Vim GUI and runtime files ■ Vim console program ■ Create .bat files ■ Create icons for Vim □ On the Desktop ■ In the Start Menu Programs Folder ■ Add Vim Context Menu ■ Create Default Config ■ Create Plugin Directories ■ Private □ Shared ■ Native Language Support
-
Click Next.
-
Select Vi / Vim behavior | Vim with some enhancements (load defaults.vim).
-
Note: Selecting Remap a few keys remaps Ctrl-A to Select All.
-
Note: Selecting Remap a few keys remaps Ctrl-C to Copy to Clipboard.
-
Note: Selecting Remap a few keys remaps Ctrl-F from Page forward to the Find string dialog.
-
Note: Selecting Remap a few keys remaps Ctrl-V to Paste from Clipboard.
-
Select Mappings | Do not remap keys (Default).
-
Select Mouse | Right: popup menu, Left: select mode (Windows).
Vi / Vim behavior Compatibility and enhancements Vim with some enhancements (load defaults.vim) Mappings Remap a few keys for Windows (Ctrl-V, Ctrl-C, Ctrl-A, Ctrl-S, Ctrl-F, etc) Do not remap keys (Default) Mouse Behavior of right and left buttons Right: popup menu, Left: select mode (Windows) -
Click Next.
C:\Program Files\Vim
-
Click Install.
-
Uncheck Show README after installation finished.
-
Click Finish.
3.2. Configuration
3.2.1. Global Configuration File
-
Contents of the C:\Program Files\Vim\vimrc file.
"autocmd BufEnter * :syntax sync fromstart autocmd BufNewFile,BufWinEnter * setlocal cinoptions= autocmd BufNewFile,BufWinEnter * setlocal formatoptions-=c autocmd BufNewFile,BufWinEnter * setlocal formatoptions-=o autocmd BufNewFile,BufWinEnter * setlocal formatoptions-=r autocmd BufNewFile,BufWinEnter * setlocal formatoptions-=t autocmd BufNewFile,BufRead * setlocal indentkeys= autocmd BufNewFile,BufWinEnter * setlocal nocindent autocmd BufNewFile,BufWinEnter * setlocal nosmartindent autocmd BufNewFile,BufRead * setlocal noundofile " autocmd BufNewFile,BufRead * silent! map <C-a> ggVG " autocmd BufNewFile,BufRead * silent! unmap <C-F> " map <C-a> <esc>ggVG<CR> "colorscheme industry " Custom asciidoctor syntax highlighting. highlight link asciidocListingBlock Type highlight link asciidocLiteralBlock Statement highlight link asciidocLiteralParafraph Statement " Fix cursor in Windows Terminal after using vim. " Cursor settings: " 1 -> blinking block " 2 -> solid block " 3 -> blinking underscore " 4 -> solid underscore " 5 -> blinking vertical bar " 6 -> solid vertical bar let &t_SI = "\<Esc>[1 q" let &t_SR = "\<Esc>[1 q" let &t_EI = "\<Esc>[1 q" set compatible set encoding=utf-8 set guifont=Courier_New:h12:b:cANSI set hlsearch set ignorecase set modeline set noautoindent set printoptions=paper:a4 set title set viminfo='100,<500,s10,h set visualbell set wrap " Save backup, swap and undo files in your home folder. " Backups will persist across reboots and only be accessible to you. " The double slash (//) tells Vim to use the full path of the file being edited to create the backup filename, " which prevents filename collisions when having files with the same name in different directories. " Vim will still use the current directory as a fallback option if the primary folder is not writable. set backup set backupdir=$TEMP//,. set directory=$TEMP//,. set undodir=$TEMP//,. " vim -b : edit binary using xxd-format! augroup Binary au! au BufReadPre *.bin,*.dll,*.exe,*.prn let &bin=1 au BufReadPost *.bin,*.dll,*.exe,*.prn if &bin | %!xxd -g1 au BufReadPost *.bin,*.dll,*.exe,*.prn set ft="xxd -g1" | endif au BufWritePre *.bin,*.dll,*.exe,*.prn if &bin | %!xxd -g1 -r au BufWritePre *.bin,*.dll,*.exe,*.prn endif au BufWritePost *.bin,*.dll,*.exe,*.prn if &bin | %!xxd -g1 au BufWritePost *.bin,*.dll,*.exe,*.prn set nomod | endif augroup END
3.2.2. Syntax Highlighting
-
Install Git.
3.2.2.1. Ansible
-
Enter the following commands at a Command Line.
cd %TEMP% git.exe clone https://github.com/pearofducks/ansible-vim.git xcopy /e /i /y ansible-vim\ftdetect "C:\Program Files\Vim\vimfiles\ftdetect" xcopy /e /i /y ansible-vim\ftplugin "C:\Program Files\Vim\vimfiles\ftplugin" xcopy /e /i /y ansible-vim\indent "C:\Program Files\Vim\vimfiles\indent" xcopy /e /i /y ansible-vim\syntax "C:\Program Files\Vim\vimfiles\syntax" -
The following plugin displays thin vertical lines at each indentation level for code indented with spaces.
-
Enter the following commands at a Command Prompt with administrative privileges.
cd %TEMP% git.exe clone https://github.com/Yggdroot/indentLine.git xcopy /e /i /y indentLine\after "C:\Program Files\Vim\vimfiles\after" xcopy /e /i /y indentLine\doc "C:\Program Files\Vim\vimfiles\doc" xcopy /e /i /y indentLine\glyph "C:\Program Files\Vim\vimfiles\glyph"
3.2.2.2. Asciidoc
Vim 9 includes built-in support for AsciiDoc syntax highlighting, which is automatically activated for files with the .asciidoc or .adoc extensions. Some customization can be done in the /etc/vim/vimrc.local file.
3.2.2.3. PowerShell
-
Enter the following commands at a Command Prompt with administrative privileges.
cd %TEMP% git.exe clone https://github.com/PProvost/vim-ps1.git xcopy /e /i /y vim-ps1\ftdetect "C:\Program Files\Vim\vimfiles\ftdetect" xcopy /e /i /y vim-ps1\ftplugin "C:\Program Files\Vim\vimfiles\ftplugin" xcopy /e /i /y vim-ps1\indent "C:\Program Files\Vim\vimfiles\indent" xcopy /e /i /y vim-ps1\syntax "C:\Program Files\Vim\vimfiles\syntax" -
Note: When using an unconfigured vim to copy and paste text into a file it may auto indent the pasted text or the right mouse click may not paste the text at all.
-
Type
:set compatibleand press Enter in vim before pasting any text.
-
4. Vim
4.1. Accents
Use the following table to enter accents in Vim
By decimal value |
Ctrl+V nnn |
000 <= nnn <= 255 |
By decimal value |
Ctrl+V nnn |
000 <= nnn <= 255 |
By octal value |
Ctrl+V Onnn or Ctrl+V onnn |
000 <= nnn <= 377 |
By hex value |
Ctrl+V Xnn or Ctrl+V xnn |
00 <= nn <= FF |
By hex value for BMP Unicode codepoints |
Ctrl+V unnnn |
0000 <= nnnn <= FFFF |
By hex value for any Unicode codepoint |
Ctrl+V Unnnnnnnn |
00000000 <= nnnnnnnn <= 7FFFFFFF |
4.2. Common Pitfalls
4.2.1. Frozen Command Line
-
Note: On Linux, accidentally pressing Ctrl+S freezes the Command Line.
Press Ctrl+Q to unfreeze.
4.2.2. Suspended Vim Session
-
Note: On Linux, accidentally pressing Ctrl+Z suspends the current Vim session.
Typefg(bring to foreground) to resume.
4.2.3. Vim: Warning: Output is not to a terminal
-
Note: The next commands give a Vim: Warning: Output is not to a terminal warning.
Type:q!and press Enter to exit Vim.vim foo.txt | <command> vim foo.txt > <file>
-
Note: On Linux, the vim foo.txt | more command makes it even more difficult to exit Vim.
Wait for the message to disappear and enter the following commands.q " Quit the more command. :q! " Quit Vim. reset " Reset the shell. -
Note: On Windows, the vim foo.txt > nul command gives no warning at all.
4.3. Cut and Paste
Cut or copy lines without counting the lines
:set number " Turn on line numbers.
:set nu " Turn on line numbers.
:set nonumber " Turn off line numbers.
:set nonu " Turn off line numbers.
:set number! " Toggle line numbers.
:set nu! " Toggle line numbers.
:set relativenumber " Turn on relative line numbers.
:set rnu " Turn on relative line numbers.
:set norelativenumber " Turn off relative line numbers.
:set nornu " Turn off relative line numbers.
:set relativenumber! " Toggle relative line numbers.
:set rnu! " Toggle relative line numbers.
4.4. Debug Commands
:echo $MYVIMRC
:echo $VIMRUNTIME
:help system-vimrc
:highlight
:scriptnames
:set
:set runtimepath?
:verbose set formatoptions
4.5. Regular Expressions
.* Greedy match
.\{-} Non-greedy match
4.6. Tabs
:retab " Change all existing tab characters to match current tab settings.
:set expandtab " Insert space characters whenever tab key is pressed.
:set noexpandtab " Disable 'expandtab' option.
:set tabstop=4 " Insert 4 space characters when tab key is pressed.
4.7. Tags
Enter the following commands at a Command Line.
cd neo-com
ctags --list-languages
ctags -R src gen
vi src/com/shoklo/smru/neocom/NeoCom.java
:set tags=tags
:tag <function> " Jump to the function definition.
Place the cursor on the function name and press Ctrl+] to jump to its definition.
Press Ctrl+T to jump back to the original location.
:tags " View the tag stack.
:tag " Move to the next tag in the stack.
:pop " Move to the next tag in the stack.
:tselect " ???
:ptselect " ???
:tnext " ???
:tprev " ???
:q
vi -t <function>
:help tags
:q
4.8. Useful Commands
/[^\x00-\x7F] " Find non ascii characters.
:g/^.\{,260}$/d " Delete all lines shorter than 260 characters.
:n " Switch to next file.
Ctrl^ " Switch to previous file.
:e <file> " Open <file>.
:e! " Restore current file to last saved version.
:w !sudo tee % " Save read-only file.
:w >> <file> " Append current file to <file>.