Presentation is loading. Please wait.

Presentation is loading. Please wait.

vi basic introduction through advanced tips and "tricks"

Similar presentations


Presentation on theme: "vi basic introduction through advanced tips and "tricks""— Presentation transcript:

1 vi basic introduction through advanced tips and "tricks"
Presented by Michael Paoli, at: BALUG

2 vi – what vi is screen oriented text editor
modal editor (modes: command, "insert", ex) very powerful, rather flexible, yet efficient editor optimized for ease of use and speed by those proficient at it operates on buffer copy of file(s) – only writes out specified file(s) being edited when explicitly commanded to do so

3 vi – what vi is not: WYSIWYG editor font editor spell checker
grammar checker optimized for ease of learning it EMACS (perfectly good operating system which lacks a good text editor)

4 vi – some general features/capabilities
recovery mode ex command line mode negligible on-screen cruft – one mostly sees only what one is editing, and almost nothing else regular expressions works exceedingly well with numerous Linux/Unix commands (pipes) It's (so?) hard to learn(!?), it's so easy (and efficient!) to use! - yes, and yes!!!

5 vi's modes – command mode
initial default when invoked as vi or view character(s) typed act as command(s), rather than being themselves inserted into text Escape (^[) – used in vi (visual) mode to return to command mode. Not sure what mode one is in? Hit Escape a few times, vi will beep terminal bell (or flash visual "bell") when one hits Escape when already in command mode – at that point one is assuredly in command mode.

6 vi's modes – "insert" mode insert/append/replace, etc. modes – characters are literally added as text, with some slight exceptions some of those exceptions: Escape exits "insert" mode Unix/Linux erase (typically ^H or Delete) and kill (typically ^U) characters work as expected ^V - enter the following character literally regular printable ASCII characters and space and tab are not exceptions ^D – backtab over autoindent ^^D – kill autoindent just for current line a, A, i, I, o, O, R, etc. enter "insert" mode

7 vi's modes – ex command mode
from vi (visual) mode - : (colon) – one then types ex command(s), ending with Escape (or newline or carriage return) when invoked as ex, starts in ex command mode Q – quit visual mode, go to ex command mode (vi to return to visual mode)

8 vi – the basics Typical/basic invocation: vi file start adding text: a
You'll notice some patterns in the commands, e.g. lower and upper case, mnemonics (more-or-less), etc. At least by default, long lines wrap on display, but vi doesn't logically break/wrap/fold the lines themselves by default a – append after cursor A – append at end of line

9 vi – more basics I – insert before first non-blank on line
i – insert before cursor I – insert before first non-blank on line o – open line below O – open line above rx – replace single character with x R – replace characters (overtype) s – substitute characters S – substitute lines c<cursor motion> – change through motion C – change rest of line . - repeat last change u – undo last change (most types) "dp – put (retrieve) dth last delete U – Undo changes to current line

10 vi – moving around ^F – Forward screenfull ^B – Backward screenfull
^D – Down half screenfull ^U – Up half screenfull [num]G – Goto line number num (end default) ^H, h, j, k, l, <space> – one character left, left, down, up, right, right, respectively /re, ?re – search forward(/) or backward(?) for regular expression re n, N – next / or ? match (N to reverse direction)

11 vi – moving around w – forward word W – forward "big" Word
b – backward word B – Backward "big" word e – end of word E – End of "big" word /re/+n – nth line after regular expression re ?re?-n – nth line before regular expression re ]], [[ - next(]])/previos([[) section/function % - to matching nested () or {} (or <> or [])

12 vi – moving around `` - to previous context location
'' - like ``, but first non-whitespace on line mx – mark position with letter x `x – to position marked with letter x 'x – like `x, but first non-whitespace on line H, M, L – Home/Middle/Last window line + (or return or newline) – next line at first non-whitespace - - like +, but previous line ^ - first non-whitespace on line

13 vi - moving around 0 - (zero) – first character on line
$ - last character on line fx – forward onto character x within line tx – forward up to character x within line Fx, Tx – like fx and tx but backwards ; - repeat last f, F, t or T , - like ; but reverse direction n| - to column number n ), } - to next sentance()) / paragraph(}) (, { - like ) and } but backwards

