Upload
minsuk-lee
View
306
Download
2
Tags:
Embed Size (px)
DESCRIPTION
using vi part of Linux (ubuntu) lecture.
Citation preview
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
Using vi editor
Minsuk LeeHansung University, Seoul, Korea
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
Why vi ?
• vi: Terminal based text editor– vi doesn’t need mouse, Function/Arrow keys
• Just regular keyboards• High speed editing
– vi works with other source code tools• ctag, …
• The original code for vi was written by Bill Joy in 1976, as the visual mode for a line editor called ex that Joy had written with Chuck Haley. [wikipedia ‘vi’].
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
• Modes of vi– Command Mode
• Cursor movement, delete, …– For cursor movement, You can also use arrow, [HOME],[END],[PgUp],[PgDn] keys
– Input Mode• Edit (typing the document)
– Ex Mode• Line command (write, cancel, quit, help, and ‘ex’ command)
Vi as a modal editor
InputMode
Com-mandMode
ex Mode
$ vi file
‘i’, ‘a’
[ESC]
‘:’
[ESC][ENTER]
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
viMenu for
Gnome-Terminalnot for vi
vi screen
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
1. $ vi filename2. You are in [Command Mode]3. EDITTING YOUR TEXT
– Type ‘i’ to change into [Input Mode]– EDIT [Type whatever you want]– Type ESC to return [Command Mode]– Move cursors using arrow keys or ‘h’, ‘k’, ‘k’, ‘l’, ‘w’,
‘b’– Delete a character using ‘x’
4. If done, ‘ZZ’ or ‘:wq’ to save and quit, or ‘:q!’ to just quit
5. $ cat filename
Survival guide of vi
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
• It’s the result of unwanted stop. (e.g., killed)
If you see this screen
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
Recover message
Found a swap file by the name ".myfile.swp" owned by: myuser dated: Sun Aug 28 11:12:41 2011 modified: noWhile opening file "myfile“ dated: Sun Aug 28 11:46:35
2011 NEWER than swap file!(1) Another program may be editing the same file. … Quit, or continue with caution.(2) An edit session for this file crashed. If this is the case, use ":recover" or "vi -r myfile“ to re-
cover. If you did this already, delete the swap file
".myfile.swp" to avoid this message.
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
vi commands (1)• Command mode to Input Mode
– ‘i’ : from the cursor position– ‘a’ : after the current character– ‘I’ : from the starting column– ‘A’ : after the last character of the line
• Cursor movement– ‘gg’ : move to 1st line of the file– ‘G’ : move to the last line of the file– ‘H’ : move to top right corner of the screen– ‘nnG’ or ‘:nn [ENTER]’ : move to nnth line– ‘h’, ’j’, ’k’, ’l’ : move left, down, up, right by one charac-
ter (nn)– ‘w’, ‘b’ : move to next, previous word (nn)
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
vi commands (2)• Delete, modify
– ‘x’, [DEL] : delete character under cursor (nn)– ‘X’ [BS] : delete the previous character (nn)– ‘dw’ : delete word (nn)– ‘dd’ : delete a line (nn)– ‘cw’ : modify to the end of current word (nn)– ‘cc’ : modify whole line (nn)– ‘C’ : modify to the end of current line– ‘r’ : replace one character under cursor (nn)– ‘R’ : replace characters
• Copy and paste– ‘yw’ : copy word (nn)– ‘yy’ : copy current line (nn)– ‘p’ : paste copied or deleted item, next (nn)– ‘P’ : paste before (nn)
UNDO ?
Try ‘u’
REPEAT ?
Try ‘.’
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
vi commands (3)• Search
– ‘/word ’ : search ‘word ’ forward– ‘n’ find next (nn)– ‘?’ change search direction backword– ‘/’ change search direction forward
• Replace – ‘:s/source/destination’ : replace source to destination– ‘:%s/source/destination’ : replace the FIRST occurrence of
all lines– ‘:%s/source/destination/g’ : replace the ALL occurrence of
all lines– ‘:aa,bbs/source/destination/g’ : replace from line aa to bb
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
vi commands (4)• File Commands– ‘:w’ : save– ‘:w filename’ : save as filename– ‘:wq’ or ‘ZZ’ : save and quit– ‘:q’ : quit without save, if modified, ‘:q!’– ‘:e filename’ : read and edit filename– ‘:r filename’ : read and insert filename– ‘:!<command> : run shell command– ‘:n’ : next file without save, ‘:n!’ if modified– ‘:wn’ : write and next
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
Configuration of vi
• ‘:set <command>’ or in configuration file ~/.exrc– set autoindent : auto indentation– set cindent : auto indentation for C language– set autowrite : automatic write when modify– set smartindent : smarter indentation– set textwidth=nn : wrap after nn th column– set wrap : auto wrap– set backup : do not make backup– set tabstop=nn : set tab with nn – set shiftwidth=n : set indentation with n– set ignorecase : ignore case when search– set showmode : show current mode in bottom line– set number : show line number
Unset ?:set no<command>
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
And more
• Vi has millions of functions– http://vimdoc.sourceforge.net/htmldoc/help.html#help.t
xt
– http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html
– http://wiki.kldp.org/KoreanDoc/html/Vim_Guide-KLDP/Vim_Guide-KLDP.html (KOREAN)
– http://weezzle.net/1575 (KOREAN)
• Look for ‘vi cheat sheet’ !!!– Single page command lists
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
Vi cheat sheets
<www.viemu.com>
<IBM>
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
gedit
• Gnome editor
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
Small tips for source code edit-ing
• Syntax Highlighting (use vim, rather than vi)• Keeps Indentation ! (editor can help)• Do not use tab, but use 4 Spaces (editor can do this)• See man pages for ALL functions you use (some IDE can
help)
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
Let’s Practice !• Write a C program vi, compile & run
Open a terminal$ vi hello.c // show line #$ gcc –ohello hello.c // error ? Edit it !!$ ./hello$ ./hello Minsuk$ ./hello Minsuk Lee$ ./hello Minsuk\ Lee
CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPANEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
hello.c : Type as is !!!