Last Updated: February 25, 2016
·
3.503K
· harlow

Run tests from MacVim in iTerm session

The following allows me to use key strokes from within MacVim to stream commands to the current iTerm session

Picture

The run_command script lives in /usr/local/bin/run_command. It runs the input as a command in the terminal, and then puts application focus back on MacVim.

 #!/usr/bin/env osascript
 on run argv
   tell application "iTerm"
     tell the first terminal
       tell the current session
         write text argv as string
       end tell
     end tell
   end

   tell application "MacVim"
     activate
   end tell
end run

Then set up the .vimrc file to call the run_command script

function! SendToTerminal(args)
  execute ":silent !run_command '" . a:args . "'"
endfunction

function! ClearTerminal()
  call SendToTerminal("clear")
endfunction

function! RSpec()
  call ClearTerminal()
  if exists("s:current_test")
    call SendToTerminal("rspec -fd " . s:current_test)
  endif
endfunction

function! RunCurrentTest()
  let s:current_test = expand('%:p')
  call RSpec()
endfunction

function! RunCurrentLineInTest()
  let s:current_test = expand('%:p') . ":" . line('.')
  call RSpec()
endfunction

function! RunLastCommand()
  call RSpec()
endfunction

nmap <Leader>t :call RunCurrentTest()<CR>
nmap <Leader>l :call RunCurrentLineInTest()<CR>
nmap <Leader>rr :call RunLastCommand()<CR>

For more information and other vim tips checkout out my dot files

https://github.com/harlow/dotfiles

2 Responses
Add your response

great script !

over 1 year ago ·

how i run mvim at my iterm session? because when i type mvim then mvim its pop up in to another windows?

over 1 year ago ·