Apidays New York 2024 - The value of a flexible API Management solution for O...
Â
The "vi" Text Editor
1. The â vi â Text Editor
Alessandro Manfredi Lug Roma 3 - http://www.lugroma3.org
2. ?
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
3. Roadmap
⢠Yes, Iâm going to convince you to use this tool
⢠Who, What, When, Where, Why
⢠How to
⢠Examples & Training
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
4. Once Upon a Time ...
The 5 âwâ
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
5. The editor war
⢠Modal Editor
⢠Small and Fast
⢠Textual env
⢠Extensive use of chords
⢠Bells & Whistles
⢠GUI
Both have a difďŹcult learning curve
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
6. And the winner is ...
POSIX ( IEEE 1003 â ISO/IEC 9945 )
SUS ( IEEE & The Open Group )
( that means there is no actual winner, you can use whatever you want
but since vi is part of the standard you should really know how to use it )
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
7. Wait, I never missed anyth using ...
⢠gedit
⢠kate / kwrite
⢠nano
⢠joe
⢠oowriter
⢠notepad
⢠... whatever
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
8. So why ? ( running into where )
⢠Again, IT IS PART OF THE STANDARD
⢠That means you ďŹnd it everywhere
including BSD*, Mac Os X, Solaris, HP-UX,
AIX, Windows with SFU, your microwave
oven (if itâs posix-compliant), etc.
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
9. Other strong reasons
(that you will probably ignore)
⢠Really powerful editor
⢠Complex tasks with few keystrokes
⢠Effective regex support
⢠Makes you damned fast
(especially with touchtyping)
⢠You can never raise hands from keyboard
⢠Someone says modal editing increase productivity
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
10. What (the jargon ďŹle says..)
vi: /V-I/, not, /vi:/, never, /siks/, n. [from âVisual
Interfaceâ] A screen editor crufted together by Bill Joy for
an early BSD release. Became the de facto standard Unix
editor and a nearly undisputed hacker favorite outside of
MIT until the rise of EMACS after about 1984.
Tends to frustrate new users no end, as it will neither take
commands while expecting input text nor vice versa [... ]
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
11. How the ADM3A Keyboard
looks like
⢠Esc was where now TAB is
⢠No arrows, just H , J , K , L
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
12. Clones
⢠Vim Any clone extends the basic âviâ
⢠Elvis program with addictional functions
but all of them share the common
⢠nvi mode and subset of commands you
⢠gvim are (hopefully) going to learn
⢠bvi hint: try $which vi
⢠calvin
⢠lemmy, elwin, winvi, ...
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
13. How to ...
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
14. Modal Editing
⢠Command Mode
⢠Cursor is on the text
⢠Canât see what youâre typing, just its effects
⢠Input Mode
⢠Text typing and ONLY text typing
⢠Directive Mode
⢠Entered with <:>
⢠You can see what you type this time =)
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
15. Switching Modes (1)
Command Mode
Input Mode : Directive Mode
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
16. Enter the editor
⢠vi
⢠vi ďŹlename
⢠vi ďŹlename1 ďŹlename2 ďŹlename3
⢠vi -r ďŹlename
⢠vi +cmd ďŹlename
⢠vi +lnum ďŹlename
⢠vi @rcďŹle
⢠...
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
17. Very Basic usage example
<a> Hello folks! <Esc> <:><w><q> <Enter>
( btw, â<x>â means â press the âxâ key )
$ cat ďŹlename
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
18. Switching Modes (2)
⢠Command Mode > Input Mode
⢠<a>, <A>,<i>,<I>, <o>,<O>, ...
⢠Input Mode > Command Mode
⢠<Esc>
⢠Command Mode > Directive Mode
⢠<:>
⢠Directive Mode > Command Mode
⢠clean the directive line â:â included
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
19. Some Directive Line Commands
:w âwriteâ (save)
:w ďŹlename save as ďŹlename
:q quitâ
:q! quitâ forced (forget changes)
:wq ( :x ) Combination of âwâ and âqâ
:e ďŹlename âeditâ ďŹlename
:r ďŹlename readâ ďŹlename (copy next)
:n open the ânextâ ďŹle
:rew ârewindâ ďŹle list
:ab token expression create a âtokenâ macro for exp
:unab token delete the âtokenâ macro
:n go to line n
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
20. Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
21. Moving Cursor
h, j, k,l left, down, up, right
G to the end of the document
{} beginning / end of the paragraph
() beginning / end of the sentence
$ to the end of the line
^ to the beginning of the line
n| to the n-th column
w beginning of the next word
e end of the next word
+ ďŹrst non-blank of next line
- ďŹrst non-blank of previous line
fx ďŹrst occurence of the âxâ char
n cmd repeat ânâ times âcmdâ
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
22. Editing Commands
i,a,I,A,o,O input mode editing
u undo the last change
U undo all the changes on the line
J join current line and the next
<< , >> move the line left or right
<L , >L move the following lineset l or r
y# yank the # range
p# paste the # range
d# delete the # range
yy, pp, dd yank, paste, delete one line
Commands can be combined, e.g. 6yy yanks 6 lines
Possible ranges # are w, ), }, fx, nj, $ as word, paragraph, sentence,
ďŹrst x occurrence, till the n-th line and till the end of the line.
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
23. Examples
( and Training )
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
24. The % and the â . â commands
â˘% , in command mode is the block range
⢠match things like vdDASFgvfd(sdf,rgae,##,adSDf)
⢠â . â , just repeat the last command
⢠consider that â <i> [type text] <Esc> â is still a command
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
25. â . â and the programmer day ...
⢠... that often starts with the need to implement some
functions from signature already in the headers
⢠â3yyâ and âpâ to copy something like
bool check(int a, char c);
int value(int strange);
int failure(int my_process);
⢠The ďŹrst task is replace semicolon with the blocks
<A><backspace> { <Enter> } <Esc> <j.j.>
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
26. Good way to modal editing
⢠One mode for each task support tasks separation
⢠Command mode should be the most used
⢠Insert text in input mode is a command too
⢠and should be used âtransactionallyâ
⢠e.g. reading should be achieved in command mode
⢠words replacement too! e.g. <cw>newword<Esc>
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
27. â % â and the programmer day ...
⢠... that goes on extracting function calls from
complex expressions and assigning âem to a variable
⢠something like
if (!my_st.enf && do_smtg( s.isopn(), s.isdmz) && ( a & b.c )){ ... }
⢠<fdc%> var <Esc><O> var = <Ctrl+R> ; <Esc>
⢠turns that into
var = do_smtg( s.isopn(), s.isdmz) ;
if (!my_st.enf && var && ( a & b.c )){ ... }
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
28. Keep the audience awake
or pull them to leave the room
⢠What do these commands do ?
3J , 3dl , 3iHi<Esc>u , yf6 , dfyp , cfyp , llcfxy<Esc>hp
⢠What is the difference between these commands?
â 4c( â and â 2c2( â
⢠How to ďŹnd the next 3 â x â char ?
⢠How to replace the next 3 sentences with â-censored-â
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
29. Search
fx ďŹnd the next occurrence of x
tx scroll to the next x
Fx ďŹnd the previous x
Tx scroll backwards to the prev x
/exp search forward for exp
?exp search backwards for exp
; repeat last search
, reverse the last search order
/ ,? repeat the last search
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
30. Regexp
⢠âA regular expression (regex or regexp for short) is
a special text string for describing a search patternâ
⢠Very common in programming languages
⢠Different syntaxes
⢠Match regular languages
⢠In short is a string made of chars, wildcards,
multipliers and escape sequences, that can match an
entire class of other strings.
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
31. Some regexp elements (in vi)
^ beginning of the line
$ end of the line
w alphanumeric chars
W non-alphanumeric chars
exp? exp, 0 or 1 occurrences
exp+ exp, 1 or more occurrences
exp* exp, 0 or more occurrences
exp|pxe exp or pxe
. ANY char
< beginning of a word
> end of a word
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
32. Search and replace
...with regexp from directive line
⢠: <range> s / <exp> / <replace> / <mod>
⢠Ranges: n, . , $, %, n,m
( for ân linesâ, âsingle lineâ, âtill the endâ, âwhole ďŹleâ,âfrom n to mâ )
⢠Mods: c, g, n
( for âask conďŹrmâ, global and âďŹrst n matchesâ )
⢠Search and execute command
⢠: <range> g / <exp> / <command>
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
33. Search Options
⢠:set (no)ic
⢠:set (no)hlsearch
⢠:set (no)incsearch
⢠:set (no)...
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
34. More Directive Line Commands
⢠:map key cmd
⢠:unmap key
⢠:[n,m]w ďŹlename
⢠:!shcmd
⢠:r shcmd
⢠:sh
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
35. Clones ConďŹguration (vim)
⢠VIM (:r $VIMRUNTIME/vimrc_example.vim)
⢠:e ~/.vimrc ( linux & some unix )
⢠:e $VIM/_vimrc ( windows )
⢠:e /usr/share/vim/vimrc ( Mac OS X )
⢠set ai (set AutoIndex)
⢠set ruler (show cursor position)
⢠syntax on (enable syntax hl)
⢠set nohlsearch (disable hlsearch)
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
36. Keep in mind that ...
⢠Be good at modal editing means
think complex commands ... fast!
⢠Learn with practice!
⢠Donât read these slides again and again! But ...
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
37. Whatâs next ?
⢠Use vimtutor !
⢠Build your own reference, at ďŹrst with few
commands, then improve that when you need it.
⢠Donât practice as an exercise, use the editor when
you need to edit texts.
⢠Donât be surprised if you will ďŹnd yourself
typing :wq even when not using vi
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
38. Q? /(& A)?/
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
39. How vi would seem if has been made by Microsoft
Credits to Mariganâs Blog - http://blogs.sun.com/marigan/entry/how_the_vi_editor_would
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org
40. Online References
⢠Wikipedia
http://en.wikipedia.org/wiki/vi
⢠The vi lovers homepage
http://thomer.com/vi/vi.html
⢠Why do those nutheads use vi?
http://www.viemu.com/a-why-vi-vim.htm
⢠Vi Reference Manual
http://drumlin.thehutt.org/vi/
⢠(Book) O'REILLY - Learning the vi Editor, Sixth Edition
http://www.oreilly.com/catalog/vi6/
Alessandro Manfredi The â vi â Text Editor Lug Roma 3 - http://www.lugroma3.org