https://vimvalley.com/ https://vim-adventures.com/ https://www.vimgolf.com/
1# HCL
2mkdir -p ~/.vim/pack/jvirtanen/start
3cd ~/.vim/pack/jvirtanen/start
4git clone https://github.com/jvirtanen/vim-hcl.git
5
6# Justfile
7mkdir -p ~/.vim/pack/vendor/start
8cd ~/.vim/pack/vendor/start
9git clone https://github.com/NoahTheDuke/vim-just.git
trigger a vim tutorial vimtutor
the most powerful commands:. : Repeat the last modification.* : Where the cursor is located, keeps the word in memory and goes to the next occurrence..* : together, repeat an action on the next word.
Encrypt a file with VIM, pasted from sources
1:setlocal cm=blowfish2
2:X
3>Enter the encryption key:
4:wq
:ab ue University of Economics - creates an alias ue, then in insert mode every time you type ue + Enter it will write University of Economics for you.
:unab ue - removes the alias ue
:ab rue Mr Smith main street - ⚠️ Be careful of the common mistake where the alias is contained in the alias.vim -u test_vimrc : when you want to test a vimrc first
1# Active coloring and presentation
2syntax on
3:colorscheme torte
4# OR :colorscheme elflord
5set bg=dark
6set autoindent
7
8# YAML tabulations
9autocmd FileType yaml setlocal ai ts=2 sw=2 et
10
11# extension .md to be recognized.
12filetype on
13au BufNewFile,BufRead *.{md,mdown,mkd,mkdn,markdown,mdwn} set filetype=markdown
14
15# set not compatible mode to enable Vim features.
16set nocp
1:so $MYVIMRC | :source ~/.vimrc
1:set [ nu | number ]
2:set [ nu! | nonu | nonumber ]
3:set printoptions=number:y
ls -l /usr/share/vim/vim*/colors/1:colorscheme torte
1:set runtimepath ?
2runtimepath=~/.vim,[...]/usr/share/vim/vimfiles/after,~/.vim/after
~ to toggle case.~ Toggle case of the character under the cursor, or all visually-selected characters.3~ Toggle case of the next three characters.g~3w Toggle case of the next three words.g~iw Toggle case of the current word (inner word – cursor anywhere in word).g~$ Toggle case of all characters to end of line.g~~ Toggle case of the current line (same as V~).
Shift + v , then u to convert to lowercase, or U to convert to uppercase.U : Uppercase the visually-selected text.
gUU : Change the current line to uppercase (same as VU).guu : Change the current line to lowercase (same as Vu).gUiw : Change current word to uppercase.
u will undo changevi +33 file - opens the file at line 33vi + file - opens the file at the last linevi +/research - opens the file at the first line where “research” is foundvi -R file - opens read-onlyvi -r file - after a crash, recovers the swap filevi file1 file2 - to open two documents (access the next one with :n)
:vs ~/.vimrc - open in another window:sh ~/script.sh - run a script
:e [file] - edit another file without leaving vi (the benefit is keeping what is in the buffer and navigating between several documents):n - navigate between the files open in vi (also ctrl ^, but it depends on the system):x - closes all the documents opened with vi:sp - opens a second document on the same screen
navigate between the two withctrl+w w - to switchctrl+w j - to go downk - to go up_ - to make the buffer take the whole screen= - to make the buffers equal again
:sf - opens another document in the same window:read pliku - imports everything from “pliku” at the cursor position (:r same effect):r /file - inserts a file from the cursor position:r! command - inserts the result of a command
Ctrl G - to see the file information, whether it is modified, etc.
Esc + q: - gives the command historyEsc + q\ - gives the search history
ZZ - quits and saves (shortcut):w - saves (w [file] saves under the given name):q! - quits:e! - discards all modifications
Manipulate text:n,mm j - moves from line “n” to “m” to line “j” (with /expression instead of “j”, moves after the line containing “expression”):n,mt j - copies from line “n” to “m” to line “j”:n,mw file - copies lines “n” to “m” into file:n,mw>> file - copies “n” to “m” at the end of file:'a,'bw file - copies what is in buffers a and b into file
SHELL COMMAND:! cmd - runs shell commands while in vi
e.g. :!df, :!ls -l, or :!cat /etc/passwd > ~/hasla.txt
Note: :! is glued to the command that follows.
INSERT MODE
to enter insert modei - write where the cursor isI - insert at the beginning of the lineA - insert at the end of the linea - write just after the cursorC - cuts from the cursor to the end of the line, then enters inserto - opens a new line below the cursorO - opens a new line above the cursors - deletes under the cursor and enters insert modeS - deletes the whole line and enters insert moder - enters “replace” mode for a single characterR - enters “replace” mode (note: if you erase, you get the old characters back)
NOTE: R in replace mode, if you erase, you get the old characters back.
COMMAND MODE
gg - to the beginning of the fileG - to the end of the fileL - to the end of the screen / M - to the middle / H - to the top of the screenZ + Enter - puts the cursor at the top of the screen / Z.nL - to n lines before the end of the screen / nH - to n lines from the top of the screennG - to line number n / 1G - to the very beginning / G - to the very end+ - to the beginning of the next line- - to the beginning of the previous line$ - sends the cursor to the end of the line( - to the beginning of the sentence) - to the end of the sentence0 - sends the cursor to the beginning of the line (or also ^)w - to the next wordb - to the previous word (2b: 2 words back)e - to the end of the word / E - also, but does not account for accents.ctrl+f - next screenctrl+b - previous screenctrl+d - half a screen downctrl+u - half a screen upmx - sets a (invisible) mark that can be found with `x (the cursor is sent back to that mark)Shift+v - puts a cursor on the entire line, select the number of lines with the arrow then “d” or “y” etc.d`x - erases from the mark to the cursory`x - copies from the mark to the cursor
x - deletes under the character
dd - deletes the line (d$ same effect but from the cursor)
Note: combines with numbers and directions. number + d (or x) + direction (h,j,k,l,$,0,w,b etc.)ddp - deletes a line and re-inserts it belowdd3p - pastes the same line three times5dx - erases 5 characters3dw - erases 3 words3dd - erases 3 linesdG - erases from the cursor to the end; :.,$d deletes everything from the beginning to the end
n,md - erases from line n to m
Note: any deletion is kept in the buffer for pasting.
D - deletes the line from the cursor
cw - erases the word and enters insert mode
cc - changes the line (erases the line and puts it in the buffer - p to paste later)
ciW - change inside Word
ci" - change inside quotes ""
c - combines with the movement cursor (e.g. c$, c0, c2b, etc.)
J - joins the next line with this one~ - swaps lowercase to uppercase and vice versau - returns to the previous modification (undo)U - returns over all modificationsCtrl + r - redop - pastes after the cursorP - pastes before the cursory - copy
yy - copies the lineY - same effect
y$ - copies from the cursor to the endyw - copies the word; 3yy - copies 3 lines;" - lets you save into a buffer (e.g. “a3yy copies 3 lines into buffer a, re-paste with “ap)
search
/word - searches for the string “word” after the cursor?word - searches for the string “word” before the cursor* - where the cursor is, searches for the same word
Note: n to go to the next, N to go back
COMMAND-LINE MODE (:)
Command-line mode always starts with :. Some commands can only be run in command-line mode.
(e.g. :3,7d) because they need to be visualized. There is also a history; you can search previous commands with the arrow.
Modes and Options:set - gives the modes in use (those launched by EXINIT and .exrc):set showmode - see the mode you are in.:set all - shows all possible modes to choose from:set OPTION? - shows whether the option is enabled or not:set nu - displays the line numbers:set nonu - removes this option (general rule: :set no+mode removes the chosen mode):set ignorecase - ignores uppercase:set magic - enables meta-characters:set list - shows the end of lines with a $ and the tabulations with ^|:set wrapmargin=n (wm=n) - moves the left margin by value n:set autowrite (aw) - saves automatically before a search, a check, a shell, etc.:set autoindent (ai) - provides automatic indentation when writing.:set showmatch (sm) - shows the matches between () or {} and [].
Note: you can save all the vi configuration into a file and call it with :so file_name, which loads that configuration.
Note: in /etc/virc/.exrc you configure the machine’s vi to launch options as soon as vi starts.
Otherwise create a .exrc file in the user directory.
substitutions (the sed tool inside vi):s/stare/nowe/ - searches for the string “stare” and changes the first occurrence in the sentence to “nowe”:s/stare/nowe/g - g to change all occurrences in the sentence:%s/stare/nowe/g - % to search through the whole text:n,ms/stare/nowe/g - searches between n and m:s/stare/nowe/gc - c asks for confirmation at each change:g/wzorzec/s/stare/nowe/g - g at the start searches for “wzorzec” and only applies the changes in the sentences containing “wzorzec”. Note: with g at the start, % is no longer necessary.:m - moves the selected expression
Sed tip
:s - repeats the last global modification:%&g - repeats the last global modification on the whole text:%s;/home/student;/home/toor;g - “;” replaces “/”, so “/” becomes a normal character.:g!/ok/s/$/TODO/g - wherever there is no “ok” in the sentence, put “TODO” at the end:%s/[0-9]$//gc - deletes if there is a digit (between 0 and 9) at the end of the sentence:g/^[0-9]/m$ - moves all lines starting with a digit to the end of the document
The meta-characters for vi’s sed. - equivalent to a character (careful: space counts as a character)* - any string^ - searches at the beginning of the line$ - searches at the end of the line\<word - searches at the beginning of the word (e.g. “moteur” will be taken)word\> - searches at the end of the word\ - cancels the meta-character and counts it as a normal character[ab] - a or b\(word_A\) - saves this word into buffer 1 (up to 9), retrieved with \1:%s/\(kolwalski\) \(Jan\)/\2 \1/ - swaps the two names.
Can serve to replace up to 9 expressions, or to place text between two expressions, etc.