My Favorite vi Editor Tips - June 21, 2008
Nothing quite compares to the vi editor when performing fast edits of files on a Unix command line. Tip “#0″ would be to know your modes. Command mode (press ESC key) interprets your keystrokes as commands (for example, “j” moves down). Insert mode (press I key) lets you actually type stuff into your file. Line mode (press “:” key) lets you do cool stuff like search and replace. Here are my favorite tips.
- In Command mode, CTRL+g shows you current line information.
- In Command mode, forward slash “/search_term” searches down. Question mark “?search_term” searches up.
- In Command mode, “Number” followed by capital G takes you directly to the line number (23G takes you to line 23).
- Need to delete pesky ^M characters from a Windows text file? In Line mode you can do “:%s/^M//g” (to type the ^M you need to hold down CTRL and then press V then press M)
- Need to quickly comment out the next bunch of lines in a Perl script? In Line mode, this will comment out the next 5 lines starting with the current line. “:.,+5 s/^/# /g”
- Need to uncomment those same lines? “.,+5 s/^# //g”
- In Line mode “:set ic” will make searches case insensitive. “:set noic” will change them back to case sensitive.
- In Command mode, capital “G” takes you directly to the end of the file. “$” takes you to the end of the current line.
- In Line mode, use “c” with a search and replace to make vi ask you to confirm each replacement (example: “:%s/Breed/Bread/gc”).
- In Line mode, you can delete all empty lines with “:g/^$/ d”
- To search for characters such as the Microsoft Word curly quotes or any non-standard character that displays something like “\xe2\xe2\xe3″, you can use CTRL+x to type them (example: CTRL+x followed by “e” followed by “2″ will print “\xe2″)
Comments Off