Open a Text File to Specific Line Number at Command Line

May 28, 2007 - 4 Comments

Terminal in OS X

Have you ever known that you need to open a specific text file to a specific line number? Perhaps for a code review or adjustment, whatever the reason this is fairly common. Rather than open the text file in nano or vim and then navigate to the line number you want to access, there is a faster way available to command line users.

In fact, you can jump directly to a specific line number of a text file on launch of vim or nano.

How to Open VIM or nano to Specific Line Number of Text File

John Talbot writes in with this useful terminal tip: “I spend a lot of time writing scripts and editing text through the terminal, so if I get a script error telling me that line 240 is messed up, I use the following command to quickly access the problematic line: nano +240”

I found this to be a really useful command, if you’re unclear of the syntax it’s simple:

nano +linenumber filename

For example, opening the file “” to line number 21 would be with nano:

nano +21

And opens directly with line 21 selected and ready to go.

Great, isn’t it? This works in vim as well:

vi +240

And vim opens to line 240 of Perfect!

Try it out, it works great in most command line text editors for any platform, whether Mac OS X, Linux, BSD, you name it, it works great.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: David Mendez in Command Line, Mac OS X, Tips & Tricks


» Comments RSS Feed

  1. talentpoolz says:

    hey that’s actually pretty useful for command line tools like nano, but is there a way to do it with a gui editor? I doubt it, but then again you can just easily skip to a line in things like TextWrangler

  2. dbr says:

    In vim, the command is :
    :[line number]

    So to go to line 240 would be :240

  3. Mike says:

    bbedit supports the same syntax “bbedit +240”

  4. While dbr is right that inside vim you would type “:240” you can also use exactly the same command with vim as nano so “vi +240” works from the command line.

    On a historic note, this was added to vi so you could jump to a line mentioned in an error message from the C compiler and command line IDEs used it extensively.

Leave a Reply


Shop for Apple & Mac Deals on

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates