SlideShare a Scribd company logo
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
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
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
vi
                                                                    Menu for
                                                                 Gnome-Terminal
                                                                    not for vi




                                                                     vi screen




NEAOSS MC2.0   CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
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
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
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
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
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
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
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
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
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
Vi cheat sheets




                                                                 <www.viemu.com>



                                       <IBM>


NEAOSS MC2.0   CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
gedit
    • Gnome editor




NEAOSS MC2.0   CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA
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
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
hello.c : Type as is !!!




NEAOSS MC2.0   CC-BY 2.0 KR, © Korea OSS Promotion Forum, NIPA

More Related Content

PPTX
09.using shell
PPTX
Space Foundation Overview
PPT
溪州鄉珍珠芭樂
PDF
Blackberry
ZIP
Aart1400 seminar
PPTX
Presentation hasil
PPT
Bluetooth 1
PPTX
A auga e_o_corpo_humano
09.using shell
Space Foundation Overview
溪州鄉珍珠芭樂
Blackberry
Aart1400 seminar
Presentation hasil
Bluetooth 1
A auga e_o_corpo_humano

Viewers also liked (20)

PPT
岸角客家文化
PPT
2011 National Space Symposium
PPTX
05.linux basic-operations-1
PPT
Portfolio Presentation 2
PPTX
08.file system
PPTX
Future of sharing beyond
PPSX
Pp R L S 2011
PPT
Eshopping1
PDF
PSPD Newletter Spring 2011
PDF
これからの司法書士に求められるIT力強化セミナー
PPT
Portfolio Presentation 2
PDF
IT企業専門パソコン肩こり出張マッサージ「ほぐ神」4月末までの渋谷キャンペーン
PDF
The Complete Roadmap Workbook Final Use
DOCX
Small business and enterprenure
PPTX
PPTX
Binary search
PPTX
it's software!
PPT
Module 1 presentation
PPT
Intelligent System for Alzheimer's Disease
PPTX
Open Source Software Day Talk
岸角客家文化
2011 National Space Symposium
05.linux basic-operations-1
Portfolio Presentation 2
08.file system
Future of sharing beyond
Pp R L S 2011
Eshopping1
PSPD Newletter Spring 2011
これからの司法書士に求められるIT力強化セミナー
Portfolio Presentation 2
IT企業専門パソコン肩こり出張マッサージ「ほぐ神」4月末までの渋谷キャンペーン
The Complete Roadmap Workbook Final Use
Small business and enterprenure
Binary search
it's software!
Module 1 presentation
Intelligent System for Alzheimer's Disease
Open Source Software Day Talk
Ad

Similar to 07.using vi (20)

PDF
PDF
Beginning with vi text editor
KEY
VIM for the PHP Developer
PDF
Linux text editors Vim nano
PDF
Linux text editors
PDF
Tuffarsi in vim
PDF
VIM for Programmers
PDF
Vim For Php
PDF
Using VI Editor in Red Hat by Rohit Kumar
PDF
Using vi editor
PPTX
Vi editor
PDF
VIM for (PHP) Programmers
PPTX
Linux Text Editor (Vi) Command List Summary
PDF
Vi reference
PDF
Vi reference
PPTX
Presentacion vim
ODP
Vim and Python
PPT
VI Editors
PDF
1_Editors_in_Unix
PDF
Mission vim possible-full
Beginning with vi text editor
VIM for the PHP Developer
Linux text editors Vim nano
Linux text editors
Tuffarsi in vim
VIM for Programmers
Vim For Php
Using VI Editor in Red Hat by Rohit Kumar
Using vi editor
Vi editor
VIM for (PHP) Programmers
Linux Text Editor (Vi) Command List Summary
Vi reference
Vi reference
Presentacion vim
Vim and Python
VI Editors
1_Editors_in_Unix
Mission vim possible-full
Ad

More from Minsuk Lee (20)

PPTX
CES 처음 가는 분을 위한 가이드
PPTX
NIA-PaaS-TA Pub 키노트
PPTX
모두콘, 어떻게 배울 것인가 발제.
PPTX
GDG-DevFest, 만들면서 자랑하면서 성장하는 개발자
PDF
개발자, 회사.. 왜 오픈소스를 해야할까?
PDF
진정한 소프트웨어 융합교육에 대하여
PDF
FOSS CON Korea 2018
PPTX
소프트웨어 공부하는법
PPTX
자기소개서, 이력서 쓰는 법
PPTX
왜 우리는 개발자에 집중하지 않는가?
PPTX
Somul 2017-이민석
PPTX
국민대-컴퓨터프로그래밍-2017-1-오프라인강좌
PPTX
왜 소프트웨어를 배워야할까?
PDF
소프트웨어, 정말 되는 건가?
PPTX
소프트웨어, 소프트웨어 개발자
PDF
프로그램 기초
PPTX
Software Company, Open Soure Software Company
PPTX
Open Source 그리고 git과 github, code review
PPTX
Data and Sorting Algoritm
PPTX
국민대학교 컴퓨터프로그래밍
CES 처음 가는 분을 위한 가이드
NIA-PaaS-TA Pub 키노트
모두콘, 어떻게 배울 것인가 발제.
GDG-DevFest, 만들면서 자랑하면서 성장하는 개발자
개발자, 회사.. 왜 오픈소스를 해야할까?
진정한 소프트웨어 융합교육에 대하여
FOSS CON Korea 2018
소프트웨어 공부하는법
자기소개서, 이력서 쓰는 법
왜 우리는 개발자에 집중하지 않는가?
Somul 2017-이민석
국민대-컴퓨터프로그래밍-2017-1-오프라인강좌
왜 소프트웨어를 배워야할까?
소프트웨어, 정말 되는 건가?
소프트웨어, 소프트웨어 개발자
프로그램 기초
Software Company, Open Soure Software Company
Open Source 그리고 git과 github, code review
Data and Sorting Algoritm
국민대학교 컴퓨터프로그래밍

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