Americas

  • United States
sandra_henrystocker
Unix Dweeb

Unix tip: Getting smarter and faster with vi

How-To
Dec 09, 20166 mins
Data CenterLinux

Whether you call it vi or vim, the longstanding Unix editor has a wonderful set of features that continue to make it a versatile and surprisingly powerful tool. Many of those features make it easier for you to get a lot of work done in a small amount of time. In today’s post, we’re going to look at some of the vi features that facilitate making changes to your text files — especially repetitive changes.

Numbering lines, moving text from one place to another, using auto-indentation, bookmarking locations, and making global text replacements can all save you a considerable amount of time and improve your focus when you’re working with large text files.

Numbering lines

Displaying line numbers when you’re editing text can be very helpful, especially when you’re troubleshooting and an error tells you that there’s a problem on line number 765. Adding and removing line numbers is as easy as typing :set number and :set nonumber while you’re working.

  1 #!/bin/bash
  2
  3 # findsuid--Checks all SUID files or programs to see if they're writeable,
  4 #   and outputs the matches in a friendly and useful format.
  5
  6 mtime="7"       # How far back (in days) to check for modified cmds
  7 verbose=0       # By default, let's be quiet about things.
  8
  9 if [ "$1" = "-v" ] ; then
 10   verbose=1          # User-specified findsuid –v, so let's be verbose.
 11 fi
:set number

Moving lines

The commands to use for moving lines from one place to another within a file depend on whether you want to move the lines or repeat them (i.e., not delete them from the original location). If your goal is to move the lines, you would use a command such as 3dd (delete three lines) when positioned on the first of the lines to be moved and, after moving to the insert location, simply typing p (for “put”) to insert the lines. If you want to copy the lines, use 3yy to “yank” the lines instead and p when youi get to the insertion point.

Using auto-indentation

Using autoindent, once you indent a line, the subsequent lines will automatically start at the same location. Type :set autoindent to turn autoindenting on. Typing : will turn off indentation on the current and subsequent lines.

#!/bin/bash

:set autoindent
if [ $# != 1 ]; then
    echo "USAGE: $0 arg"
    exit 1
fi

To turn off autoindent, use :set noautoindent.

Finding text

Finding text with vi is pretty basic. You type / followed by the text you want to find (e.g, /findme). Once you’ve specified the pattern you want to find, you can find the next instance of the pattern simply by typing n. There are also some interesting and useful ways to use this command. For example, you can use a pattern — such as /findme.*too to find patterns that include “findme” following by any number of characters and then the word “too”.

In addition, if you make a change to the text that you’ve found — say you change an instance of “goodbye” to “so long for now”. Type n for “next” to find the next instance of “goodbye” and . to repeat the change. You can run through a file making changes with long n and . sequences. While this isn’t going to be as fast as making a global change with one subsitute command, it allows you to examine each piece of text to be replaced before you replace it — sometimes avoiding changes you didn’t really mean to make.

While Unix/Linux systems are case sensitive, you can set vi to ignore case by using the :set ignorecase setting. This can be extremely useful when you have no idea whether the text you are searching for will be capitalized or not. And, as for reverting this setting, the process is much the same as other settings. You type :set noignorecase.

Replacing text

Replacing text by finding the text you want to modify and entering commands such as c3w to replace three words with some other text. But you can also replace text globally (e.g., all instance of some string in the file). The command :s /this/that/ replaces one word with another on the current line. The command :s/this/that/g makes the change to every instance of the word “this” on the current line. The command :%s/this/that/g changes every “this” in the file to “that”, even multiple instances of “this” in the same line.

A more exotic use of the the substitute command is to use the syntax that captures a pattern you create and then uses it in the replacement command. For example, the command :%s/(pattern)/new 1 to use/g will look for instances of the word “pattern”, storing that string as pattern #1 (refer to the “1” in the command), and then use the pattern in the replacement. Where the text was “pattern”, it will turn into “pattern to use”. Any string you specify will be stored in the captured pattern and you can save up to nine of them.

The components of these replacement commands are:

:%s			make global substitutions
/			pattern separator
(string)	        the string you want to find must be embedded in
			the ( and ) character sequences
1			the pattern you've indentified
g			specifying that you want the change to be made
			everywhere in the file

You can also add a ^ before the text you are searching for if you only want to make changes when it appears at the beginning of lines or a $ following the text if you only want to change text at the ends of lines. Similarly, the command :%s/(you)$/and 1 too/g changes “you” to “and you too”, but only where it appears as the last word on lines.

Using bookmarks

Probably one of the less well known vi features allow you to insert bookmarks into files when you’re editing them by typing an m followed by any letter (a-z). In other words, you can have up to 26 bookmarks active in a file, making it easier to move back and forth in a large file when you’re editing it. When you want to return to a bookmarked location, just type the ` character followed by the latter you used as your bookmark. For example, `a might be your first bookmark.

You can flip between a bookmark and the previous one by typing — very handy if you need to go back and forth between two locations in a file.

 

 

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.