<?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>Forem: David Lains</title>
    <description>The latest articles on Forem by David Lains (@dlains).</description>
    <link>https://forem.com/dlains</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%2F27878%2Fbdf4314e-be48-4ece-be29-a5111aa9c7f7.jpeg</url>
      <title>Forem: David Lains</title>
      <link>https://forem.com/dlains</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dlains"/>
    <language>en</language>
    <item>
      <title>Even Better Rails Date Formatting</title>
      <dc:creator>David Lains</dc:creator>
      <pubDate>Tue, 03 Sep 2019 17:29:34 +0000</pubDate>
      <link>https://forem.com/dlains/even-better-rails-date-formatting-2742</link>
      <guid>https://forem.com/dlains/even-better-rails-date-formatting-2742</guid>
      <description>&lt;p&gt;I just recently came across &lt;a href="https://andycroll.com/"&gt;Andy Croll&lt;/a&gt;'s &lt;a href="https://andycroll.com/ruby/using-strftime-in-a-rails-view-is-probably-a-mistake/"&gt;article&lt;/a&gt; about using &lt;code&gt;strftime&lt;/code&gt; to format date and time values in a Rails View.&lt;/p&gt;

&lt;p&gt;He makes a strong argument that you shouldn't use &lt;code&gt;strftime&lt;/code&gt; in a Rails View:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= @user.last_sign_in_at.strftime('%m-%e-%y %H:%M') %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because Rails has built in date and time formats, and you can also define your own. To use one of the built in formats you can just call &lt;code&gt;to_s&lt;/code&gt; on a &lt;code&gt;Date&lt;/code&gt; or &lt;code&gt;Time&lt;/code&gt; class and pass in the format you wish to use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= @user.last_sign_in_at.to_s(:long) %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;See &lt;a href="https://andycroll.com/ruby/using-strftime-in-a-rails-view-is-probably-a-mistake/"&gt;Andy Croll's article&lt;/a&gt; for the nitty gritty details.&lt;/p&gt;

&lt;h2&gt;
  
  
  What About &lt;code&gt;nil&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;If you've spent much time writing Ruby or Rails code you probably already see an issue with the &lt;code&gt;to_s&lt;/code&gt; call above. What if the &lt;code&gt;last_sign_in_at&lt;/code&gt; value is &lt;code&gt;nil&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;You might think... "Hmm, no problem. Calling &lt;code&gt;to_s&lt;/code&gt; on &lt;code&gt;nil&lt;/code&gt; just returns an empty string, so no harm, no foul."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_s&lt;/span&gt;
  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Well, there are two problems with that line of thinking. First, Rails actually creates a &lt;code&gt;to_formatted_s&lt;/code&gt; method that takes a single argument which references the format to use. The &lt;code&gt;to_formatted_s&lt;/code&gt; method is aliased too &lt;code&gt;to_s&lt;/code&gt; for &lt;code&gt;Date&lt;/code&gt; and &lt;code&gt;Time&lt;/code&gt; objects. If you try to pass an argument to &lt;code&gt;nil&lt;/code&gt;s version of &lt;code&gt;to_s&lt;/code&gt; you get an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_s&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;ArgumentError&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wrong&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;arguments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;given&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Second, you might actually want to have some visual representation that the date or time is missing or unset. I like to show a couple of dash characters when a date or time is missing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best of Both Worlds
&lt;/h2&gt;

&lt;p&gt;Here is how I handle date and time formatting in my Rails apps.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;application_helper&lt;/code&gt; module I add a &lt;code&gt;format_date&lt;/code&gt; method, so it is available to every view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;format_date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;format&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blank?&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'--'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_s&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In your views you can call it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= format_date @user.last_sign_in_at %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If the user has never signed in you will see the two dashes that signify there is no &lt;code&gt;last_sign_in_at&lt;/code&gt; time set. Otherwise the &lt;code&gt;last_sign_in_at&lt;/code&gt; will be formatted with the &lt;code&gt;:long&lt;/code&gt; format.&lt;/p&gt;

