Vim Tip of The Week : +[num] or +/{pat}

By | 2007/07/13

First let me start of by saying that if you haven’t yet taken the plunge to use vi/vim you really should.  This editor will take you to levels of efficiency that you’ve only dreamed of, and probably beyond.  Seriously, take the time to teach yourself some vim tips.

You might want to read a few of my previous posts on vim.

How to pwn your text files with vim

Find and replace with vim

Vim : the ultimate editor

..and for those that want something more, let’s talk a bit about the +[num] and +/{pat} options when launching vim.  These options will let you begin editing on a certain line number (+[num]) or at an instance of a certain pattern (+/{pat}).  Let me show you a few examples.

If you’d like to begin editing a file on line 45 you could launch vim using a command such as:

vim file +45

If you’d like to begin editing at the last line in the file you could use +[num] without assigning a value:

vim file +

This option is really helpful for things such as trying to fix code or fixing syntax errors in configuration files.  If the log displays the line number you could jump right to the line as you open the file.  …and you thought the :[num] option was convenient!

vim file +185, vim file +2, vim file +823, etc.. simple ways to begin editing at a certain point.

The +/{pat} option lets you begin editing at the first occurrence of a certain pattern.  This means instead of opening the file and beginning your search function you can use the search as you open vim.  Some examples:

vim +/foo, vim +/bar, vim +/baz

You can use the :help search-pattern within vim to see available search patterns.

Anyway, I thought I would share this little tidbit of vim-awesomeness today.  Hopefully I can continue to do a vim tip of the week.  Would the faithful readers like that?  Would you like more advanced vim options? Simpler tutorials? Are you happy with gedit and kate and don’t even want to bother?  Let me know, I’ll do my best to help.

6 thoughts on “Vim Tip of The Week : +[num] or +/{pat}

  1. Sander

    This faithful reader would like more vim tips like this. Things like this make editing source code much faster. Thanks!

  2. Frank

    This is a valid tip, but the POSIX standard states that these options should be prefixed with “-c” instead of the “+”. A quick check of “vim –help” shows that vim does support the standard syntax.

    One of the other commenters indicated that they would like additional tips like this. I don’t think my cheatsheet is a “set of tips”, but it does describe the vi command set (which after looking at my cheatsheet could be described as “one from Column A and one from Column B”). I put the link to the cheatsheet in the “website” field of this comment, but you’ll need to scroll down to the “Unix” section.

    Enjoy.

  3. Harley Pig

    Actually, the ‘+’ command line option is a lot more versatile than that.

    You can use almost any ‘:’ command with the +:

    vim +’h keyword’
    vim +%s/pattern/replace/c file

    etc.

    You can also do multiple + commands:

    vim +command +command +command file

    One caveat: the + command only works on the first file listed.

  4. anon

    Great tips + comments as always. The more vi/vim the better!

  5. geshido

    As you can run any : command with ‘+’ in command line options, then you also could do the following:

    vim FILE1 FILE2 +bn +’normal G’ +’normal aHello there’

    .. for example. 🙂

Comments are closed.