How to view and manage script line numbers in OS X

Apple's built-in text editors offer some quick options for locating and revealing line numbers in documents, which can be exceptionally useful when scripting.

While the need to locate a specific line in a text file may not be a very useful feature in regular prose text, for managing structured code such as scripts and XML files that are common in OS X, being able to identify a specific line or two may be quite useful.

If you have attempted shell scripting in OS X, you may find that when your scripts run into errors you will be given a brief description of the problem that includes the line where the problem occurred. While the use of BBEdit, TextWrangler, and other powerful text editors will make finding these lines a fairly simple task, if you are using Apple's built-in editors (including Terminal-based editors) then finding line numbers are more hidden options.

Bash script error line number
When a syntax error occurs in a shell script, the system will output the line on which the error occurred to help you locate it, but many built-in programs by default will not show you the line number (click for larger view). Screenshot by Topher Kessler/CNET

The first option is for Apple's default editor TextEdit, which can be used as a crude editor for plain-text files. For this program a new hint on MacOSXHints today outlines a relatively hidden but potentially useful feature which allows you to jump to a specific line number in any opened document. Simply press Command-L in a document, and then enter the line number and the program will jump to and select that line. You can also use relative selections such as "+2" to select the next two lines from the location of the cursor (see the hint for more details).

In addition to TextEdit and third-party text editors, you can use some convenient Terminal editors and commands to view the line numbers of a script file. A couple of the common editors in OS X are pico (nano) and vi (vim), which will be familiar to those who have used BSD systems, but less so to those just starting out with scripting. In both of these programs, you can reveal line numbers by the following actions:

  • pico/nano -- Press Control-C to reveal the current cursor location
  • vi/vim -- Enter the VI command ":set number"

The final option in the OS X Terminal is to use the "cat" command which will output a document's contents line-by-line to the Terminal window, and is a great way to preview a document. The "cat" command offers an option to number its output, so if you type "cat -n" followed by a single space, then drag your document to the Terminal window and press Enter to execute the command, it will show the document's contents in a numbered list.

Numbered line output with the "cat" command
With the "cat" command's line numbering, we can identify the syntax error quickly (click for larger view). Screenshot by Topher Kessler/CNET

With this option, one potentially useful application is to create a line-numbered version of any plain text document by redirecting the output to a new file, such as the following example:

cat -n ~/path/to/document.txt > ~/numberedfile.txt

This command will number the lines of the file "document.txt" and save it in a new file called "numberedfile.txt" in your home folder, which you can then open in any program.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Tags:
Computers
About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    Join the discussion

    Conversation powered by Livefyre

    Don't Miss
    Hot Products
    Trending on CNET

    HOT ON CNET

    Find Your Tech Type

    Take our tech personality quiz and enter for a chance to win* high-tech specs!