Tuesday, February 18, 2014

Terminal adventures

I do a lot of work remotely for work. Somewhere in the server room there is a beast of a computer with 100's of GB of RAM and a 10-gigabit connection to a dozen other computers hosting half a petabyte of storage.

Mostly I work in MATLAB. If I need graphics, we have a nice OpenNX setup that does remote desktop quite well. It's nearly seamless when I'm on-site, and is still tolerable when I use it from home over DSL (as long as nobody else at home is using the upload capacity).

But often I don't need full graphics, just a text editor and the MATLAB command window. So in today's adventure, we are going to try setting up tmux and vim.

tmux


tmux is a terminal multiplexer; basically a modern version of screen. To be honest, I was pretty happy with screen, but everybody says this is better, so let's see what all the fuss is about.

The computer I'm logging onto runs Red Hat, so:

sudo yum install tmux

Side by side


Start tmux and run MATLAB

tmux
matlab

Split left/right (split-window -h)

Ctrl+b %

Resize the split  (if your local machine is a Mac, Alt = Shift+Option)

Ctrl+b Alt+[right]

Switch between panes (last-pane)

Ctrl+b ;

View history, i.e. scroll up/down

Ctrl+b Shift+PageUp

Reconfiguring tmux


Ctrl+b happens to be "page-up" in vi, so I wanted to change its binding. The natural choice is Ctrl+a, but that's "start of line" in emacs. I'm also a little weird in that I use a Dvorak keyboard layout, which makes conventional keyboard shortcuts rather cumbersome.

So my first choice was Ctrl+; (semicolon), which is nice because it's unused and conveniently located (where the Z key is). Unfortunately, terminal emulators do not like sending Ctrl with anything except letters and a few punctuations.

Next I tried Ctrl+m, which is unused and not too bad to get to. Boy, that was a mistake. After ~20 minutes of head-scratching, I discovered that Ctrl+m is actually used quite frequently! Ctrl+m is a carriage return, i.e. the [Enter] key is bound to Ctrl+m. I hate computers.

This is what I ended up with:

unbind C-b
set -g prefix C-t
bind C-t send-prefix
bind h last-pane

vim


This is what goes into my ~/.vimrc

Some standard "nice" things:

set nocompatible
set backspace=2
set history=1000
set laststatus=2
set wildmenu
set showcmd
set ruler
set ttimeout
set ttimeoutlen=50

Was having color issues because vim was deciding that tmux could only show 8 colors instead of 16

if &t_Co==8
  set t_Co=16
endif

Enable syntax highlighting and line numbers

syntax on
set number

Use MATLAB-style tabs

set softtabstop=4
set shiftwidth=4
set expandtab

Let the left/right arrows go across lines (instead of stopping at the start/end of the line)

set whichwrap+=<,>,h,l,[,]

Remap ijkl to act as arrow keys and IJKL for jump words and jump 10 lines. But do it in Dvorak.

nnoremap c k
nnoremap t j
nnoremap n l
"coincidentally, j in Dvorak is h!
nnoremap C 10k
nnoremap T 10j
nnoremap H B
nnoremap N E

I'm not to concerned about losing the other shortcuts, but n/N (find next/find previous) are nice, so let's remap those:

nnoremap b n
nnoremap B N

And let's use emacs-style shortcuts in insert mode:

inoremap <C-a> <esc>0i
inoremap <C-e> <esc>$a
inoremap <C-k> <esc>ddi
inoremap <C-y> <esc>Pi

And in normal mode, too, why not

nnoremap <C-a> 0
nnoremap <C-e> $
nnoremap <C-k> dd
nnoremap <C-y> P

For syntax highlighting, I used the syntax file from MatlabFilesEdition, haven't tried out the other stuff in that package (matchit, indentation, etc). I'm not used to the highlighting of the MATLAB functions, so I disabled that by commenting out the hi link line for matlabFunc.

No comments:

Post a Comment