######### ####### ####### #######
## ## ## ## # ## #
## ## ## ## ##
###### ## ## #### ####
## ## ## ### ###
## ## ## ## ##
## ## ## # ## # ##
## ####### ####### #######
~ Linux / FOSS Meetup Linz ~
~ Jan 2024 ~
CC BY-SA 4.0 Daniel KNITTL-FRANK
### ### #### # #
## ## ## ## ##
## ## ## #### ####
## ## ## ## ## ## ##
## ## ## ## ### ##
## ## ## ## # ##
### ## ## ##
# #### #### ####
https://en.wikipedia.org/wiki/Vim_(text_editor)
How to start vim? Type vim
How to exit Vim? Type :wq<Enter> to save and exit,
:q! to exit without save
(Pro-tip: ZZ and ZQ in normal mode)
Vi is like a Ferrari, if you're a beginner, it handles like a
*****, but once you get the hang of it, it's small, powerful and FAST! --- (Unknown)VIM is like a new model Ferrari, and sounds like one too - "VIIIIIIMMM!" --- (Stephen Riehm, Germany)
https://elias.rhi.hi.is/vim/quotes.html
insert, visual, etc.)<Esc> to exit any mode and return to normal modeVim comes with vimtutor binary which provides an "interactive" tutorial.
You should try it :]
^
k Hint: The h key is at the left and moves left.
< h l > The l key is at the right and moves right.
j The j key looks like a down arrow.
v
But you can almost always simply use the arrow keys to navigate
Shameless plug: … or you can learn Neo to avoid moving your hand to the arrow keys
To enter insert mode:
i: inserta: appendo: starts a new lineDon't panic and <ESC>
u: undo<C-R>: redo
(g-/g+ for walking through undo-tree)
<C-V>: block/column selectionV: line-based selecto: jump to start/end of selectiongv: re-select previous selectionx: delete character (cross out)dd: delete linecc: change lines: substitute character under cursorJ: Join linesy: copy (yank)d or x: cut (delete)p, P: paste after, Paste beforew: word forwardb: word backwarde: end of word), (: sentence forward, backward}, {: paragraph forward, backward%: matching item, e.g. () [] {}^: start of text0, $: start and end of line1G or 1gg: go to line 1Compose single keys to build powerful actions:
2.: repeat last action 2 times
yw: yank to next word
y3w: yank next 3 words
das: delete a sentence
ciw: change inner word
gUiw: Upper-case inner word
v enters visual mode (selecting text)
viw: inner wordvis: inner sentencevib: inner block, e.g. () []vi": inside double quotesvap: around paragraphva]: around bracketsvat: around tag/regex, ?regex: search regular expression forward, backward*, #: forward search for word under cursor, backward searchn, N: next match, previous match
fc, Fc: next/previous character 'c' (inclusive)
tc, Tc: next/previous character 'c' (exclusive);, ,: repeat forward, repeat backward
:%s/pattern/replace/g: substitute text file-wide
=: auto-indent text/source code>, <: indent, unindentgq: format text:center: center text:left: left align text:right: right aliign text<C-N>: autocomplete next<C-P>: autocomplete previous<C-X> <C-L>: autocomplete line<C-X> <C-F>: autocomplete file path<C-X> <C-N>: autocomplete with context<C-X> <C-O>: omni-completionAccess registers with "r
"ry3w: yank next 3 words into register "r"
"r2p: paste 2 times from register "r"Registers can be used to store and replay macros
qr: start recording into register "r"; press q when finished@r: execute macro from register "r"@@: re-run last macro@:: re-run last "ex" command
E.g. macro to capitalize first word in sentence, then jump to next sentence:
()guw~)
Pro-tip: Macros are simply text in registers, you can yank, edit, and paste them:
"rp and "ry
Need to filter current line through external command?
:.!cmd: filter current line through command
(Pro-tip: Type !!)
(Pro-tip: !!sh to execute the current line as shell command)
:[range][command]
Useful commands:
:make: execute Makefile:!cmd: run external command:%!cmd: filter and replace content by command:1,10w file: write first 10 lines to file:r!date: read current date and time into file:g/pattern/command: perform global command:v/pattern/command: perform inverted global command
:g/^$/d: delete empty lines:g/^#/norm A # add " #" after each Markdown header line``` set number " show line numbers
set ignorecase smartcase " ignore case if pattern lower case
set incsearch " incremental search set hlsearch " highlight all matches
" show non-printing characters (make sure your terminal supports UTF8!) set list listchars=tab:→ ,nbsp:␣,trail:•,space:⋅ ```
Can be persisted in ~/.vimrc. If you want to have a look at my settings,
check out https://github.com/knittl/.files/blob/master/.vimrc
~/.vim/plugin/https://github.com/junegunn/vim-plug
Installing:
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://github.com/junegunn/vim-plug/raw/master/plug.vim
Then add to your ~/.vimrc:
call plug#begin()
Plug 'git repository url'
call plug#end()
mattn/emmet-vim: HTML and CSS editing on steroidstpope/vim-surround: Clever way of changing surrounding elementstpope/vim-fugitive: Never leave Vim to do Git stuffplasticboy/vim-markdown: Better visual editing of markdown filesjunegunn/vim-easy-align: Align text and tablesman vim:help