14 vi - adjusting screen ^L – redraw screen (simple repaint, all versions) ^R - redraw screen (most versions, z<return>, z., z- - redraw screen with current line positioned at top(<return>)/middle(.)/bottom(-) /re/z- to regular expression re and positioned at bottom line ^E – expose line below (scroll up one line) ^Y – show line above (scroll down one line)

15 vi – delete, yank, put, etc. d<cursor motion> - delete through cursor motion into unnamed buffer x, X – delete character at(x) / to left of(X) cursor "xd – like d, but into named buffer x y<cursor motion> - yank through cursor motion into unnamed buffer, "xy – into buffer x dd, yy, Y – delete(dd)/yank(yy or Y) line D, d$ - delete through end of line [num]dd – delete [num] line(s) p, P – put contents of unnamed buffer after(p)/before(P) cursor or current line "xp, "xP – like p and P but for named buffer x

16 vi – more stuff ZZ – write out any changes and quit
[num]J – Join lines (default 2) !<cursor motion>command – take contents covered by cursor motion, pipe them to shell command and replace them with the output of that shell command, ! in command substitutes prior command ^Z – suspend vi or ex to background >>, << - shift right(>>)/left(<<) ~ - toggle upper/lower case

17 vi – ex stuff! : from command mode in vi start an ex command, which is then terminated by Escape, newline or return. In ex mode, the prompt is :, and that leading : isn't input as part of the command. Q – quit visual mode, go to ex command mode :vi – go to visual mode :wq – write and quit :x - write out any changes and exit :q – quit (warns if changes not written) :q! - quit (even if changes not written) :sh – shell :!cmd – run shell command cmd and return, ! in command substitutes prior command

18 vi – more ex stuff! :[range]d – delete line(s) (default current)
:[range]s/re/replacement/[cg] - on range line(s) (default current) replace matched regular expression re with replacement, optionally confirming(c) and/or globally(g) (all locations within line(s)) :g/re/command – globally (all lines) matching regular expression re do ex command command :$[+-relnum] :.[+-relnum], :num - goto last, current or line number num line, optionally +- relnum lines

19 vi – yet more ex stuff! :.= - what's my current line (number)?
:$= - what's my last line (number)? :f [file] – what's my file name (set it to file) :[pos]r [file] – read in file (default current) after after pos (default current) line :[range]w [file] – write range (default all) to file (default current) :n – next file :e [file(s)] – start editing file(s) (default current) :rew – rewind to the first file :f through :rew above may be "forced" by appending !, e.g.: :f!, … :rew! :arg – show my current file and file arguments

20 vi – yet more ex stuff! :w !command, :r !command – to/from shell command, not to be confused with forced(!) in most file and shell commands, % will substitute current file name, and # will substitute "alternate" file name, and ex will do shell metasyntax expansion of file names given :[range]mpos – move range (default current line) after line pos :[range]tpos – copy range (default current line) after line pos :[range]j – join lines (default .,.+1)

21 vi – yet more ex stuff! specifying line and line ranges:
number – line number /re/ - next line matching regular expression re 'x – line marked by x ^, $, ., etc. for specific line -, + - preceding(-) / next(+) line first,last – use any two of the above for first and last separated by , (comma) specifies that range (first can't occur after last) % - all lines (same as 1,$ or ^,$)

22 vi – handy ex options: use :se option to set, :se to show changed from default, :se all to show all showmode, showmatch, autoindent, wrapmargin=num, directory=, [no]ignorecase, [no]number, [no]list, [no]readonly, shiftwidth=num, tabstop=num, [no]wrapscan

23 vi - invocation view, vi -R - "read only" vi -r - recovery mode EXINIT
~/.exrc ex vi +/re/ start at regular expression re vi +[num] – start at line number num (default end)

24 vi – tips, tricks, fast 'n cool stuff
deep – exchange two words read in from / write out too / filter through command (:w !, :r !, !, etc.) use for scratch computations/analysis avoid accidental overwrites – don't let vi know a filename % and # in ex command :n, :rew, :e spell checks, comparing, (un)expanding tabs nvi/vim: leftright/nowrap quick ruler

25 resources, references, etc.
quick reference card, e.g.: An Introduction to Display Editing with Vi,William Joy, Mark Horton, various sources, e.g.: manual pages: vi(1), ex(1), nvi(1), vim(1), etc. The vi Lovers Home Page: Learning the vi and Vim Editors:


Download ppt "vi basic introduction through advanced tips and "tricks""

Similar presentations


Ads by Google