d1pa0a
Last Updated: February 25, 2016
·
2.715K
· harlow
Harlow avatar

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

Say Thanks
Respond

2 Responses
Add your response

6662
Zzh

great script !

over 1 year ago ·
16818
4fb7b3460ac49867715688aeef705690

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 ·
Filed Under