Pair Programming
Impressive Ruby Productivity with Vim and Tmux

Summarized using AI

Impressive Ruby Productivity with Vim and Tmux

Chris Hunt • February 21, 2013 • Earth

In this engaging presentation at the LA RubyConf 2013, Chris Hunt details how Vim and Tmux can significantly boost Ruby developers' productivity, showcasing their capabilities within a terminal environment.

Key Points Discussed:

  • Introduction to Chris Hunt: Formerly with Square, Chris now works with CameraBites and shares his enthusiasm for utilizing terminal-based tools in Ruby development.

  • Introduction to Tmux:

    • Tmux is a terminal multiplexer, allowing developers to manage multiple terminal sessions. Users can create, navigate, and customize windows and panes within a single Tmux session, improving workflow by organizing various terminal applications.
    • Chris demonstrates how to install Tmux using Homebrew and creates a sample session named "LA RubyConf," showcasing Tmux's status bar and the ability to customize information displayed.
  • Utilizing Vim with Tmux:

    • Vim, a powerful terminal-based text editor, complements Tmux. Chris highlights useful Vim plugins for Ruby on Rails development, including:
    • CtrlP: A fuzzy file finder for quick navigation.
    • Ag (The Silver Searcher): For searching through files based on keywords.
    • Surround Plugin: To easily manipulate surrounding characters.
    • Tabular: For aligning code vertically for readability.
  • Test-Driven Development (TDD) with Vim:

    • Chris illustrates a workflow where developers can write tests in Vim while easily switching between files and running tests without exiting the editor.
    • He describes a practical example where he creates a 'sum' method, detailing the iterative process of writing and testing code.
  • Collaboration through Tmux:

    • Chris introduces the concept of remote pair programming with a public Tmux session, allowing multiple collaborators to work on the same problem simultaneously.
    • This includes sharing code, rerunning tests, and facilitating an agile development process.
  • Advanced Features:

    • The session concludes with Chris discussing the use of an 'arglist' in Vim, which allows developers to load multiple files into a list for easier batch operations, enhancing efficiency when making global changes across files.

Takeaways:

  • Vim and Tmux are powerful allies for Ruby developers, enhancing productivity by allowing a seamless workflow entirely within the terminal.
  • The integration of various plugins and features exponentially boosts the capabilities of both tools, making complex tasks simpler.
  • Engaging with these tools fosters collaboration, especially in remote settings, making them essential for modern development practices.

Chris invites questions at the end, encouraging attendees to explore and adopt Vim and Tmux in their workflows, positioning them as essential tools for any Ruby developer.

Impressive Ruby Productivity with Vim and Tmux
Chris Hunt • February 21, 2013 • Earth

Impress your friends, scare your enemies, and boost your productivity 800% with this live demonstration of vim and tmux. You will learn how to build custom IDEs for each of your projects, navigate quickly between files, write and run tests, view and compare git history, create pull requests, publish gists, format and refactor your code with macros, remote pair program, and more, all without leaving the terminal. Come prepared to learn and ask questions; this is serious business.

Help us caption & translate this video!

http://amara.org/v/FGc3/

LA RubyConf 2013

