Flying with VIM
vimtutor
:help
:help <command>
:helpgrep text
https://vim-adventures.com
https://en.wikipedia.org/wiki/Vim_(text_editor)/
https://en.wikipedia.org/wiki/Vi
Modal text editor
- Visual mode
- Insert mode
- Commandline mode
Motion controls - ggVG?
gg - go to first line
V - Select Current line and dont exit select
G - Go to last line
ggVG == Select all == Ctrl+a (in modern applications)
Original Keyboard layout used in ADM-3A terminal
Vim run commands
Opening more than one file
In vertical split:
gvim -O file1 file2
In horizontal split:
gvim -o file1 file2
Buffers
:b
:ls
Add all files in a folder to buffer
gvim *
Diff
:windo diffthis
gvimdiff file1 file2
Visual Mode
Indenting
Shift+>>
Shift+<<
Auto Complete
Ctrl+n - go to next match
Ctrl+p - go to previous match
Go to line number
:8
:<line_number>
Print line number
:|
:8|
Searching
/
n - next
N - previous
Cursor Movement
Arrow keys - <>^v
Traditional - hjkl
h - Left
j - Down
k - Up
l - Right
Change cases
~
U
u
Search current word under cursor
*
n - Find Next
N - Find Previous
Repeat last edit
.
Repeat last command
&
Undo
u
Redo
Ctrl+r
Select
v; ve; vb; vw; V
Vertical Select
Ctrl+v
Copy / Yank
y; yy; yw; yb; ye; Y
Cut
x
Paste
p; Shift+p
Delete
d; dw; dd; d+End
D - Delete from current cursor potision to end of line
C - Delete from current cursor potision to end of line and start insert
Go to file under cursor
gf
Marking / Bookmarking
To set:
m[a-z0-9]
m[A-Z0-9]
To go to the mark:
`[a-z0-9]
`[A-Z0-9]
Go back to previous file
Ctrl+^
Recording
To record:
qa
“do things”
q
Scrolling horizontally
ze
zs
z<Left> ; zh
z<Right> ; zl
Insert / Edit / Replace mode
Enter Insert mode
i - insert
a - append
o - new line below
O - new line above
r - replace
R - replace and dont exit
Vertical Edit mode
Ctrl+v+Shift+i+text_to_write+Esc
Command line mode
Veritical Split
:vsp
:verticalsplit
Ctrl+w+v
Horizontal Split
:sp
:split
Ctrl+w+s
Sorting
:sort
Enable mouse
:set mouse=a
Run shell commands inside vim
:!
:sh
Remap visual mode keys
a->b
b->a
:nnoremap ; :
:nnoremap : ;
Color Column
set colorcolumn=81
Save file
Write and save changes to current file:
:w
Write and save changes to into new file:
:sav new_file_name.txt
No Fold / wrap text
:set nowrap
:set wrap
File explorer
:E
Open file
:e full_path_to_file.ext
Open file Buffer
:b file_name.*
:b<number> i.e :b1 |
Open Buffer list
:ls
Command line mode as Insert and Visual mode
Ctrl+f
To replay recording:
@a
@"register
~/.vimrc startup customizations
Commenting in ~/.vimrc
"
Enable backspace
set backspace=indent,eol,start
Change color scheme
colorscheme desert
colo desert
Change background color
set background=dark
set background=light
Use spaces instead of tabs
set expandtab
set tabstop=2
set shiftwidth=2
Enable line numbers
set number
set nu
set relativenumber
set rnu
Search case insensitive
set ignorecase
set smartcase
Use characters to display white spaces
set listchars=nbsp:_,tab:>-,trail:~,extends:>,precedes:<
set list
Change font
set gfn=Cascadia_Code:h14
Old syntax:
set gfn=Cascadia_Code\ 14
Line Space
set lsp=4
Syntax Mapping
autocmd BufNewFile,BufRead *.aliases set syntax=csh
autocmd BufNewFile,BufRead *.sv set syntax=verilog
autocmd BufNewFile,BufRead *.json set syntax=javascript
autocmd BufNewFile,BufRead *.tl set nowrap
autocmd BufNewFile,BufRead *.md colo slate
Wrapping text
set wrap
set nowrap
Display full file path in status bar
set statusline+=%F
Remember buffers
exec ‘set viminfo=%,’ . &viminfo
Cursor Lines
set cursorline
set cursorcolumn
Highlight search
set hlsearch
set hls
Show line numbers in status bar
set ruler
Change color palette
if $COLORTERM == 'gnome-terminal'
set t_Co=256
endif
Limit max columns to break
set columns=100
Virtual edit
set ve=all
Linux Style Tab expand in explorer
set wildmode=longest,list,full
set wildmenu
Mini Commandline calculator
command! -nargs=+ Calc :py print <args>
py from math import *
Code folding
set foldmethod=indent
or
set foldmethod=syntax
Storing text and commands in registers
Vim Registers:
0-9
a-z
A-Z
To print stored content:
In Virtual mode:
@register
In Insert mode:
Ctrl+r+register
Live search and Regex matching
set incsearch
" Testlist fold and open
let @z = ":s/ -p/\\r-p/g\n" "Open testlist
let @a = ":s/\\n/ /g\n" "Close testlist
" Transpose
let @x = ":s/\\(.\\)/\\1\\r/g\n" "Horizontal characters to vertical
let @s = ":s/\\n//g\n" "Vertical characters to horizontal
" Setups/Comma fold and open
let @c = ":s/,/,\\r/g\n"
let @d = ":s/,\\n/,/g\n"
" Verilog IF-ELSE
let @f = "iif () begin\nend\nelse begin\nend"
let @u = "i`uvm_info(\"ENV_NAME\", $psprintf(\"NONE\"), UVM_NONE);"
let @i = "i$display(\"None\");"
Find and replace
Replace Once:
:s/text_to_replace/replacement_text/
Replace all occurance:
:s/text_to_replace/replacement_text/g
Ask before replace:
:s/text_to_replace/replacement_text/gc
Count number of text occurance:
:s/text_to_count//gn
Replace all occurance in the file:
:%s/text_to_replace/replacement_text/g
Regular Expressions
Replacing 123 in the string with 456
text_to_replace_xyz_123_find_this
:s/\(.*\)_[0-9]\+_\(.*\)/\1_456_\2/gc
Virtual Editing Mode
To enable:
set ve=all
set virtualedit=all
To revert:
set ve=
Editing binary files
Convert to Hex:
:%!xxd
Convert back to Bin:
:%!xxd -r
Things not covered but interesting
Vim Scripting
Vim Plugins
Vim Functions
Vim Custom Syntax Highlighting
Other off-topic things