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.

If this site has been useful, please consider participating in the Fundraiser.

Other Points of Interest

  • No Related Post

Comments

6 Responses to “Vim Tip of The Week : +[num] or +/{pat}”

  1. Sander on July 13th, 2007 12:06 pm

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

  2. http://shot.pl/wycinki/ on July 13th, 2007 12:16 pm

    More Vim tricks! More advanced ones! :)

  3. Frank on July 13th, 2007 2:09 pm

    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.

  4. Harley Pig on July 14th, 2007 8:28 am

    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.

  5. anon on July 19th, 2007 1:36 pm

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

  6. geshido on August 6th, 2007 2:49 am

    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. :)

Leave a Comment




    Subscribe to the RSS feed!


    subscribe to the ubuntu tutorials RSS feed

    Ubuntu Tutorials Fundraiser


    Please Donate to
    Server Improvement

    Target amount: USD1,000.00
    Total Donations: USD255.00
    Amount Needed: USD745.00

    Thank you for your support!

    Click to Donate

    Polls



  • Blogroll

  • Ads by Google