title: How to emulate Matlab’s Run command (F5) from within Vim?
body: <p>I’m using Linux and gVim/Vim as an external editor for Matlab.</p>
  <p>In Matlab editor you can run a file by pressing <kbd>F5</kbd>. I’m trying to reproduce this in Vim (installed from the <code>gvim-gtk</code> Debian package).</p>
  <p>I know it is possible to send the name or the current file or the selected text to Matlab in “shell mode” like so:</p>
  <pre><code>execute '!echo &quot;' .&quot;run(\'&quot;.expand(&quot;%:p&quot;).&quot;\')&quot; . '&quot;| matlab -nojvm' 
  execute '!matlab -nodisplay &lt; '.expand(&quot;%:p&quot;) 
  noremap &lt;C-CR&gt; :?%%?;/%%/w !matlab -nojvm
  </code></pre>
  <p>But I want to use the Matlab GUI (which in my case is already opened).</p>
  <p>Here is the solution I thought could work out for me:</p>
  <ol>
  <li><p>Use a key mapping in vim that would:</p>
  <ul>
  <li>(a) Put in the clipboard something like: <code>run('path-to-file')</code> or <code>cd('folder');run('filename')</code>.</li>
  <li>(b) Call a shell command that gives the focus to the Matlab GUI</li>
  </ul>
  </li>
  <li><p>At this point, I should be in Matlab command window, so I just press <kbd>Ctrl</kbd>-<kbd>V</kbd>, and the content of the clipboard should be pasted. (Even though I’m on Linux, I use Matlab with Windows keybindings.)</p>
  </li>
  </ol>
  <p><strong>Step 1(a):</strong>
  The following will put the <code>run('filename')</code> command to the clipboard, and Matlab will paste it when <kbd>Ctrl</kbd>-<kbd>V</kbd> is pressed:</p>
  <pre><code>let @+=&quot;run(\'&quot;.expand(&quot;%:p&quot;).&quot;')&quot; 
  </code></pre>
  <p>The following two commands would work with the Linux middle button pasting, but that is not what I want, as I want to avoid the mouse in this step:</p>
  <pre><code>let @*=&quot;run(\'&quot;.expand(&quot;%:p&quot;).&quot;')&quot; 
  
  execute '!echo &quot;' . &quot;run(\'&quot;.expand(&quot;%:p&quot;).&quot;\')&quot; . '&quot;| xclip'  
  </code></pre>
  <p><strong>Step 1(b):</strong> To give the focus to the Matlab window, I use the following command:</p>
  <pre><code>wmctrl -a MATLAB &amp;
  </code></pre>
  <p>This works well, but it might also raise a browser window, if it happens to have a page open with the word Matlab in its title (like the one you are reading at the moment).</p>
  <p>See also a more complex solution: <a href="https://stackoverflow.com/questions/2250757/is-there-a-linux-command-to-determine-the-window-ids-associated-with-a-given-pro">Is there a linux command to determine the window IDs associated with a given process ID?</a>.</p>
  <p><strong>My (now old) option, but <kbd>Ctrl</kbd>-<kbd>V</kbd> does not work! Only mouse pasting does:</strong></p>
  <pre><code>function! MatRun()
      let @+=&quot;run(\'&quot;.expand(&quot;%:p&quot;).&quot;')&quot; 
      let @*=&quot;run(\'&quot;.expand(&quot;%:p&quot;).&quot;\')&quot; 
      :call system('xclip', @+)
      !wmctrl -a MAT
  endfunction
  
  map &lt;F5&gt; :call MatRun() &lt;cr&gt;&lt;cr&gt;
  </code></pre>
  <p>Somehow, <kbd>Ctrl</kbd>-<kbd>V</kbd> does not work in this case when combined with <code>wmctrl</code> (see the edit below). The whole point for me is to avoid using the mouse for this operation.</p>
  <p>Thank you for your help.</p>
  <h3>Update: a working option</h3>
  <p>My bad, I was using <code>xclip</code> where I should have used <code>xclip -selection c</code>. See my reply below.</p>
  <p>I’d be glad if somebody find a better solution, for instance: pasting directly in Matlab command windows, making sure you are in the command windows (<kbd>Ctrl</kbd>-<kbd>0</kbd>), or avoiding catching browser windows with <code>wmctrl</code>.</p>