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

How do you run your scripts with vim (or emacs)?

by baurel (Sexton)
on Sep 14, 2008 at 12:46 UTC ( #711269=perlquestion: print w/replies, xml ) Need Help??
baurel has asked for the wisdom of the Perl Monks concerning the following question:

I work mostly in vim. I'm not a very advanced vim user, though. So even vim gives me some headeache from time to time I still prefere it to the various editors and IDEs out there, because I like the command mode and because it's available and preinstalled on most platforms.

Although I've tried different things I'm still *very* unsatisfied with the implementation of the following basic workflow:

1. Edit a perl script in the editor
2. Press one key (eg. F5) to save and run the script
3. Print the output to a window below the editor window
4. Possibility to easily switch to the output window and scroll through the messages
5. Possibility to easily switch back to the editor window

Currently I work with a GNU screen/vim combo.
________________ | term1: vim | |~ | |~ | |________________| | term2 | |jdoe:% | |________________|

But there is always a lot of typing involved. For example to accomplish step 2. in the workflow above:

- ESC, :w (to save the script in vim in term1)
- CTRL-Z, tab (to switch to term2)
- type "./" (to run the script in term2)

This can't be it, I think it's far from ideal...

How do you run your scripts with vim? Or is emacs better suited for this?

thanks ben

Replies are listed 'Best First'.
Re: How do you run your scripts with vim (or emacs)?
by parv (Priest) on Sep 14, 2008 at 14:11 UTC

    Seems like you are just starting out with vim|emacs. Both require some quality time to be spent with them to have them bent per your desires (almost). Please see :help on :buffers, :map, :make & :cwindow (which I found via tips search section of Vim home), among other commands.

    I don't run a program through vim, only to check if perl will compile it (map <F5> :setlocal makeprg=perl\ -c\ %\|make<CR>). I do have an occasionally exercised key binding to run the program: map <F6> :setlocal makeprg=perl\ %\|make<CR>.

    Yes, edit-Ctrl-Z-run-fg cycle is indeed tedious for more than thrice. I much prefer to use two xterm windows: one for the editor; other to run and redirect output as I like. Along with command history as pointed out earlier, there is also xterm buffer space to temporarily hold some adjustable number of lines of text.

    I don't like a split window (in any of vim or emacs) which causes small viewport, thus much scrolling. On a big monitor (or two side by side), both windows will be side by side. Else, I just use my window manager's key binding to flip between the windows.

    I must admit that if I were to be working on console (about 25x80 resolution) with access to only one tty, I would be extremely motivated to run a program within vim and to find a way to save output in a file which would linger around and be inserted in buffer list.

      When working with limited *nix consoles, I find it very convenient to use a virtual window manager like screen.

      Especially when dealing with multiple remote logins, it can help to keep the working environment clear. I often use one xterm for emacs and another xterm PuTTY terminal that runs screen with multiple logins on the same or different hosts.

Re: How do you run your scripts with vim (or emacs)?
by Joost (Canon) on Sep 14, 2008 at 16:12 UTC
      Haven't used mode-compile myself before (but will take a look!)

      I tend to just use M-x compile in my Emacs (normally an emacs -nw on an ssh session to a server) combined with an M-x shell buffer where I run little test iterations from the shell.

      Something like this in .emacs:

      (add-hook 'perl-mode-hook (lambda () (unless (or (file-exists-p "makefile") (file-exists-p "Makefile")) (set (make-local-variable 'compile-command) (concat "perl -c " (file-name-completion buffer-file-name))) +)))
      can bind the default M-x compile to run the perl interpreter on your code and if there are errors hitting Enter on the error message will take you to the offending line in your code buffer.

      Having the shell running in an emacs buffer is handy because the whole buffer (or a region of it) can be saved for documentary purposes and you can go back to pick up a command from previously, edit it and re-run it. Since many of my such trials are faking a CGI run (e.g.  perl ./ param1=fred param2=yaada param3=DoIt), it's fairlly easy to tailor and then modify the command from one trial to another.
Re: How do you run your scripts with vim (or emacs)?
by RMGir (Prior) on Sep 14, 2008 at 13:45 UTC
    I usually edit with gvim, and test in an xterm alongside.

    I've used screen before, that also works well.

    It's all a matter of personal preference, of course. There's no wrong answer.

    Insert a "now if only emacs had a good editor, it'd be the answer" joke here :)

    Emacs does actually have a good terminal mode, so if you can stand using it to edit, it is a good choice.

Re: How do you run your scripts with vim (or emacs)?
by Anonymous Monk on Sep 14, 2008 at 12:55 UTC
    one window for editor, another window for ./ with history feature (so you can up arrow through previous commands) works the way I like it
      > one window
      what exactly to you mean with this window:
      - vim window?
      - terminal window (, xterm,..)?
      - GNU screen window?
      and further: - how do you combine save of the script and switch to the window?
      - how do you setup that keybinding?
Re: How do you run your scripts with vim (or emacs)?
by shmem (Canon) on Sep 14, 2008 at 19:17 UTC
Re: How do you run your scripts with vim (or emacs)?
by repellent (Priest) on Sep 14, 2008 at 22:28 UTC
    You may take advantage of Vim's Quickfix List window.

    Place this in your .vimrc:
    function! RunCmd(cmd) let fn = expand("%:p") let ft = &l:filetype botright copen setlocal modifiable %d _ silent execute "read !".a:cmd." ".fn 1d _ normal! 0 if ft != "" execute "setf ".ft else setlocal filetype= endif setlocal nomodifiable nomodified wincmd p endfunction command! -nargs=1 Run call RunCmd(<q-args>) command! RunPerl call RunCmd("/usr/bin/perl")

    Then, while editing your script in the current Vim buffer, do:

    :Run perl

    You should be able to also map these commands to shortcut keys. YMMV -- perhaps something like:
    nnoremap <silent><F5> :<C-u>up|RunPerl<CR> inoremap <silent><F5> <C-o>:up|RunPerl<CR> vnoremap <silent><F5> <Esc>:up|RunPerl<CR>gv

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://711269]
Approved by wfsp
and the radiator hisses contentedly...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-01-20 06:15 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (173 votes). Check out past polls.