Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

VIM syntax highlighting in test scripts

by grantm (Parson)
on Mar 24, 2003 at 02:04 UTC ( #245364=note: print w/replies, xml ) Need Help??

in reply to Editing features for advanced users

Excellent article. Here's another tip Perl/vim users may find useful:

When you're creating test files for your CPAN distribution (or other projects) you'll often have a bunch of t/*.t files. Because they don't have a shebang line or a .pl extension, the Perl syntax mode is not automatically enabled. You can have it come on automatically by including a comment like this:

# vim: syntax=perl

Replies are listed 'Best First'.
Re: VIM syntax highlighting in test scripts
by jasonk (Parson) on Mar 24, 2003 at 02:47 UTC

    Or, if you don't want to add that to the top of every test script, you can make vim understand that *.t files are perl by adding this to your .vimrc:

    autocmd BufRead *.t set syntax=perl

    This also has the advantage of having hilighting already turned on when you are editing a new file that you haven't added the vim comment to yet.

    We're not surrounded, we're in a target-rich environment!

      Or, if you have filetype autodetection turned on, you can use:

          autocmd BufRead *.t set filetype=perl

      This will trigger the FileType event which is used to load syntax files, etc. See :help filetype for details.

        On Debian Lenny, it appears that modeline is off by default in vim. You can put the following in your local .vimrc to enable:

        set syntax=on set modeline set modelines=2
        modelines=2 appears to be the number of lines from the top of file to scan for modelines. Then put this on the 2nd line of your source:
        # vim: syntax=perl
        You can manually set the syntax scheme in the vim session with
        :set syntax=perl
        Links for more info:
.vimrc script for "setting a current file to run", for which to compile, jump to errors, and run
by mascip (Pilgrim) on Jul 17, 2012 at 17:42 UTC

    Hi all,
    here is my new .vimrc script for
    - compiling my script
    - IF there is an error, it opens a new tab, where it jumps to the line of the first error, and displays all the errors in a QuickFix buffer under the code (so, all my previous buffers are preserved and it doesn't "move them around")
    - ELSE (no errors) it executes the script
    This is all fairly common, but i added a feature :
    by pressing
    it sets $CURRENT_FILE_TO_RUN to the path of the current active file. Then, when i press
    from any other file or tab, it runs the "current file to run" (the one chosen with ;f).

    In its current version, you MUST have this line in all your perl files, i'm sure it's possible to not need this, i just didn't find how on my computer

    " CAREFUL : i MUST have exactly this line in my Perl files : use Vi::QuickFix 'C:/quickfix_errors_tmp.err';

    It's very useful to me when i'm trying to make a test pass. I press ;f on the test file. Then I open other files in other buffers and/or tabs to modify them and try to make the test pass, and when i want to run the test, i just have to press ;r.
    Then if there are errors it jumps to them, i correct them, press ;r to re-run the test, until the test passes. Then, i press ;qq to close the two buffer of the tab opened by QuickFix (there must be a better way to implement ;qq, but it works fine like this for me)

    I also added ;R to run the current active file (and not the "file to run").

    I hope this helps other people. Here is the code, to paste in .vimrc

    """"""""""""""""""""""""""" " COMPILE, JUMP TO ERROR, and RUN the "CURRENT FILE TO RUN" " CAREFUL : i MUST have exactly this line in my Perl files : " use Vi::QuickFix 'C:/quickfix_errors_tmp.err'; let $ERROR_FILE = "C:/quickfix_errors_tmp.err" map ;r :call MakeWithCopenAndCfileInNewTab()<CR><CR> "current file to make must be the current one... let $CURRENT_FILE_TO_RUN = "%" let $PERL_COMPILE = "perl\\ -Ilib\\ -c\\ " function SetMakeprg() exe ":set makeprg=" . $PERL_COMPILE . $CURRENT_FILE_TO_RUN endfunction autocmd FileType perl call SetMakeprg() autocmd FileType perl set errorformat=%f:%l:%m autocmd FileType perl set autowrite "...unless we choose the file that we are reading as the "current file + to run" map ;f :call SetActiveFileToRun()<CR> function SetActiveFileToRun() let $CURRENT_FILE_TO_RUN=bufname("%") call SetMakeprg() endfunction " print who is the "current file to run" map ;F :call TellAndStop("The \"file to run\" with ;r is " \ . $CURRENT_FILE_TO_RUN \ . "\nUse ;R to run the active buffer file instead" ) "Compile, jump to first error (with QuickFix), and execute if there's +no error "From Marc Jessome, by email, 14th July 2012. "I modified the "for e in qflist "if !e.valid "by replacing it with "if len(qflist)>1 " because the elements of my qflist were not valid until i used :cfile function MakeWithCopenAndCfileInNewTab() make let qflist = getqflist() if len(qflist)>1 tabnew copen cfile $ERROR_FILE else exe "!perl\ -Ilib\ " . $CURRENT_FILE_TO_RUN endif endfunction " tells something to the user, and asks to press a button function TellAndStop(text) call inputsave() let name = input(a:text . "\npress Enter...") call inputrestore() endfunction "run the current file, not the "current_file_to_make" map ;R :call MakeTheCurrentFile()<CR> function MakeTheCurrentFile() "copy the "current_file_to_make" to a buffer let $BUF=$CURRENT_FILE_TO_RUN "compile+run the current file let $CURRENT_FILE_TO_RUN=bufname('%') call SetMakeprg() call MakeWithCopenAndCfileInNewTab() "retrieve the "current_file_to_make" path (from the buffer) let $CURRENT_FILE_TO_RUN=$BUF call SetMakeprg() endfunction "used for QuickFix to open its window after i compile set switchbuf=useopen " to quit a tab with 2 buffers open map ;qq :q<CR>:q<CR> " END of : COMPILE, JUMP TO ERROR, and RUN the "CURRENT FILE TO RUN " """"""""""""""""""""
    BE CAREFUL (this happened to me) : if 'makeprg' is set to a value somewhere else in your .vimrc file, be careful that it doesn't override the value set by this code

    Please enjoy it, improve it, and share what you've improved.

    PS : i think i didn't put this message in the right place. Feel free to move it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://245364]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2017-03-23 07:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (282 votes). Check out past polls.