CS 497C - Introduction to UNIX Lecture 9: The vi/vim Editor Chin-Chih Chang chang@cs.twsu.edu Navigation * You can use the cursor motion keys (arrow keys) to move around the screen. But some terminals don’t recognize these keys. * vi has its own set of commands that move the cursor: - h - moves cursor left. - l - moves cursor right. - k - moves cursor up. - j - moves cursor down. Navigation * You can use the control keys to change the view of displayed text on your window. The four essential scrolling commands are: - [Ctrl-f] - Scrolls full page forward. - [Ctrl-b] - Scrolls full page backward. - [Ctrl-d] - Scrolls half page forward. - [Ctrl-u] - Scrolls half page backward. - [Ctrl-l] - Redraws the screen. Navigation * Precise navigation commands in vi are: - b - moves back to beginning of word. - e - moves forward to end of word. - w - moves forward to beginning of word . - 0 or | - moves to beginning of line. - $ - moves to end of line. - 1G - moves to beginning of file. - G - moves to end of file. Operators * vi operators are commands for modifying the content of a buffer. * These are operators to be remembered: - d - delete - y - yank (copy) - p - put back deleted/yanked text - c - change - ! - filter to act on text * These operators perform function in a combination with a command. Deleting, Moving, and Yanking Text * These are commands for deleting text: - x - deletes character under cursor. - X - deletes previous character. - d$ or D - deletes from cursor to end of line. - dd - deletes current line. - dG - deletes till end of file. - dw - deletes current word. Deleting, Moving, and Yanking Text * These are commands for moving/putting back text: - J - joins current line with next line. - p - puts deleted/copied text below current line or on right of cursor. - P - puts deleted/copied text above current line or on left of cursor. * These are commands for yanking text: - yy or Y - yanks current line. - y$ - yanks from cursor to end of line. - yw - copies current word. Changing Text (c and ~) * These are commands for changing text: - c0 - changes from cursor to beginning of line. - c$ or C - changes from cursor to end of line. - cw - changes current word. - cc - changes current line. - cG - changes from cursor to end of file. - ~ - reverses case of character under cursor. Repeating and Undoing Last Editing Instructions * To repeat the last command, use . (dot). * To undo last commands, use: - u - undoes last editing instruction. - U - undoes all changes made in current line. - [Ctrl-r] - redoes previous undo actions (Linux only).