---------------------------------------------------------------------- Introduction to VIM - Text editor Jim McQuillan - jam@McQuil.com Michigan User Group - MUG.org Tuesday, April 9, 2024 ---------------------------------------------------------------------- Vim is a text editor that is fast, powerful, highly configurable and free. It can be found already installed on pretty much any Unix/Linux system and available for Windows. There are over 1,000 commands and settings. Most people use a small handful of the available commands. In this presentation, we'll cover the handful of commands that I use daily. ---------------------------------------------------------------------- History Created by Bram Moolenaar. He started in 1988 and first released a public version in 1991. It was created as an improvement of Bill Joy's* vi. Released under the Vim license, compatible with the GPL. Current version is 9.1 * Bill Joy, from Farmington Hills, MI ---------------------------------------------------------------------- Starting Vim vim - Open an empty buffer vim filename - Open a buffer with the named file in it vim file1 file2 ... - Open multiple files, use :n and :prev to move between them Exiting Vim :q - Quit. Warns if unsaved changes :q! - Quit, abandoning unsaved changes :wq - Write the buffer and quit :x - Write the buffer and quit ZZ - Write the buffer and quit ---------------------------------------------------------------------- Modes Vim has 14 modes, we'll cover just a few of them here. NORMAL mode - Used for editor commands (Cursor movement) INSERT mode - Used for inserting text REPLACE mode - Any text entered will replace existing text Command-line mode - Single line at bottom for entering commands Visual mode - Select areas of text ---------------------------------------------------------------------- Moving around in your file h,j,k,l - Cursor motion Ctrl-F - Page forward Ctrl-B - Page backward w - Advance to start of next word e - Advance to end of word 0 - Beginning of line $ - End of line :0 - Beginning of file :$ - End of file % - whatever char you are on '[({' it'll find the other one of the pair Ctrl-g - Shows name of file being edited ---------------------------------------------------------------------- Searching / - Search forward ? - Search backward n - Repeat search (either forward or backward) N - Repeat search in opposite direction ---------------------------------------------------------------------- Modifying text r - Change character under cursor cw - Change word x - Delete character under cursor or delete selection {num}x - Delete num characters dw - Delete word {num}dw - Delete num words Shift-d - Delete to end of line dd - Delete line (places the line(s) in the yank buffer) {num}dd - Delete num lines y - Yank line {num}y - Yank num lines p - put line AFTER current line Shift-p - put line BEFORE current line ---------------------------------------------------------------------- Modifying text ... Continued Shift-j - Join next line to end of current line . (dot) - Repeat last command :r filename - Read in the contents of another file i - Enter INSERT mode at current cursor position a - Enter INSERT mode AFTER current cursor position Esc - Return to NORMAL mode : - Enter COMMAND mode ---------------------------------------------------------------------- Repetition count Many commands take a number of repetitions Here's some examples: 3x - Delete 3 characters 4dw - Delete 4 words 7dd - Delete 7 lines 10yy - Yank 10 lines into the yank buffer 4w - Jump forward 4 words ---------------------------------------------------------------------- Undoing your changes u - Undo - Multiple levels Ctrl-r - Redo - Multiple levels :q! - Quit the editor without saving changes ---------------------------------------------------------------------- Saving your file :w - Write the buffer :w filename - Write the buffer to an alternate file :wq - Write the buffer and quit :x - Write the buffer and quit ZZ - Write the buffer and quit :{range}w filename - Write a portion of your buffer to a file ---------------------------------------------------------------------- Ranges of lines Many commands take a range of addresses to perform their action on Here's some examples: . - The current line num - A specific line $ - The last line of the file num,num - A range of lines % - All lines +num - Relative to the current line ---------------------------------------------------------------------- Command mode : - Enter COMMAND mode Esc - Return to NORMAL mode :s/pattern/replacement/ - Search for the pattern and replace occurrences with {replacement}. Only on the current line and only the first occurence on the line. :%s/pattern/replacement/ - Search and replace, like above, but on ALL lines :%s/pattern/replacement/gi - Search and replace, like above, but on ALL lines AND all occurences on each line AND do ignore case ---------------------------------------------------------------------- Visual Mode Shift-v - Select line(s) Ctrl-v - Select column Follow those commands with cursor motion to select more lines or columns ---------------------------------------------------------------------- Split windows :sp - Split window horizontally, same file in both windows :sp filename - Split window, and begin editing a new file Ctrl-W j - Go to next horizontal window Ctrl-W k - Go to previous horizontal window {num}Ctrl-W+ - Increase height of current window {num}Ctrl-W- - Decrease height of current window :vs - Split the window vertically Ctrl-W l - Go to next vertical window Ctrl-W h - Go to previous vertical window Ctrl-W X - Swap the current window with the window below it Ctrl-Wc - Close CURRENT window Ctrl-Wo - Close OTHER window ---------------------------------------------------------------------- Settings :set - Show current settings :set all - Show all available settings :set nu - Turn on line numbering :set ic - Turn on case-insensitive searching :set noincsearch - Turn off incremental search :set nowrapscan - When searching, default is to wrap around to the beginning of the file when the end is reached :set nowrap - Don't wrap long lines. Scroll right to see long lines ---------------------------------------------------------------------- And so much more... - .vimrc - Status line customization - themes - Syntax highlighting - plugins - Key Remapping - Macros - Vimscript - registers - marks - gvim - The graphical version of vim - neovim - A fork of Vim ---------------------------------------------------------------------- Help :help subject - To read the built-in help pages Vimtutor - Run it at command line When in doubt about what mode you are in, hit Esc key. It'll take you back to normal mode. ---------------------------------------------------------------------- Questions? ----------------------------------------------------------------------