&lt;p&gt;If you want to override the default format you can always pass in a different one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= format_date @user.last_sign_in_at, :db %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or use your own format (provided you defined one as outlined in Andy Croll's article).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= format_date @user.last_sign_in_at, :myformat %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Be aware of &lt;code&gt;nil&lt;/code&gt;s in your code. Anytime you see a &lt;code&gt;.&lt;/code&gt; in your code think to yourself, "What happens if the thing on the left side of that &lt;code&gt;.&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt;?".&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally posted at &lt;a href="https://davidlains.com"&gt;davidlains.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>programming</category>
    </item>
    <item>
      <title>tmux Window Juggling</title>
      <dc:creator>David Lains</dc:creator>
      <pubDate>Tue, 29 Jan 2019 07:41:41 +0000</pubDate>
      <link>https://forem.com/dlains/tmux-window-juggling-5e7k</link>
      <guid>https://forem.com/dlains/tmux-window-juggling-5e7k</guid>
      <description>&lt;p&gt;I keep finding more and more depth to &lt;em&gt;tmux&lt;/em&gt;. It really is a remarkable piece of software. In this article I will show you how to turn a &lt;em&gt;tmux&lt;/em&gt; Window into a Pane and vice-versa.&lt;/p&gt;

&lt;p&gt;Why would you want to move Panes and Windows around in a &lt;em&gt;tmux&lt;/em&gt; session? I'll explain my scenario, and maybe that will give you some ideas of your own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debugging Sessions
&lt;/h2&gt;

&lt;p&gt;I create &lt;em&gt;tmux&lt;/em&gt; scripts for all of my projects. This allows me to simply run the script to have the development environment for the given project up and running in a &lt;em&gt;tmux&lt;/em&gt; session.&lt;/p&gt;

&lt;p&gt;For a Rails project I generally have a &lt;em&gt;tmux&lt;/em&gt; window named &lt;code&gt;server&lt;/code&gt; that is running the Rails server. I make it the last window in the list because I don't need to look at it too often.&lt;/p&gt;

&lt;p&gt;However, I also use Byebug for debugging Ruby. If you haven't used Byebug or a similar debugger before the way it works is you add a &lt;code&gt;byebug&lt;/code&gt; statement in your code where you want a breakpoint. When you run the code the &lt;code&gt;byebug&lt;/code&gt; statement will halt the process and transfer control to the debugger.&lt;/p&gt;

&lt;p&gt;In a Rails project the server is running the code, so when Byebug hits a breakpoint the debugger stops in the server window. Then you can interact with the debugger as you normally would, inspecting variables, stepping through code, etc.&lt;/p&gt;

&lt;p&gt;Now you can perhaps see where this is going. When debugging Rails it would be very handy to have the server window in a split pane next to my editor. That way I can see the relevant code and the debugger in the same &lt;em&gt;tmux&lt;/em&gt; window.&lt;/p&gt;

&lt;h2&gt;
  
  
  Window Becomes Pane
&lt;/h2&gt;

&lt;p&gt;The first &lt;em&gt;tmux&lt;/em&gt; command we need is &lt;code&gt;join-pane&lt;/code&gt;. The &lt;code&gt;join-pane&lt;/code&gt; command gives you the ability to attach a window or pane somewhere else in &lt;em&gt;tmux&lt;/em&gt; to your current window. The &lt;code&gt;join-pane&lt;/code&gt; command has the following form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;join-pane [-bdhv] [-l size | -p percent] [-s src-pane] [-t dest-pane]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We wont need all of those options thankfully. The &lt;code&gt;-s&lt;/code&gt; option is used to specify the source pane or window to join into the current window.&lt;/p&gt;

&lt;p&gt;A quick refresher for how to reference things in &lt;em&gt;tmux&lt;/em&gt; may be in order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[session]:[window].[pane]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For example, if your current &lt;em&gt;tmux&lt;/em&gt; session is named &lt;code&gt;giggles&lt;/code&gt; then a reference to the third pane in the second window would be &lt;code&gt;giggles:2.3&lt;/code&gt;. You can omit the session name if you are currently in the session with the window you want to reference. And yes, that means you can use &lt;code&gt;join-pane&lt;/code&gt; to move a window or pane from a different &lt;em&gt;tmux&lt;/em&gt; session to your current session.&lt;/p&gt;

&lt;p&gt;For my case I want to move a whole window, so I can just use &lt;code&gt;-s N&lt;/code&gt;, where &lt;code&gt;N&lt;/code&gt; is the window number I'm interested in. You can also move a window by its name, so &lt;code&gt;-s server&lt;/code&gt; would work just as well.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-l&lt;/code&gt; and &lt;code&gt;-p&lt;/code&gt; arguments can be used to size the new pane appropriately. I'm fine with the default 50/50 split so there is no need to use either.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-t&lt;/code&gt; argument can be used to move the source pane to someplace other than the current window. Also not an option I will need. The &lt;code&gt;-b&lt;/code&gt; option works with &lt;code&gt;-t&lt;/code&gt; to specify that the new pane should be placed to the left or above the target pane.&lt;/p&gt;

&lt;p&gt;The joined pane will be active when you complete the &lt;code&gt;join-pane&lt;/code&gt; command. If you don't want it to be you can use the &lt;code&gt;-d&lt;/code&gt; option.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-h&lt;/code&gt; and &lt;code&gt;-v&lt;/code&gt; arguments make new pane a horizontal or vertical pane respectively.&lt;/p&gt;

&lt;p&gt;Now I can put together the first command I need. I want to move my &lt;code&gt;server&lt;/code&gt; window into my &lt;code&gt;editor&lt;/code&gt; window for this debugging session. First I enter command mode with &lt;code&gt;&amp;lt;Prefix&amp;gt;:&lt;/code&gt;. Now the &lt;code&gt;join-pane&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;join-pane -h -s server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And there it is, my server window next to my editor window. One weird thing about &lt;em&gt;tmux&lt;/em&gt; is how it deals with horizontal and vertical splits. The &lt;code&gt;-h&lt;/code&gt; argument specified above produces what I would call a vertical split. There is a pane on the left and a pane on the right. The &lt;code&gt;-v&lt;/code&gt; argument does the opposite, putting one pane on top and the other on the bottom.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pane Becomes Window
&lt;/h2&gt;

&lt;p&gt;To reverse the &lt;code&gt;join-pane&lt;/code&gt; operation and make a pane a window use the &lt;code&gt;break-pane&lt;/code&gt; command. The &lt;code&gt;break-pane&lt;/code&gt; command has the following format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;break-pane [-dP] [-F format] [-n window-name] [-s src-pane] [-t dest-window]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Again, we won't need all of these options. Just like with the &lt;code&gt;join-pane&lt;/code&gt; command, if you specify the &lt;code&gt;-d&lt;/code&gt; option the window will not become active after the move. The &lt;code&gt;-P&lt;/code&gt; argument tells &lt;em&gt;tmux&lt;/em&gt; to print information about the new window, and the &lt;code&gt;-F&lt;/code&gt; argument provides the ability to format that information.&lt;/p&gt;

&lt;p&gt;If you don't specify a source pane with &lt;code&gt;-s&lt;/code&gt; then the currently active pane will be broken out to its own window. Otherwise you can specify the pane to move the same way you did for the &lt;code&gt;join-pane&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Panes are not named like windows are, so when you move a pane to its own window &lt;em&gt;tmux&lt;/em&gt; will use the default name for the window, which is the current command. If you would like to name the window use the &lt;code&gt;-n&lt;/code&gt; argument and the window name.&lt;/p&gt;

&lt;p&gt;The other option, if you have an existing window that you would like to move the pane to you can use the &lt;code&gt;-t&lt;/code&gt; argument with the reference to the desired window.&lt;/p&gt;

&lt;p&gt;For my case when I'm done with the debugging session I just want to move the pane back to its own window named &lt;code&gt;server&lt;/code&gt;, and I don't need it to be active. I'd rather the &lt;code&gt;editor&lt;/code&gt; window remain active. I can achieve this with the following command (remembering to activate command mode first with &lt;code&gt;&amp;lt;Prefix&amp;gt;:&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;break-pane -d -n server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That tells &lt;em&gt;tmux&lt;/em&gt; to move the currently active pane to a new window at the end of the window list, do not make it active, and name it &lt;code&gt;server&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Key Bindings
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;join-pane&lt;/code&gt; and &lt;code&gt;break-pane&lt;/code&gt; commands aren't too hard to use in an ad-hoc manner to move panes around as necessary. It is a little bit of typing, but if the window to move is not named, or you need to move different windows around you are stuck having to use the commands themselves.&lt;/p&gt;

&lt;p&gt;In my case I know the &lt;code&gt;server&lt;/code&gt; window will always be available because it is in the &lt;em&gt;tmux&lt;/em&gt; script that sets up the environment. Since I know the window name will not change I can create key bindings for those commands. As an added benefit, these key bindings are only available for this session because they are not in the main &lt;code&gt;.tmux.conf&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tmux bind-key u join-pane -h -s server
tmux bind-key U break-pane -d -n server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, whenever I want to see the &lt;code&gt;server&lt;/code&gt; window next to my code I can just type &lt;code&gt;&amp;lt;Prefix&amp;gt;u&lt;/code&gt; and to return it to its own window I can type &lt;code&gt;&amp;lt;Prefix&amp;gt;U&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally posted at &lt;a href="https://davidlains.com"&gt;davidlains.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>tmux</category>
      <category>productivity</category>
      <category>debugging</category>
    </item>
    <item>
      <title>Create Your Own Vim Commands</title>
      <dc:creator>David Lains</dc:creator>
      <pubDate>Tue, 22 Jan 2019 06:13:54 +0000</pubDate>
      <link>https://forem.com/dlains/create-your-own-vim-commands-415b</link>
      <guid>https://forem.com/dlains/create-your-own-vim-commands-415b</guid>
      <description>&lt;p&gt;If you have used Vim for any length of time you have run across Ex commands. Hell, you use an Ex command to exit Vim; &lt;code&gt;:quit&lt;/code&gt;, or just &lt;code&gt;:q&lt;/code&gt;. There are Ex commands available to do pretty much anything that can be done in Vim.&lt;/p&gt;

&lt;p&gt;You can make your own Ex commands as well. Many Vim plugins add their own Ex commands as part of the plugin. You might think it is hard to create a new Vim command, but it isn't. I'll show you the basics here, and where to look for more information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Open a Reference File
&lt;/h2&gt;

&lt;p&gt;I find it much easier to learn things if there is something I want to build, so this is the command that will be built.&lt;/p&gt;

&lt;p&gt;There is a particular code editing situation that I find myself in very often. I'm working on a bit of code, say an HTML page, and I need to add a class name that is defined in one of the project's CSS files. I can't remember the class name though. I want to open the CSS file in a split window so I can see it, but I don't need to edit it.&lt;/p&gt;

&lt;p&gt;This is easy enough to do in Vim, simply &lt;code&gt;:vsplit [path_to_css_file]&lt;/code&gt; and the CSS file is available in a split window.&lt;/p&gt;

&lt;p&gt;Ahh, but there is a problem. Using &lt;code&gt;:vsplit&lt;/code&gt; opens the CSS file in a new Vim window, makes it the active window and moves the HTML file I'm editing to the right. What I really want is to open the CSS file in a window on the right and continue editing the HTML file.&lt;/p&gt;

&lt;p&gt;I just need the CSS file for reference.&lt;/p&gt;

&lt;p&gt;I'll actually create two commands in this post. One to open a reference file in a vertical window to the right and leave the current window active, and a horizontal version of the same command that opens the reference file in the bottom window.&lt;/p&gt;

&lt;p&gt;Along the way I will describe the Vim &lt;code&gt;:command&lt;/code&gt; command and its options so you can create your own Vim commands if you need to.&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;:command&lt;/code&gt; Command
&lt;/h2&gt;

&lt;p&gt;That's right, the Vim command to create user defined commands is &lt;code&gt;:command&lt;/code&gt;. You can use &lt;code&gt;:command&lt;/code&gt; on its own in Vim to get a list of user defined commands. It is handy if you ever forget a command name you created or one you loaded from a plugin. When creating a user command &lt;code&gt;:command&lt;/code&gt; takes the following form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="p"&gt;:&lt;/span&gt;command &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;args&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; Name actions
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The new command you create has to have a name, of course, otherwise how could you call it. The name must start with a capital letter. This is a requirement of Vim and is used to keep user defined commands separate from internal Vim commands. There is one exception though, it can not be named &lt;code&gt;:Next&lt;/code&gt;. Apparently Vim broke its own rule.&lt;/p&gt;

&lt;p&gt;Once the command is working you can use it in Vim by entering &lt;code&gt;:Name&lt;/code&gt;, but, you know, replacing Name with the actual command name.&lt;/p&gt;

&lt;p&gt;I'll name these commands &lt;code&gt;:Ref&lt;/code&gt; and &lt;code&gt;:Vref&lt;/code&gt; to stick to the Vim convention of &lt;code&gt;:split&lt;/code&gt; for a horizontal window and &lt;code&gt;:vsplit&lt;/code&gt; for a vertical window. So the commands so far are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="p"&gt;:&lt;/span&gt;command Ref
&lt;span class="p"&gt;:&lt;/span&gt;command Vref
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Optional Args
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;:command&lt;/code&gt; command takes zero or more optional arguments. The most important of these is the &lt;code&gt;-nargs=&lt;/code&gt; argument. This lets Vim know if the command takes any arguments. The possible values it can be set to are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0 : The command takes no arguments. This is the default, so it can be omitted entirely.&lt;/li&gt;
&lt;li&gt;[1-9]: The command takes the specified number of arguments.&lt;/li&gt;
&lt;li&gt;*: The command takes zero or more arguments.&lt;/li&gt;
&lt;li&gt;+: The command takes one or more arguments.&lt;/li&gt;
&lt;li&gt;?: The command take zero or one argument.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The commands I want to build will take a single argument, the path to the reference file to open, so I'll be using &lt;code&gt;-nargs=1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can access the arguments in a number of ways. First, just using &lt;code&gt;&amp;lt;args&amp;gt;&lt;/code&gt; somewhere in the &lt;code&gt;actions&lt;/code&gt; part of the command. Second, if you need to make sure the arguments are properly escaped for use in an expression you can use &lt;code&gt;&amp;lt;q-args&lt;/code&gt;. Finally, if you are going to pass the arguments to a Vim function you can use &lt;code&gt;&amp;lt;f-args&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another argument that will be used in these commands will be &lt;code&gt;-complete=&lt;/code&gt;. This argument lets Vim know what kind of tab completion is available for the command. There are over thirty different values that this can be set to. Far too many to cover here, but some of the values are &lt;code&gt;buffer&lt;/code&gt; to complete buffer names, &lt;code&gt;dir&lt;/code&gt; to complete directory names, &lt;code&gt;help&lt;/code&gt; to complete help topic name and &lt;code&gt;file&lt;/code&gt; to complete file and directory names.&lt;/p&gt;

&lt;p&gt;The last argument that will be needed is the &lt;code&gt;-bar&lt;/code&gt; arguments. This tells &lt;code&gt;:command&lt;/code&gt; that the &lt;code&gt;actions&lt;/code&gt; can contain a vertical bar &lt;code&gt;|&lt;/code&gt; followed by additional Ex commands, or a double quote &lt;code&gt;"&lt;/code&gt; followed by comments. I'll need two different Ex commands in these new commands, so they will need to be separated with a vertical bar.&lt;/p&gt;

&lt;p&gt;Some of the other arguments you can supply to &lt;code&gt;:command&lt;/code&gt; are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-range&lt;/code&gt; allows the command can accept a range of lines in the current buffer to operate on.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-count&lt;/code&gt; allows you to supply a repeat count for the command.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-register&lt;/code&gt; allows you to specify a register other than the unnamed register.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more on the available &lt;code&gt;:command&lt;/code&gt; arguments look at &lt;code&gt;:h user-commands&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With the arguments defined for the new commands we end up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="p"&gt;:&lt;/span&gt;command &lt;span class="p"&gt;-&lt;/span&gt;nargs&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="k"&gt;file&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;bar Ref
&lt;span class="p"&gt;:&lt;/span&gt;command &lt;span class="p"&gt;-&lt;/span&gt;nargs&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="k"&gt;file&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;bar Vref
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All that is left is to create the actions to take when the commands are executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Command Action
&lt;/h3&gt;

&lt;p&gt;The Vim documentation actually calls this the replacement, because it replaces your command name with the command or commands you specify here. I find actions to be a bit more intuitive. In any case, this is what you want to have happen when you enter your command into Vim.&lt;/p&gt;

&lt;p&gt;The user command documentation doesn't say anything else about the actions, I suppose because all of the other documentation is applicable. Any Ex command can be used here to achieve the desired effect.&lt;/p&gt;

&lt;p&gt;For my case I want to open a new window to the right or bottom with the file specified in the command, but leave the current window active. The built in &lt;code&gt;:split&lt;/code&gt; and &lt;code&gt;:vsplit&lt;/code&gt; Ex commands don't have the behavior I want.&lt;/p&gt;

&lt;p&gt;I looked at the Vim documentation for &lt;code&gt;:split&lt;/code&gt;, thinking that perhaps there was an argument that would give me the behavior I wanted. It does take a couple of arguments, but they are related to the file format, and cursor positioning when the file opens. Not really what I was looking for.&lt;/p&gt;

&lt;p&gt;A little further down in the documentation for &lt;code&gt;:split&lt;/code&gt; I found the &lt;code&gt;splitbelow&lt;/code&gt; and &lt;code&gt;splitright&lt;/code&gt; settings. If you &lt;code&gt;:set splitbelow&lt;/code&gt; then new windows created with &lt;code&gt;:split&lt;/code&gt; are created below the active window instead of above. The same goes for &lt;code&gt;:set splitright&lt;/code&gt;; new windows will be created to the right.&lt;/p&gt;

&lt;p&gt;That looked like exactly what I wanted. I can set those values in my &lt;code&gt;.vimrc&lt;/code&gt; right before I define the commands and everything will work like I want.&lt;/p&gt;

&lt;p&gt;While that would work I wasn't in love with the solution. That will change the default for every split window. I don't like changing the default settings if I don't have to. A little more digging the the Vim documentation near the &lt;code&gt;:split&lt;/code&gt; docs and I found the exact right thing.&lt;/p&gt;

&lt;p&gt;There are command modifiers for the split window commands called &lt;code&gt;:rightbelow&lt;/code&gt;, &lt;code&gt;:leftabove&lt;/code&gt;, &lt;code&gt;:topleft&lt;/code&gt; and &lt;code&gt;:botright&lt;/code&gt;. These are Ex commands, but are called modifiers because they only take another Ex command as an argument and override the &lt;code&gt;splitbelow&lt;/code&gt; and &lt;code&gt;splitright&lt;/code&gt; settings.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;:rightbelow&lt;/code&gt; modifier just executes whatever command you supply as an argument. If the supplied argument opens a split window it will open it to the right, or below, depending on the argument. So, now the commands are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="p"&gt;:&lt;/span&gt;command &lt;span class="p"&gt;-&lt;/span&gt;nargs&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="k"&gt;file&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;bar Ref &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;rightbelow&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;split&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;:&lt;/span&gt;command &lt;span class="p"&gt;-&lt;/span&gt;nargs&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="k"&gt;file&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;bar Vref &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;rightbelow&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;vsplit&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The commands are almost complete. The only issue left to deal with is that the new window that is created is still made active. That is simple enough to fix, I can just add a window movement command to make the previous window active again.&lt;/p&gt;

&lt;p&gt;My initial thought was to use the Vim window movement commands; &lt;code&gt;&amp;lt;C-w&amp;gt;k&lt;/code&gt; to move to the upper window and &lt;code&gt;&amp;lt;C-w&amp;gt;h&lt;/code&gt; to move to the left window. Vim didn't parse that correctly though. Apparently all of the commands have to be Ex commands.&lt;/p&gt;

&lt;p&gt;I had to dig through Vim help topics for a bit but I found what I needed. All of the Vim window commands can be activated through the &lt;code&gt;:wincmd&lt;/code&gt; Ex command. So, whenever you type &lt;code&gt;&amp;lt;C-w&amp;gt;x&lt;/code&gt; you could also use &lt;code&gt;:wincmd x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The final commands, then, are as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="p"&gt;:&lt;/span&gt;command &lt;span class="p"&gt;-&lt;/span&gt;nargs&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="k"&gt;file&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;bar Ref &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;rightbelow&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;split&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;wincmd&lt;/span&gt; &lt;span class="k"&gt;k&lt;/span&gt;
&lt;span class="p"&gt;:&lt;/span&gt;command &lt;span class="p"&gt;-&lt;/span&gt;nargs&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="k"&gt;file&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;bar Vref &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;rightbelow&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;vsplit&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;args&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;wincmd&lt;/span&gt; &lt;span class="k"&gt;l&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There you have it; two brand new Vim commands. They take one argument, the path to a file, which can be &lt;code&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt; completed. They open the file  in a new split window, either to the right or below the currently active window and then move to the previously active window.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this little trip through Vim commands. There is so much flexibility available in Vim. With a little bit of effort and digging through the help files you can build your own Vim Commands to do pretty much anything you need.&lt;/p&gt;

</description>
      <category>vim</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Quick tmux Sessions</title>
      <dc:creator>David Lains</dc:creator>
      <pubDate>Tue, 15 Jan 2019 06:32:40 +0000</pubDate>
      <link>https://forem.com/dlains/quick-tmux-sessions-ko3</link>
      <guid>https://forem.com/dlains/quick-tmux-sessions-ko3</guid>
      <description>&lt;p&gt;I've been using &lt;a href="https://github.com/tmux/tmux"&gt;&lt;em&gt;tmux&lt;/em&gt;&lt;/a&gt; for a few years now. For every project I work on I create a &lt;em&gt;tmux&lt;/em&gt; script that will start up a development environment for that project.&lt;/p&gt;

&lt;p&gt;For a long time the &lt;em&gt;tmux&lt;/em&gt; scripts I created just opened windows to run the various servers that the project required and nothing else. For a Rails project it would typically be a window for the &lt;a href="https://rubyonrails.org/"&gt;Rails&lt;/a&gt; Server, another window for &lt;a href="https://redis.io/"&gt;Redis&lt;/a&gt;, one for &lt;a href="https://sidekiq.org/"&gt;Sidekiq&lt;/a&gt; and sometimes one for &lt;a href="https://mailcatcher.me/"&gt;Mailcatcher&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It was very handy to just run a script to have the whole environment for a project up and running, but I typically didn't interact much with the &lt;em&gt;tmux&lt;/em&gt; session after that. Just checking server output from time to time. The rest of the time I was in &lt;a href="https://atom.io/"&gt;Atom&lt;/a&gt; or the browser.&lt;/p&gt;

&lt;p&gt;When I started using Neovim I began to spend more time in &lt;em&gt;tmux&lt;/em&gt;. I updated my project specific &lt;em&gt;tmux&lt;/em&gt; scripts to include an &lt;em&gt;editor&lt;/em&gt; window along with the existing server windows.&lt;/p&gt;

&lt;p&gt;I got so used to this way of working that I would find myself creating mini &lt;em&gt;tmux&lt;/em&gt; sessions with just an &lt;em&gt;editor&lt;/em&gt; window and a &lt;em&gt;console&lt;/em&gt; window whenever I needed to do any kind of quick editing work outside of a full project. It is easy to setup a simple two window session, but why repeat the same process over and over?&lt;/p&gt;

&lt;p&gt;Here is a short shell script that will create a simple two window &lt;em&gt;tmux&lt;/em&gt; session given a directory path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;basename&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;
tmux new &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="nv"&gt;$NAME&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; editor &lt;span class="nt"&gt;-d&lt;/span&gt;
tmux send-keys &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nv"&gt;$NAME&lt;/span&gt; &lt;span class="nv"&gt;$EDITOR&lt;/span&gt; C-m
tmux new-window &lt;span class="nt"&gt;-n&lt;/span&gt; console &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nv"&gt;$NAME&lt;/span&gt;
tmux &lt;span class="k"&gt;select&lt;/span&gt;&lt;span class="nt"&gt;-window&lt;/span&gt; &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nv"&gt;$NAME&lt;/span&gt;:1
tmux attach &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nv"&gt;$NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is just the main part of the script, the full script is commented and checks the input parameters. You can see the actual script &lt;a href="https://gist.github.com/dlains/b28690d9975bec72c811b1824bfe3ee3"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The script expects a single argument, a path to a directory. It extracts the final directory name in the path into the &lt;code&gt;NAME&lt;/code&gt; variable. It uses this name as the &lt;em&gt;tmux&lt;/em&gt; session name throughout the script.&lt;/p&gt;

&lt;p&gt;After extracting the name the script changes directory to the provided path. After that it creates a new &lt;em&gt;tmux&lt;/em&gt; session, and names the first window &lt;em&gt;editor&lt;/em&gt;. Once the session is created the script sends a command to the &lt;em&gt;tmux&lt;/em&gt; session to start the editor associated with the &lt;code&gt;EDITOR&lt;/code&gt; environment variable. On my system that is set to &lt;code&gt;nvim&lt;/code&gt;, but it could be any editor that can be started from the command line. Be aware, though, if you start a GUI based editor it will open outside of the Terminal and there won't be much use for this &lt;em&gt;tmux&lt;/em&gt; editor window.&lt;/p&gt;

&lt;p&gt;Now that the first window is created, named and the editor is running the script creates the second window. It simply names it &lt;code&gt;console&lt;/code&gt;. I find it helpful to have a dedicated console window for working with Git or examining files or other tasks. Some of these tasks can be done in Neovim, but I like having a dedicated console window for some tasks.&lt;/p&gt;

&lt;p&gt;Once the &lt;code&gt;console&lt;/code&gt; window is created the script tells &lt;em&gt;tmux&lt;/em&gt; to select the &lt;code&gt;editor&lt;/code&gt; window and then attaches to the session.&lt;/p&gt;

&lt;p&gt;And there it is. One simple script and you can have a mini &lt;em&gt;tmux&lt;/em&gt; session running in a matter of seconds. I named the script &lt;code&gt;qs&lt;/code&gt;, for quick session, and put it in my path. Now whenever I need to do a little bit of work, for instance in my &lt;em&gt;dotfiles&lt;/em&gt; project, I can simply run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;qs ~/dotfiles
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you would like to use the script yourself just copy the code to a file, make sure the file is executable with &lt;code&gt;chmod +x [file]&lt;/code&gt; and make sure your &lt;code&gt;EDITOR&lt;/code&gt; environment variable is set to something appropriate, or change the &lt;code&gt;$EDITOR&lt;/code&gt; reference in the script to whatever you like.&lt;/p&gt;

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