Contenu connexe
Similaire à 07.using vi (20)
07.using vi
- 1. Using vi editor
Minsuk Lee
Hansung University, Seoul, Korea
minsuk@hansung.ac.kr
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 2. 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’].
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 3. Vi as a modal editor
• 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 file
‘i’, ‘a’ ‘:’
Input Command ex
Mode Mode Mode
[ESC] [ESC]
[ENTER]
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 4. vi
Menu for
Gnome-Terminal
not for vi
vi screen
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 5. Survival guide of vi
1. $ vi filename
2. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 6. If you see this screen
• It’s the result of unwanted stop. (e.g., killed)
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 7. Recover message
Found a swap file by the name ".myfile.swp"
owned by: myuser dated: Sun Aug 28 11:12:41 2011
modified: no
While 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 recover.
If you did this already, delete the swap file ".myfile.swp"
to avoid this message.
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 8. 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 character (nn)
– ‘w’, ‘b’ : move to next, previous word (nn)
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 9. vi commands (2)
• Delete, modify
– ‘x’, [DEL] : delete character under cursor (nn)
– ‘X’ [BS] : delete the previous character (nn)
– ‘dw’ : delete word (nn)
UNDO ?
– ‘dd’ : delete a line (nn) Try ‘u’
– ‘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) REPEAT ?
– ‘R’ : replace characters
• Copy and paste
Try ‘.’
– ‘yw’ : copy word (nn)
– ‘yy’ : copy current line (nn)
– ‘p’ : paste copied or deleted item, next (nn)
– ‘P’ : paste before (nn)
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 10. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 11. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 12. 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
Unset ?
– set backup : do not make backup
:set no<command>
– 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 13. And more
• Vi has millions of functions
– http://vimdoc.sourceforge.net/htmldoc/help.html#help.txt
– http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.h
tml
– 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 14. Vi cheat sheets
<www.viemu.com>
<IBM>
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 15. gedit
• Gnome editor
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 16. Small tips for source code editing
• 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)
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 17. 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
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
- 18. hello.c : Type as is !!!
NEAOSS MC2.0 CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA