Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

.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 ( #982298=note: print w/ replies, xml ) Need Help??


in reply to VIM syntax highlighting in test scripts
in thread Editing features for advanced users

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
;f
it sets $CURRENT_FILE_TO_RUN to the path of the current active file. Then, when i press
;r
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';

EXAMPLE SCENARIO :
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.


Comment on .vimrc script for "setting a current file to run", for which to compile, jump to errors, and run
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://982298]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2014-08-31 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls