Presentation on vi

From UCLUG
Jump to: navigation, search


This is the Presentation on vi given by Jās Eckard on March 9, 2010. This meeting was video recorded, and the video recording can be found at http://www.archive.org/details/UCLUG_March_2010 .

The text files launched in vi during this presentation were:

  1. this page in plaintext (copy and paste just the text of this page)
  2. the wiki markup of this page (click edit at the top and either copy and paste the textbox or use Firefox Add-On "It's All Text" and launch in gvim)
  3. the Constitution of the United States of America (Save Link As...)

What is vi?[edit]

  • text editor
  • word processor?
  • IDE?
  • modal editor:
    • edit (normal) mode
    • insert mode
    • command mode
    • more

History[edit]

  • Bill Joy @ Sun
  • "visual" display of ex
  • now, "class" of editors: nvi, elvis, vile, vim, original vi

Why?[edit]

  • "on every un*x"
  • "old-school"
  • commands: keyboard: hand doesn't have to leave
  • not easy to learn, but powerful, most vi clones are programmable

If you learn nothing else[edit]

  • <ESC>:q!
  • mnemonic:
Before yourself you're about to hang,
escape colon que bang!

First[edit]

  • vi
  • vi file_that_exists
  • vi file_that_exists_not
  • view read-only_file
hjkl 
left/down/up/right movement → bsdgames (robots, worm)
i 
insert mode
<ESC> 
edit mode
: 
command ("colon") mode

Second most important[edit]

u 
undo
<CTRL>-r 
redo (vim)

Motion/Movement[edit]

hjkl 
left/down/up/right
<SP><BS> 
right/left
arrowkeys 
vim
wW 
forward by word (character clump/whitespace)
bB 
back by word (ditto)
eE 
end of word (ditto)
^$ 
begin/end of line
|0 
column number (zero)
fF 
find character (right/left)
tT 
find 'til (right/left)
;, 
repeat fFtT (forward/reverse)

Multipliers[edit]

  • almost all commands

Insert Mode[edit]

  • Don't start out in: more often, altering existing file rather than creating new
iI 
insert (at cursor/begin of line non-whitespace)
aA 
insert after (cursor/end of line)
oO 
create an open line (after/before current line)
r 
replace character
R 
replace "mode"
cC 
change (motion/to end of line)
sS 
substitute (character/line)
<INS> 
insert/replace mode

Find[edit]

/? 
find regex (down/up)
nN 
next (forward/backward)
*# 
find word under cursor

Search/Replace[edit]

:s 
substitute
cgi 
confirm/global/ignore case
& 
entire pattern match
\1\2\3... 
first, second, third, etc. subpattern match
& 
(edit mode)repeat :s

Editor of the Beast?[edit]

At this point in the talk, I figured it would get boring, so I added a topical joke. Watch the video for the punchline. -- chruck (Jās)

Command mode[edit]

:e 
edit file
:q 
quit
:w 
write
:q! 
force quit
:wq 
write and quit
:r 
read file, append
:! 
execute command
:r! 
read command, append output
:p 
print
:g 
global (grep)
. 
current line
0 
zeroth line
$ 
last line
% 
all lines
:<number> 
goto line number
  • other ex (ed/sed) commands

Delete[edit]

dD 
delete (motion/end of line)
xX 
cross-out character(s) (at/before cursor)
<CTRL>-w 
delete word (insert mode)

Map/Abbreviate[edit]

:map 
map (combination of) keys to (combination of) keys ("macro")
:ab 
abbreviate

Cut buffer[edit]

yY 
yank (motion/line) ("copy")
pP 
put (after/before) ("paste")

Marks[edit]

m 
mark (letter/capital letter)
'` 
jump to mark (line/character)

Miscellaneous[edit]

. 
repeat last action
ZZ 
write & close
~ 
change case
= 
equal command
-<CR> 
begin of (prev/next) line
HML 
high/medium/low (line after/before)
Ggg 
go to line number/last line/first line
<CTRL>-g 
file status
{} 
paragraph (prev/next)
[[ ]] [] ][ 
block (prev/next / beginning/end)
() 
sentence (prev/next)
<> 
indent motion (decrease/increase)
! 
filter movement thru program
J 
join
<CTRL>-v 
(insert mode) insert <CTRL>- characters
K 
keyword

Vim-specific[edit]

  • :set
  • editing beyond insert point
  • :help
  • vimdiff
  • folding - zo/zc
  • gvim - GUI vim
  • <CTRL>-n <CTRL>-p - matching word (next/previous)
  • omni-completion
  • plugins
  • syntax highlighting
  • <CTRL>-^ - alternate file
  • :bufdo - do to all files in file buffers
  • window splitting - :split :vsplit <CTRL>-w
  • :ls
  • :make :cw
  • equal command
  • visual mode - v
  • <CTRL>-] <CTRL>-t - goto tag (go back)
  • syntax formatting/indenting
  • spell checking: :se spell
  • directory listing
  • remote file editing
  • vimtutor
  • vim.org

