BOOST YOUR PERFORMANCE WITH VIM

SUMMARY

 

introduction

  1. What is Vim?
  2. Why Vim?

basics

  1. Vim modes
  2. Editing command

advanced

  1. Multiple files
  2. Split screens
  3. Folding
  4. Macros

extras

  1. vimdiff
  2. vimrc
  3. modeline
introduction
What is Vim?

What is Vim?

text editor

/vɪm/

a contraction of Vi IMproved

first release in 1991

Why Vim?

Why Vim?

efficiency

highly configurable

ubiquity

scriptable

install Vim

Install Vim

Debian/Ubuntu


						apt-get install vim
					

CentOS/Fedora


						yum install vim
					

Mac


						brew install macvim --override-system-vim
					

Windows (?)


						ftp://ftp.vim.org/pub/vim/pc/gvim81.exe
					
Exiting Vim
:q! <ENTER>
basics
basics > vimtutor
vimtutor
basics > vim modes

Vim modes

Normal: navigate the scructure of the file.

Insert: edit the file.

Visual: highlight portions of the file to manipulate at once.

Command: command line mode, searches, …

basics
DON'T USE THE MOUSE
basics
PRO'S ALSO
DON'T USE ARROWS
basics > motion

Motion (1/2)

key action
h left
j down
k up
l right
easter egg found!

easter egg

title url
VIM adventures https://vim-adventures.com/
Interactive Vim tutorial http://openvim.com/
basics > motion

Motion (2/2)

key action
^e scroll down a line
^y scroll up a line
^f scroll down a page
^b scroll up a page
H move cursor to the top of the window
M move cursor to the middle of the window
L move cursor to the bottom of the window
gg go to the top of file
G go to the bottom of file
basics > Structure of an Editing Command
<number><command><text object or motion>
basics > text objects

Common text objects

key meaning
w word
s sentence
p paragraph
t tag
basics > commands

Commands

key action
d delete
c change
y yank
v visual select
basics > example
diw
delete in word
basics > example
caw
change all word
basics > example
yi)
yank inside ()
basics > DRY
DOT
command
my vim habits
my vim habits

my vim habits

key action
:set number show line numbers
:<number> go to line number <number>
<C-p> auto complete word
/text find text on the document
:nohlsearch stop the highlighting for the 'hlsearch' option
<C-z> send vim to background
fg send vim to foreground again
<C-a> increment the next number
<C-x> decrement the next number
zz scrolls to the middle
advanced
advanced > multiple files

Multiple files

command action
:ls list of open buffers
:bp move to previous buffer
:bn move to next buffer
:b<n> move to <n>'th buffer
:b <filename-part> move to buffer matchin name with <filename-part>
:bw <n> wipe current or <n>'th buffer
<n><C-6> edit alternate file
advanced > split screens

Split screens

command action
:sp(lit) <filename> split window and load <filename>
:vs(plit) <filename> vertical split window and load <filename>
:only keep only this window open
<C-w> <direction> move cursor to window placed at <direction>
<number> <C-w> + increase window size by <number> lines
<number> <C-w> − decrease window size by <number> lines
<C-w> > increase window size by <number> columns
<C-w> < decrease window size by <number> columns
<C-w> _ set the current window to maximum height
<C-w> | set the current window to maximum width
<C-w> = set all windows equal size
advanced > split screens

Useful commands

Open [n] files in horizontal splits


							vim -o[n] *.php
						

Open [n] files in vertical splits


							vim -O[n] *.php
						

Make a session to remember windows placement


							:mksession ~/.vim/sessions/example.vim
						

Restore vim saved session


							vim -S ~/.vim/sessions/example.vim
						

							:source ~/.vim/sessions/example.vim
						

advanced > folding

Folding methods

Manual: manually define folds.

Indent: more indent means a higher fold level.

Syntax: folds defined by syntax highlighting.

Expr, diff, marker, …

advanced > folding

Let's try folding

Download

 ~ / Zinio / app / core / base / RESTService.js

After open the file set this Vim configuration


					 :set foldmethod=indent
					 :set foldlevel=1
					
advanced > folding

Folding commands

key action
zj moves the cursor to the next fold
zk moves the cursor to the previous fold
zo opens a fold at the cursor
zc closes a fold under cursor
za toggle a fold at the cursor
zR decreases the foldlevel to zero (all folds will be open)
zM closes all open folds
zr decreases the foldlevel by one
zm increases the foldlevel by one
advanced > folding

More about folding


						:help fold-methods
						:help folding
						:help folds
					
RTFM
advanced > macros

Macros

Sequence of commands recorded to a register

Record a macro   Play a macro

q<register>

(do the things)

q

 

@<register>

extras
extras > vimdiff

Vimdiff

Download

 ~ / Zinio / app / core / base / RESTService2.js

extras > vimdiff

Vimdiff

command action
dp diffput: puts changes under the cursor into the other file
do diffobtain: The change under the cursor is replaced by the content of the other file
]c jump to the next diff
[c jump to the previous diff
extras > vimrc

~/.vimrc

use and share your .vimrc!


colo desert
syntax on
set number
set showcmd 
set ruler
set showmode
set hlsearch
set incsearch
set autoindent
set cindent 
set ignorecase
set nocompatible
set backspace=2
set ts=4
set shiftwidth=4
set nowrap
set showmatch
"set completeopt=menu,preview
set completeopt=preview

cab Q q
cab W w
					
extras > modeline

Modeline

Is a way to define vim settings in the source file

example from vim help:


					   vi:noai:sw=3 ts=6 
					

example from any apache configuration file:


					   # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
					
bibliography
bibliography & more

Last command learnt


						'.
					

Regarding vim marks: http://vim.wikia.com/wiki/Using_marks

Questions?

Thank you!