<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chris Thorn</title>
    <description>The latest articles on DEV Community by Chris Thorn (@thorncp).</description>
    <link>https://dev.to/thorncp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F399795%2F08771281-2264-4368-8cf4-e3a446b7f9ba.jpeg</url>
      <title>DEV Community: Chris Thorn</title>
      <link>https://dev.to/thorncp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thorncp"/>
    <language>en</language>
    <item>
      <title>Vim: Semantic execution</title>
      <dc:creator>Chris Thorn</dc:creator>
      <pubDate>Mon, 01 Jun 2020 16:04:53 +0000</pubDate>
      <link>https://dev.to/thorncp/semantic-execution-3fn</link>
      <guid>https://dev.to/thorncp/semantic-execution-3fn</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted at &lt;a href="https://thorn.co/blog/vim-semantic-execution/" rel="noopener noreferrer"&gt;thorn.co/blog/vim-semantic-execution/&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;




&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop" rel="noopener noreferrer"&gt;REPL&lt;/a&gt; is an excellent tool for exploring data or spiking out ideas. We can quickly answer our questions, then get back to our editor to resume feature work. But when we are unable to get answers quickly and complexity mounts, the REPL becomes a hindrance&lt;sup id="fnref1"&gt;1&lt;/sup&gt;. Much of our time is spent moving back through the command history, hunting for that &lt;em&gt;one&lt;/em&gt; thing we need.&lt;/p&gt;

&lt;p&gt;If we instead do this exploration within our editor, we can retain every piece of code we write, while maintaining the quick feedback loop. It becomes easier to make adjustments or find that &lt;em&gt;one&lt;/em&gt; thing.&lt;/p&gt;

&lt;p&gt;We'll need to execute these scripts differently, depending on the type, which we can configure Vim to do. We can take advantage of the &lt;a href="http://vimdoc.sourceforge.net/htmldoc/filetype.html#filetype-plugins" rel="noopener noreferrer"&gt;filetype plugin&lt;/a&gt; system to use the same mapping for all our scripts. Let's use &lt;code&gt;&amp;lt;leader&amp;gt;x&lt;/code&gt; as the mapping, as a mnemonic for "execute." We'll make use of the &lt;code&gt;clear&lt;/code&gt; command to keep our output isolated, so we can focus on it.&lt;/p&gt;

&lt;p&gt;Create a &lt;em&gt;.vim/ftplugin/ruby.vim&lt;/em&gt; file with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nnoremap &amp;lt;buffer&amp;gt; &amp;lt;leader&amp;gt;x :!clear; ruby '%'&amp;lt;cr&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, we can execute simple Ruby files with our &lt;code&gt;&amp;lt;leader&amp;gt;x&lt;/code&gt; mapping. Then, we can make whatever tweaks we want, and keep iterating until we get what we need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fopj41ir0571qzhgir6qt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fopj41ir0571qzhgir6qt.gif" alt="simple ruby file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since this is configured with Vimscript, we can tailor it for it our needs. Let's look at a bit more complex example.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;.vim/ftplugin/ruby.vim&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if getline(1) == "# rails"
  nnoremap &amp;lt;buffer&amp;gt; &amp;lt;leader&amp;gt;x :!clear; rails runner '%'&amp;lt;cr&amp;gt;
else
  nnoremap &amp;lt;buffer&amp;gt; &amp;lt;leader&amp;gt;x :!clear; ruby '%'&amp;lt;cr&amp;gt;
endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;# rails&lt;/code&gt; as the first line of a Ruby script, we use &lt;code&gt;rails runner&lt;/code&gt; to execute within the context of our Rails app, with access to our models and all other supporting code. We can even store these files in the &lt;em&gt;tmp/&lt;/em&gt; directory and refer to them later.&lt;/p&gt;

&lt;p&gt;If we set this up for all the languages we use&lt;sup id="fnref2"&gt;2&lt;/sup&gt;, then Vim acts as our anchor, no matter what tools our explorations require. It feels good be to home.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Some REPLs do provide basic functionality for dealing with complexity. In &lt;code&gt;psql&lt;/code&gt;, the &lt;code&gt;\e&lt;/code&gt; command will open your editor with the most recent statement. In &lt;code&gt;node&lt;/code&gt;, the &lt;code&gt;.editor&lt;/code&gt; command allows for multiple lines, but doesn't open the editor. We can use the &lt;a href="https://github.com/jberkel/interactive_editor" rel="noopener noreferrer"&gt;interactive_editor&lt;/a&gt; gem with &lt;code&gt;irb&lt;/code&gt;, but it doesn't pull the command history, so we need to remember to start the session with it. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Some reference code that may be useful: &lt;a href="https://github.com/thorncp/dotfiles/blob/9a2a9a5afe3b16fda3c5e65a9d26722da7575987/.vim/ftplugin/go.vim#L3" rel="noopener noreferrer"&gt;go&lt;/a&gt;, &lt;a href="https://github.com/thorncp/dotfiles/blob/9a2a9a5afe3b16fda3c5e65a9d26722da7575987/.vim/ftplugin/html.vim#L5" rel="noopener noreferrer"&gt;html&lt;/a&gt;, &lt;a href="https://github.com/thorncp/dotfiles/blob/9a2a9a5afe3b16fda3c5e65a9d26722da7575987/.vim/ftplugin/javascript.vim#L6" rel="noopener noreferrer"&gt;javascript&lt;/a&gt;,&lt;br&gt;
&lt;a href="https://github.com/thorncp/dotfiles/blob/9a2a9a5afe3b16fda3c5e65a9d26722da7575987/.vim/ftplugin/markdown.vim#L5" rel="noopener noreferrer"&gt;markdown&lt;/a&gt;, &lt;a href="https://github.com/thorncp/dotfiles/blob/9a2a9a5afe3b16fda3c5e65a9d26722da7575987/.vim/ftplugin/ruby.vim#L8-L17" rel="noopener noreferrer"&gt;ruby&lt;/a&gt;, &lt;a href="https://github.com/thorncp/dotfiles/blob/9a2a9a5afe3b16fda3c5e65a9d26722da7575987/.vim/ftplugin/sql.vim#L19-L23" rel="noopener noreferrer"&gt;sql&lt;/a&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>vim</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
