![Page 1: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/1.jpg)
1
The vim Editor (ch 6)
IT244 - Introduction to Linux / Unix Instructor: Bo Sheng
![Page 2: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/2.jpg)
2
What’s vim
• vi iMproved• Text editor
– Scripts– Codes (C, Java, HTML)– Configure files and short notes
– No format
![Page 3: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/3.jpg)
3
Get Started
• Starting vim– vim practice
– Emergency exit• ESC, then “:q!”
![Page 4: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/4.jpg)
4
Command and Input Modes
![Page 5: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/5.jpg)
5
Command and Input Modes
• Command mode example– :set number
• Enter input mode– i / a : insert / append
• Back to command mode (ESC)• Save the file and quit
– ZZ
![Page 6: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/6.jpg)
6
Moving the Cursor
• In command mode– Space, enter– Arrow keys– h, j, k, l
• In input mode– Arrow keys
![Page 7: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/7.jpg)
7
Edit Text
• Delete text– In command mode
• x: delete a character• dw: delete a word• dd: delete a line
– In input mode• backspace
![Page 8: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/8.jpg)
8
Edit Text
• Add text– In command mode
• i: insert (at the current cursor)• a: append (precede the current cursor)• o/O: open a new line (below/above)
• Undo and redo– In command mode
• u • :redo
![Page 9: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/9.jpg)
9
Edit Text
• Work buffer– Your editing is not automatically saved
• Readonly mode– view filename– vim -R filename
• Write to disk– :w [filename]
• Quit (:q)
![Page 10: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/10.jpg)
10
More about Moving the Cursor
• Space, Enter, {h,j,k,l}• Search for a character in a line
– f/F– fc, fa, Fc– Repeat the last search: ;
• Moving by words– w/W, b/B, e/E
![Page 11: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/11.jpg)
11
More about Moving the Cursor
• Moving by words
![Page 12: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/12.jpg)
12
More about Moving the Cursor
• Moving by lines– j/k, up arrow/down arrow– Enter/-
![Page 13: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/13.jpg)
13
More about Moving the Cursor
• Moving by sentences/paragraphs– ) , ( , } , {
• Moving within the screen– H/M/L: top/middle/bottom line
![Page 14: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/14.jpg)
14
More about Moving the Cursor
![Page 15: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/15.jpg)
15
More about Moving the Cursor
• Page down/up– CTRL+D / CTRL+U (half screen)– CTRL+F / CTRL+B (full screen)
• Line numbers– :set number– #G
![Page 16: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/16.jpg)
16
Input Mode
• Insert text– i/I , a/A, o/O
![Page 17: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/17.jpg)
17
Delete Text
• Table 6-1 (Pg 181)– dw, d3w– d0, d$– d), d(, d4), d{, d}– dd, 5dd, dL– d (enter), D
![Page 18: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/18.jpg)
18
Change Text
• Table 6-2 (Pg 182)– cw, c3w– c0, c$– c), c(, c4), c{, c}– cc, 5cc, cL
• Change case– ~, 5~
![Page 19: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/19.jpg)
19
Search and Replace
• Search for a character– f/F, t/T, ;– 2ft– d2ft
• Search for a string– /, ?– Search ‘/’ and ‘?’– No string argument, n/N
![Page 20: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/20.jpg)
20
Search and Replace
• Incremental search– :set incsearch– :set noincsearch
• Special characters in search strings– ^: /^the– $: /,$– .: /l..e– *: /com*e
![Page 21: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/21.jpg)
21
Search and Replace
• Special characters in search strings– \<: /\<th– \>: /s\> /\<and\>
– []: /dis[ck]– [^abc], [a-z], [0-9], …
![Page 22: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/22.jpg)
22
Search and Replace
• Replace a string– :[g][address]s/search-string/replace-string[/options]
– Address• Line number: “5”, “10,100”, “.,.+10”• Work buffer: “1,.”, “.,$”, “%”• Matching strings: “/to/”, “g/to/”
![Page 23: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/23.jpg)
23
Search and Replace
• Replace a string (examples)– :s/Linux/linux– :1,.s/Linux/linux/g– :%s/Linux/linux/g– :g/to/s/Linux/linux/g– :1,$s/\<one\>/1/g– :s/Linux/”&”/g
![Page 24: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/24.jpg)
24
Copy and Move Text
• Yank (y)– y (enter), Y, yy– Copy text into a General-Purpose buffer
• Put (p/P)– Copy text from the General-Purpose buffer– Deleted text is also in the General-Purpose
buffer
![Page 25: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/25.jpg)
25
Read and Write Files
• Exit (zz)• Read files
– [address]:r [filename]– :r hello
• Write to files– [address]:w [filename]– :1,5w!– :w >> filename
![Page 26: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/26.jpg)
26
Execute Shell Commands in vim
• Spawn a new shell– :sh– ps -f– Return to vim: CTRL+D, exit
• Directly execute– :!command– :!ls– !!cat hello
![Page 27: The vim Editor (ch 6) IT244 - Introduction to Linux / Unix Instructor: Bo Sheng 1](https://reader033.vdocuments.net/reader033/viewer/2022052401/56649d6e5503460f94a4f3f0/html5/thumbnails/27.jpg)
27
Execute Shell Commands in vim
• Directly execute– Use parts of the file as standard input– vim months– :1,5!sort