[go: up one dir, main page]

DEV Community

Cover image for Building a Custom IDE with Tmux
Boris Jamot ✊ /
Boris Jamot ✊ /

Posted on • Edited on • Originally published at mamyn0va.github.io

Building a Custom IDE with Tmux

Hi!

Today, I want to share about a tool I've been using for a few months and which helps me a lot in my day to day job: tmux.

As a software craftsman and CLI lover, I'm always looking for the best tools to be as productive as possible.

Let me show you how I use tmux as the foundation of what I call my custom IDE.

Why Tmux?

Tmux is a wonderful terminal multiplexer that comes with some plugins.

You might wonder why you would need to use such a thing. Many terminal emulators come with built-in feature for splitting your term.
In fact, it comes that tmux is much more powerful for splitting and resizing windows.
The other reason is that tmux can work with any term. So if you change your term, you don't need to reconfigure all your presets. Tmux does it for you.
And you can also use tmux without X, in a simple tty.

GitHub logo tmux / tmux

tmux source code

Welcome to tmux!

tmux is a terminal multiplexer: it enables a number of terminals to be created accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.

This release runs on OpenBSD, FreeBSD, NetBSD, Linux, macOS and Solaris.

Dependencies

tmux depends on libevent 2.x, available from this page.

It also depends on ncurses, available from this page.

To build tmux, a C compiler (for example gcc or clang), make, pkg-config and a suitable yacc (yacc or bison) are needed.

Installation

Binary packages

Some platforms provide binary packages for tmux, although these are sometimes out of date. Examples are listed on this page.

From release tarball

To build and install tmux from a release tarball, use:

./configure && make
sudo make install
Enter fullscreen mode Exit fullscreen mode

tmux can use the utempter library to update utmp(5), if it is…

Install it

Install tmux through your favorite package manager, or compile it from the sources:

$ git clone https://github.com/tmux/tmux.git
$ cd tmux
$ sh autogen.sh
$ ./configure && make
Enter fullscreen mode Exit fullscreen mode

Configure it

Tmux may look a bit raw and difficult to handle at the beginning. Fortunately, it comes with a great community that helps a lot to set it up. After trying many configurations, I've ended up with the one of gpakosz:

tmux

It comes with presets for bindings and a great powerline look for the status bar.

Just have a loot at the README to customize it to your needs!

Personally, I find it useless to have the uptime in my status bar, so I removed it from the configuration. I also added some plugins:

Use it

You have to set tmux as your default shell in your preferred terminal emulator. Once done, tmux will be launched each time you open a new term.

Basically, when tmux starts, it creates an empty session with a single window containing a blank pane and a status bar. The status bar is divided in 3 parts: left, middle and right. If, like me, you choose to use the configuration above, the left part will display the session's name (or index if name is unset) and the uptime. The right part will display the battery, the date, the username and the hostname. In the middle, you'll see the windows' titles (or the name of the process running in the focused pane if the name is unset).

The basics are sessions, windows and panes. Each time you open a term (or a new tab in the term), tmux launches a new session. A new session contains a window. A new window contains a pane in your $HOME dir.

Bindings in tmux are essential: you can access them by using the prefix Ctrl-a or Ctrl-b.

Here are the main bindings I use:

  • <prefix> Ctrl-c creates a new session
  • <prefix> c creates a new window
  • <prefix> % splits the current pane vertically
  • <prefix> " splits the current pane horizontally

Now, if you feel uncomfortable with bindings, tmux has a great mouse mode, allowing you to select, switch and resize panes.

Get the most of it

The killer feature of tmux is the sessions. Imagine you want to build a window with 3 panes for editing a file, play with git and run your tests. Just create a new session, split the panes and adjust their size to your needs.

Great? Not that much.

As there is no built-in mechanism to save session, I use the tmuxp tool. This is a must-have session manager for tmux.

Install it through your distro's manager or:

$ pip install --user tmuxp
Enter fullscreen mode Exit fullscreen mode

In two words: tmuxp allows you to easily create sessions from yaml/json files and to load 'em when you need it. And it's not only about creating the windows & panes layout: you can also run commands in each of the panes. So you can run vim in the main pane, cd to your project directory in another pane to play with git, and run a file-watcher in a 3rd pane to trigger your tests:

# my-project.yaml
session_name: my project
windows:
- window_name: my custom IDE
  layout: main-vertical
  shell_command_before:
    - pj my-project
  panes:
    - vim
    - git status
    - phpunit-watcher watch
Enter fullscreen mode Exit fullscreen mode

Then just run tmuxp load -y my-project.yaml.

And if, like me, you need to build up a full environment to work on your app, know that there is no limit. The session I use for my current project has many windows:

  • 1 IDE window: 3-panes with vim, a term to play with git and a term to run the tests,
  • 9 windows, one for each log file I need to grep with jq or lnav,
  • a window to serve the assets of my front,
  • a window to run my stubs (typically a SpringBoot app).

That's it!

I hope you learned something by reading this article and that you'll have a look at tmux. I'm not an expert of it. I must say I don't use 10% of the features, but this tool has become an essential part of my dev env. I guess it could do the same for you.

Thanks for reading.

Bye.

Top comments (11)

Collapse
 
coreyja profile image
Corey Alexander

I've been using tmux for a bit but I hadn't thought to set it to launch automatically! Seems so obvious now in retrospect 😆

Thanks for the tip! Now I won't find myself accidently without my status line!

Collapse
 
biros profile image
Boris Jamot ✊ /

Yes, and now you can version your .yaml file and share it with your team mates !

Collapse
 
vintharas profile image
Jaime González García

Thank you for writing about this! I've had tmuxinator/tmuxp on my TODO list for ever and your article pushed me over the edge to stop using one session for everything and start creating reusable dev environments with tmuxp :D

Thank you!

Collapse
 
biros profile image
Boris Jamot ✊ /

Happy you found it helpful 😊

Collapse
 
zeljkobekcic profile image
TheRealZeljko

Was looking at your tmux.conf last weekend to improve mine and now you wrote an article about it. Nice stuff (mostly tmux-yank and tmuxp) :)

Collapse
 
biros profile image
Boris Jamot ✊ /

Thank you!

Collapse
 
tsouhaieb profile image
tsouhaieb

great work, would you please share the config to show the shell like in the screenshot above (with the icons and all ) ?

Collapse
 
biros profile image
Boris Jamot ✊ /

It's spacefish.

Collapse
 
cdrani profile image
Charles M Drani

Great post! I've actually been using that same configuration by gpakosz. I've also started writing little scripts to place on the status line to display the current song playing, etc.

Collapse
 
rafaqueiroz profile image
Rafael Queiroz

nice article. I used tmux for some time and I always wonder if there was a way of saving the sessions, but I never look it up for some solution. I will try this tmuxp and see how it goes.

Collapse
 
biros profile image
Boris Jamot ✊ /

It's quite straightforward. You can also have a look at tmuxinator.