Reference cards[edit]

bash[edit]

  • set -o vi

Firefox[edit]

Specific Questions[edit]

  • Language specific settings (setting tab to four spaces automagically when opening any Python file)
    • modeline
    • .vimrc/.exrc
    • :se filetype / ft=python
    • :se tabstop / ts=4
    • :se shiftwidth / sw=4
    • :se expandtab / et
    • python syntax files
  • What is the difference between vi and vim? I know they're actually pretty stark to a newbie. I at least know how to do very very crude editing in vim, but couldn't even get into insert mode in vi.
    • vim: Vi IMproved
    • for the most part, vi commands used in vim
    • :help vi_diff.txt
  • Search/Replace across multiple files ... perhaps even to files matching a certain pattern only (*.php ... or *template*php ... etc)
    • vi *.php
    • :bufdo %g/pattern/s/from/to/gc
  • Auto-completion
    • <CTRL>-n <CTRL>-p, omnifunc
  • Auto-tabbing
    • :se autoindent / ai
    • :se smartindent / si
    • :se cindent / cin

Jas' .vimrc file[edit]

As promised, here is my ~/.vimrc file. It should be documented pretty well, but if you have any questions, type :he topic or ask me. -- chruck (Jās)

" Stuff that I like in vim  --Jas
" Created by .bashrc.jas JAS_ROX=2009.12.14

" set number

set autoindent
set autowrite
set background=dark
set backspace=indent,eol,start
set backup
set cmdheight=2
set expandtab
"if has("foldenable")
    set foldenable
    set foldmethod=syntax
"endif
set history=100
set hlsearch
set ignorecase
set smartcase
set incsearch
set joinspaces
set modeline
set nocompatible
set pastetoggle=<F2>
set printoptions=paper:letter,duplex:off,formfeed:y  " <CTRL-L> for <FF>
set ruler
set shiftround
set shiftwidth=4
set showcmd
set showmatch
set tabstop=4
set textwidth=70
set viminfo='20,\"50
 
syntax on
filetype plugin on
filetype indent on

set formatoptions+=tn
set formatoptions-=l

" Suffixes that get lower priority when doing tab completion for filenames.
" These are files we are not likely to want to edit or read.
set suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out

" Add tags file for headers in /usr/include .  This should be
" generated by .bashrc.jas .
set tags+=~/.vim/systags

" shortcuts I use:
" remap 'gf' to create the file under the cursor if it doesn't exist:
map gf :e <cfile><CR>
" Format current line using 'fmt':
map ff !!fmt<CR>
" Format the comments on the current line using 'fmt':
map ff/ !!fmt<CR>jI// <ESC>
map ff# !!fmt<CR>jI# <ESC>
" Join strings from line below: ,J for strings, ;J for comments
map ,J JxxX
map ;J Jwdw
" Format current xml line to be indented:
map xml !!XMLLINT_INDENT="    " xmllint --format -<CR>dd
" *******'s lprintf() looks terrible after it has been jasindent'd:
map lp JffA "<ESC>jI           "<ESC>Jff

if has("autocmd") 
    if exists("+omnifunc")
        autocmd Filetype *
            \   if &omnifunc == "" |
            \           setlocal omnifunc=syntaxcomplete#Complete |
            \   endif
    endif
    " To use syntax highlighting with Wikipedia
    " (see http://en.wikipedia.org/wiki/Wikipedia:Text_editor_support#Vim )
    au BufRead,BufNewFile *.wiki setfiletype Wikipedia

    " When creating new files, base them on the following template
    " files:

    "au BufNewFile *.c so ~/.vim/templates/src.c
    "au BufNewFile *.cpp so ~/.vim/templates/src.c
    au BufNewFile *.c{,pp} so ~/.vim/templates/src.c
    "au BufNewFile *.h so ~/.vim/templates/src.h
    "au BufNewFile *.hpp so ~/.vim/templates/src.h
    au BufNewFile *.h{,pp} so ~/.vim/templates/src.h
    au BufNewFile *.[ch]{,pp} exe "%s/FILE/" . expand("%")
    au BufNewFile *.h{,pp} exe "%s/FILE_/" . tr( expand("%"), "a-z.", "A-Z_")
    au BufWritePre,FileWritePre *.[ch]{,pp} exe "normal ma"
    au BufWritePost,FileWritePost *.[ch]{,pp} exe "normal 'a"
endif