Wednesday, April 20, 2016

Tmux Comfortable Shortcuts

Working with tmux is all about short cuts.  To find out existing shortcuts in tmux, you can use
$ tmux list-keys
Usually tmux key configs are stored in ~/.tmux.conf file. By modifying this file, you can override these commands. Tmux has a prefix key which is ctrl + shift + b which is the default. But we can change it to an ergonomically comfortable position. Lets see how we can modify tmux.conf file. Following are some notations used in this file.

C    means ctrl
M   means alt
C-"    means ctrl + shift + "
M-Down means alt + down arrow key
-n means you can run this command without tmux prefix

Note that mouse mode is also enable in following file. You can scroll up through the pane.
If you want to select and copy text, then while holding select the text, relese shift copy the text.

# Tmux should be pretty, we need 256 color for that
set -g default-terminal "screen-256color"


# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

# change prefix
bind-key -n C-q detach

# split panes using right and down
bind -n M-Right split-window -h
bind -n M-Down split-window -v
unbind '"'
unbind %

# switching between sessions
bind -n C-] switch-client -p
bind -n C-[ switch-client -n
unbind )
unbind (

# switch panes using Alt-arrow without prefix
bind -n C-Left select-pane -L
bind -n C-Right select-pane -R
bind -n C-Up select-pane -U
bind -n C-Down select-pane -D

# Enable mouse control (clickable windows, panes, resizable panes)
set -g mouse-select-window on
set -g mouse-select-pane on
set -g mouse-resize-pane on


# command delay? We don't want that, make it short
set -sg escape-time 1

# Set the numbering of windows to go from 1 instead
# of 0 - silly programmers :|
set-option -g base-index 1
setw -g pane-base-index 1

Monday, April 18, 2016

Linux Command Line - Mean Stack

This article is going to be about setting up a development environment on a Linux machine. Have you ever watched a coding tutorial and saw a really slick editing interface with terminal split screens and a gorgeous color scheme? Well, I am not only going to show you how to configure and setup something impressive but I will also give you the insight into how to configure it as you see fit.

All the codes mentioned here are linux terminal commands and scripts.

Mean Stack

Let’s talk about the tools stack are going to be using.

Zsh

When you open up a terminal on your machine, you are most likely going to find that you are running a Bash shell.

Bash is a nice default but we’re not here for defaults, we’re here for the best we can get and that means Zsh and all the bells and whistles that come along with it. In a nutshell, Zsh gives us a much more user friendly experience on the command line, it gives us things like better auto-completion, Vim key bindings and smart guesses when you write a command wrong. Zsh has a huge user community behind it and those smart people have added many, many customisations. We can discover those as we go.


Tmux

Tmux is a terminal multiplexor. Sounds scary right? Well, it isn’t at all. Tmux is a tool that allows you to split your terminal screen into any layout you want as well as run multiple terminal instances that are easily accessible. Tmux has become the go-to tool for some of the biggest neckbeards out there.

Vim

Vim is a text editor and if you haven’t heard of it before, well, you’re either going to love it or hate it. I do hope you will love it of course as it is an extremely versatile, customisable and powerful text editor. Now, for the sake of diversity, there is another extremely powerful text editor out there called Emacs which I would suggest you look into in your spare time and decide whether you would like to learn it. Yes, I said learn, Vim and Emacs have an extremely steep learning curve in terms of how they allow you to manipulate text. I chose Vim and I will be providing links, tips and customisations to get you where you need to be ASAP.

Here is a preview of the configuration you will end up with after configuring this mean stack



















Prerequisites and Assumptions

A final comment before diving in head first is that I am making some small assumptions about your setup. They are the following:

You’re computers operating system is Debian based.

‘Debian based’ in simple terms simply means that you are using Ubuntu or a variant like Kubuntu or Lubuntu. The reason for this is that we need to be using the same package manager. Also, things are a lot more user friendly on the Ubuntu side of life.

You need some things installed to get up and running. Here are the prerequisites:

Git

You need to install git before you continue with this as git will be the primary source of packages, plugins etc. that we will be using as well as you will be using when you need to customize this. Follow the command to install git.

sudo apt-get install git


Zsh

Let’s get started. We’re going to install Zsh and kit you out with a decent terminal. Run

$ sudo apt-get install zsh


You now need to switch your default shell from whatever you have now to Zsh, Run:

$ chsh -s /bin/zsh

Close that terminal and open another and you should be looking at a lovely new Zsh prompt. We’re not done yet, remember all those customization I talked about? In order to give us a head start let’s get the community managed Zsh framework called oh-my-zsh. Let’s get it installed.


$ git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh


Now that we have oh-my-zsh installed, we can customise as much as we want, let’s start by setting up our .zshrc. The .zshrc is a configuration file that is read by Zsh every time you open up a new terminal session. In other words, we can put all our customization in this file and it will be run in the background for us. Let’s create that file:

# take us back to our home directory
$ cd

# create an empty file called '.zshrc'
$ touch .zshrc

Now, I am going to assume you don’t have Vim installed yet, so we can use a simple text editor that comes as default: nano.

$ nano .zshrc

You can type in text simply like with any text editor you are used to. Let’s start off by choosing a theme. Head over to oh-my-zsh themes Choose one you like (take a look at “3den”, that is the one I use), and then add the following line to your .zshrc

ZSH_THEME="3den"

Save the file by hitting Ctrl-x, and then y to confirm that you want to write to the file. Close that terminal and open another up. Brilliant, you should have a new and shiny prompt. I’ll take this moment to point out why I use the 3den theme. 3den gives me Git and Ruby information on the command line. It also gives me my current working directory and the time. The colors aren’t that bad either. Feel free to experiment with many different themes!

When you want to customise you Zsh installation, you’ll mostly be working in your .zshrc file. I am going to list some more things you might want to include in your .zshrc, feel free to test them, use them or not. Remember, this is your setup!

# want your terminal to support 256 color schemes? I do ...
export TERM="xterm-256color"

# if you do a 'rm *', Zsh will give you a sanity check!
setopt RM_STAR_WAIT

# allows you to type Bash style comments on your command line
# good 'ol Bash
setopt interactivecomments

# Zsh has a spelling corrector
setopt CORRECT

That is enough for now, we’ll come back to add more later on. Let’s move onto Tmux.

Tmux

Grab Tmux with the following:
$ sudo apt-get install tmux

Now, Tmux can be a tricky one to figure out at the beginning, but bear with me, we will get there. I have customised my installation with the help of the very great book Tmux – Productive mouse-free development which I totally recommend. Let’s not bore you with the defaults, (although you can find them here) go ahead and create the Tmux configuration file, called .tmux.conf:

$ cd
$ nano .tmux.conf

Add the following lines:

# set Zsh as your default Tmux shell
set-option -g default-shell /bin/zsh

# UTF is great, let us use that
set -g utf8
set-window-option -g utf8 on

# Tmux should be pretty, we need 256 color for that
set -g default-terminal "screen-256color"


# Tmux uses a 'control key', let's set it to 'Ctrl-a'
# Reason: 'Ctrl-a' is easier to reach than 'Ctrl-b'
set -g prefix C-a
unbind C-b

# command delay? We don't want that, make it short
set -sg escape-time 1

# Set the numbering of windows to go from 1 instead
# of 0 - silly programmers :|
set-option -g base-index 1
setw -g pane-base-index 1


# Allow us to reload our Tmux configuration while
# using Tmux
bind r source-file ~/.tmux.conf \; display "Reloaded!"

# Getting interesting now, we use the vertical and horizontal
# symbols to split the screen
bind | split-window -h
bind - split-window -v

OK, Ctrl-x to save the file and then run:

# the '-s' flag specifies a name (we use to attach to it later on)
$ tmux new -s myfirsttmux

Great, you see your lovely Zsh terminal, now let’s play around with those bindings you set in your .tmux.conf. Try and run them all in sequence and you will have gotten the basics of Tmux in a few minutes!

# split the screen in half
Ctrl-a |

# jump over to the right hand split
Ctrl-a 

# split that right hand side pane in half
Ctrl-a -

# jump down to that lower pane
Ctrl-a 

# Close the window
Ctrl-d

# Open up another window
Ctrl-a c

# Go to the next window
Ctrl-a n

# disconnect from Tmux
Ctrl-a d

# Check what Tmux sessions are running
$ tmux ls

# Attach back into the session
$ tmux attach -t myfirsttmux

# Escape and kill session
Ctrl-a d
$ tmux kill-session -t myfirsttmux


OK, lots of information there but you get the gist of it, You can split vertically and horizontally to your hearts content! You also have the ability to reload your Tmux configuration file whilst still in a Tmux session, so you can add a line, then hit

Ctrl-a r

You have your new configuration options loaded. Now, I mentioned I would be leaving a link to a fully customised configuration file, and I am going to keep my promise, check out my personal tmux config file . Remember, don’t just drop it all in at once, incrementally add options and test them by reloading your config!

Close Tmux, get back to a vanilla terminal and let’s move on. You’ve got the basics here.

Next, it will be Vim, the Big one. I will publish about it in my next article.