00:00:24.560 Up next is Chris Hunt, formerly of Square, now of CameraBites. He has lived in various places, including Seattle and San Francisco, but he is now a resident of Portland. If anyone is from that area, please introduce yourself so he can figure out what to do there, as he has never been to Portland before. Today, Chris is going to talk about Vim and Tmux, especially why you should try them for Ruby development.
00:01:01.840 Thanks! Hello, my name is Chris. As mentioned, I just moved to Portland, Oregon, as of Saturday, and I'm excited to be here. I want to engage with you today about Tmux ('T-mux') and Vim. First, I want to get a sense of the room. Is anyone here using Tmux? Not Vim, but Tmux? Cool. Now, who is using Vim? Okay, way more people are using Vim. That's great! If you are using Emacs or some other terminal-based text editor, I’m curious to know.
00:01:50.079 Let’s get started! Feel free to leave your laptops open if you want to mess around, as that’s totally fine with me. Also, later, you'll want to have your laptops open, so think about that too.
00:02:02.240 The first thing we'll do is talk about Tmux. If you haven't been using it, you'll need to install it. Looks like most of you are on Mac. You can use Homebrew, so the command is 'brew install tmux.' I already have it installed, so it will give me a warning when I run it, but that command will install it for you.
00:02:26.040 Next, we want to create a Tmux session. Tmux works as a server running on your machine, while your terminal acts as a client. You can connect as many clients as you want. So why might you want to use Tmux as a Ruby developer? We already use a ton of terminal-based applications—Git, Bundler, RVM, Rake, Rails console, database consoles, and SSHing into various machines. Essentially, we live in the terminal, so it makes sense to have a text editor that's also based in the terminal. That’s where Vim comes into play.
00:02:49.360 Using Tmux lets you manage all those applications more effectively. It allows you to create sessions for your applications, holding all their services together. Plus, you have windows and panes with other organizational tools. When we start a Tmux session, it will look like this: we can use the command 'tmux new-session' or just 'new' for short. We'll name this session 'LA RubyConf.' Cool!
00:03:07.560 If you look at the bottom corner of the screen, you'll see the status bar with the session name. In the right-hand corner, there are other indicators, such as your hostname, date, and time. This status bar is completely configurable, which means you can display anything you can run in the terminal.
00:03:41.880 You can put sports scores, weather information, CI status—whatever you prefer! I like to keep it simple down here. Tmux provides a help menu that lists all your keyboard shortcuts, including custom shortcuts you can configure. If you want to see my Tmux configuration with this simple status bar and other hotkeys, it's available on GitHub. I'll give you a moment to jot down that link because I don’t have slides to upload later.
00:04:25.160 Now, let’s see how Tmux works. If you look in the bottom corner, you'll notice we have one window. We can create new windows; for example, we could now have four different windows open. We can also rename our windows, which I will do now. I'll name this window 'hello' and switch back to our first window to call it 'world.' The state of each window is saved, allowing us to easily switch back and forth.
00:04:37.520 Inside a window, we can create multiple panes, and we also have multiple sessions, where we can reopen everything again. The beauty of using Tmux is that you don’t have to use keyboard shortcuts; you can also use the Tmux command to perform tasks. For example, in a new window, if I type 'tmux split-window', it splits the window. While that might seem silly because you can just use keyboard shortcuts, it allows you to script everything Tmux can do. If you have multiple Rails projects running different commands, you can script that behavior.
00:05:30.920 Let’s play with a script now. I’ll detach from this session, and you’ll notice our status bar disappears—we’re no longer in Tmux. Let me jump into a Rails app titled 'my awesome app,' and I'll show you what a Tmux script looks like. The first thing it'll do is create a new session called 'my awesome app.' In that session, it will create a new window called 'server' and start our Rails server for us.
00:06:00.640 It will also create another window called 'vim' and start Vim. Finally, it will attach to that session so we can see it in action. Let's quit out of this and run the Tmux command to execute it: 'tmux attach'. Looking at the corner of the window, we see our awesome app and our three panes: the first pane is the terminal session, the second is the Rails server running, and the last pane is Vim.
00:06:39.360 We can view our session list, where our first session still exists. We can either go back to it or to the second session we started. Pretty cool! Admittedly, I never really use this behavior to script Tmux. I prefer to have just one session with all my windows within it.
00:07:04.440 In iTerm 2, there's a useful feature in the preferences under profiles. It allows you to send text as soon as your terminal starts, which is handy. I like to start a specific session. If that session already exists, it’ll automatically attach me to it, allowing me to resume where I left off. If you are like me and frequently quit iTerm accidentally, using this feature means you'll never lose your work as it's always running in the background.
00:07:12.360 Now that we’re back in Vim, let’s talk a little bit about some Vim plugins. There are several plugins I like to use that help me with Ruby on Rails development. Since we’re working in a Rails application, let’s look at some of these useful plugins.
00:07:36.160 First off, Rails has a ton of files, so we need a nice way to search for them. I use a plugin called 'CtrlP,' which pulls up a fuzzy search menu. Anything you type will narrow down the list of files shown. For example, if I type 'A' for 'application' and 'C' for 'controllers', it will quickly lead me to the application controller file.
00:08:03.879 The next useful plugin is for searching within files. While we know how to find files, the next helpful plugin I use searches across files to find something like where a particular method or class is defined. The plugin 'Ag', or 'The Silver Searcher', works similarly to grep but has additional configuration options.
00:08:31.760 In Vim, all the commands you send are at the bottom of the window. I'm going to use 'Ag' now to search for terms within my Rails application. Let's see what results we get when searching for 'my awesome application.' We will see all the results in one pane, where we can look at them and open the relevant files quickly.
00:09:06.480 Before we move on, let's open my Gemfile. It contains dependencies for my Rails application. As you can see, all entries in this file are surrounded by single quotes, except for the sqlite3 gem, which has double quotes. This quirk bothers me, and to resolve such issues, there's a handy plugin called 'surround', which allows us to quickly change surrounding characters.
00:09:11.760 For example, to convert double quotes to single quotes, the process is straightforward. Similarly, surround allows alterations for parentheses, brackets, tags, etc. For instance, I can easily switch the surrounding characters from double to single quotes like so.
00:09:48.359 Another useful tool is 'tabular,' which aligns items vertically based on specified criteria. In my Gemfile, I often align hash rockets for improved readability. When I run 'tabular', I can match it with 'git' characters, and it will align them accordingly. This proves particularly valuable when managing multiple lines of code.
00:10:30.360 With this explored, let’s detach from the session and shift focus to test-driven development in Vim. Running tests within Vim can be seamless; we never need to leave Vim to perform updates and can switch between files effortlessly. We’re going to add a 'sum' method to Ruby's standard math library.
00:11:06.880 First, I’ll create two directories: 'lib' for the code, and 'spec' for the tests, alongside a basic Gemfile. This file should only require 'rspec.' Now, let's run 'bundle' to install our testing dependencies, ensuring we can execute tests directly in Vim.
00:11:47.880 Once we execute 'bundle', Vim might send its process to the background, asking for user input. Pressing 'enter' returns control to Vim, allowing us to resume testing. Now, let's write our test in 'spec/math_spec.rb', describing our 'sum' method and verifying its functionality across various input scenarios.
00:12:32.520 After constructing our tests, let’s run them. When we execute the command, you’ll notice a failure due to our mathematical library not being defined yet, thus prompting us to create it. We can reopen 'math.rb', and with this file created, we can provide our implementation for the 'sum' method.
00:13:14.840 Now, let's create a simple Ruby module, 'Math', and extend its functionality to include a class method for 'sum'. As soon as we define this method to return the expected result, we can run our tests again. We see a new failure indicating a mismatch, but this triggers effective iteration until our tests now pass.
00:13:52.840 At this point, we want to act efficiently, and if we run into code-writing issues, it's beneficial to create a Gist directly from Vim. I'll open the relevant spec file, add details, and then execute a Gist command to share my challenge with someone else for assistance.
00:14:14.560 So, I’ve just created a Gist, and if anyone has a better solution, they might want to contribute their suggestions. My aim is to gather peer feedback and leverage collaborative efforts to finalize this task.
00:14:56.360 Now, to incorporate a more collaborative coding environment, let’s create a shared Tmux session. I will generate a new session that's publicly accessible, allowing collaborators to join. Once they do, we will both be able to work synchronously on solving our issue.
00:15:26.480 Then, I will set it up so they can find the session using SSH and, upon connecting, use the same 'tmux attach' command to join in. It's a streamlined process, allowing us to interact whilst working on solutions together.
00:16:05.479 After this setup, not only can I view the code, but I can also rerun tests, allowing us to validate every solution in real-time. The flow of adjustments keeps us agile, rapidly iterating until we achieve a successful outcome.
00:16:35.479 Alas, one more tool I want to showcase is how to integrate 'arglist' in Vim for efficient file operations. Each file used in a project can be loaded into an 'arglist', forming an actionable collection—leading to swift modifications. I could utilize functionalities such as searching and replacing across multiple files by simply querying the arglist and modifying the parameters accordingly.
00:18:15.320 If I decide to update names in all files, the execution would seem intricate, yet simply iterating using 'ARG do' makes this process less daunting. Thus, understanding these methods enable quicker adjustments, reducing repetitive tasks significantly.
00:18:55.080 To conclude the demonstration, I'll take questions, inviting everyone curious about Vim or Tmux to share. Addressing inquiries strengthens our collective grasp on productivity tools, fostering insightful revelations.
00:19:38.240 Thus concludes my session, and I appreciate your engagement today. Remember, Vim and Tmux can supercharge your development workflow. Don’t hesitate to explore further and implement what resonates with your practices.
Explore all talks recorded at LA RubyConf 2013
+6