Vim + Tmux
How to make Vim your IDE for cross language development
By Nick LaMuro
Vim + Tmux
-
What to expect
-
Vim
- What is Vim?
- Why Vim?
- Demo
-
Tmux
- What is Tmux?
- Why Tmux
- Demo
-
Vim + Tmux
- What you can do to leverage the two
- Demo
What to Expect
What to expect from this talk
-
I will explain what vim is and how to use it (breifly…)
- Configuration, basic usage, common actions
- Plugins, how to manage them, and how to write your own
-
I will explain what tmux is and how to use it (breifly…)
- What tmux is
- Configuration and basic usages
- Plugins/Wrappers
-
I will explain how to use the two together (sortof…)
- Vimux
- Thats about it…
What to expect from me
-
I am terrible (okay… probably ‘inexperienced’) at giving talks
- so set your expectations low
-
I am not going to indoctrinate you to use these tools (for the most part…)
- I want this talk to show you why I use this setup
- Describe pros and cons
-
I am not going to bash your current setup
- Okay, that probably isn’t true
What I expect from you
-
Basic Unix/Linux terminal usage
- I don’t have time to explain this
- Tmux is not something you can use on windows, sorry.
- (But hey, linux doesn’t cost anything, plus you can use linux VM’s in windows)
-
SILENCE!!!
- Nah, just kidding.
- Questions/Critiques are welcome (and encourage).
- Heckling is legit too (to an extent…)
What is Vim?
History of Vim…
…just kidding
The gist of it…
-
Terminal based text editor
- though there are versions you can use without using the terminal…
-
Keyboard based. No Mouse.
- though you can use it if you want…
-
Very slim and minimalistic
- though you can change that…
Why Vim?
The Advantages of Vim
-
Modal development
- Normal, Visual, Insert modes
- Allows you to edit quickly and effeciently
- No mouse required!
-
Availble on all systems (even Windows)
- Works best on Unix based systems
-
Language agnostic
- It’s just a text editor
-
Very lightweight
- But very customizable
Demo
Basics
Movement
Vim movement
-
Movement in Vim is done in Normal Mode
-
Uses the
h
,j
,k
,l
keys to move one character at a time- You can also use the arrow keys, but it is highly discouraged
-
To move word by word, you can use the
w
,e
, andb
keys -
To Move by paragraphs, you can use the
{
and}
keys, or(
or)
- Requires empty lines to work correctly
Modes
Vim modes (Modal Development)
-
Normal mode (aka ‘Command Mode’)
- Used for movement and commands
- Staring mode
- Also can be returned to by pressing
Esc
orCtrl-c
in any other mode (for the most part)
-
Insert Mode
- Entering text
- Nuff said
- Entered
i
,I
,a
,A
,o
,O
and others.
-
Visual Mode
- Selecting text
- Used modify, duplicate, replace, and delete selected text
- Entered using
v
,V
,Ctrl-v
and others
That’s it!
Okay fine…
Technically there are more then three modes, but they are mostly subsets of the three previously mentioned. If you are curious and want to learn more, type :help vim-modes
while in Vim to learn more.
In Vim, help docs are available inline on a variaty of subjects regarding Vim. Just use the :help [keyword]
to get started.
Copy/Paste
Vim: Copy/Paste
-
Most copying and cutting is done from Visual Mode
-
To copy (yank) some selected text, press
y
-
To cut (delete) some selected text, press
x
-
All copied and cut text is put in a single paste buffer
-
-
To paste the text in the buffer after the cursor, press
p
- And to paste it before the cursor, press
P
(shift p
)
- And to paste it before the cursor, press
Save and Quit
Vim: Save/Quit
-
To save (write) a file, go to command mode and type
:w
-
To quit Vim, go to command mode and type
:q
-
This really is ‘quiting’ a split, but we will get into that more later
-
By Default, you are also required to save before quiting. To override this, use
:q!
. -
Or to make it one step, use
:wq
.
-
Open a new file: :e path/to/file
Hardly worth it’s own slide…
Advanced functions
Buffers
Vim: Buffers
-
Each file that is opened is kept in a buffer, and can be re-accessed at any point
-
To switch between the buffers, you can type
:bp
and:bn
to move to the previous and next buffers respectively -
To delete a buffer (close a file in Vim), you can type
:bd
.-
This will close the split you are in as well if it is not the last split that is open
-
It will also not close vim if you close the last buffer, and will just take you to a empty file buffer
-
Vim: Buffers cont.
One thing to keep in mind is that by default, vim won’t let you switch between buffers without saving the current buffer you are in first. This gets old pretty quick, and is not helpful when you are just using one file as a reference.
To get around this, you can set the ‘hidden’ option (:set hidden
) which will let you swtich between buffers freely and keep your undo history for each.
Splits
Vim: Splits
-
In Vim, you are able to ‘split’ the window vertically or horizontally
-
By default, it will split the current split (or window) in half and use the current buffer that is in the original split to display in the new split.
-
The buffers are shared between split, so you are able to switch between them freely.
-
-
To create a new horizontal split, just press
Ctrl-w s
, or:split
-
To create a new verticle split, just press
Ctrl-w v
, or:vsplit
Vim: Splits cont.
-
Both
:split
and:split
can take a file name in the argument to open a new file with -
You can switch between splits by prefixing
Ctrl-w
withh
,j
,k
, orl
(moving your to the slit to the left, down, up and right respectively) -
Resize a split by combining
Ctrl-w
with+
,-
,<
or>
(increasing the size of the split upwards, downwards, to the left and to the right respectively) -
You can close a split by either closing the buffer (
:bd
), or quiting:q
Search
Vim: Search
-
Searching for a specific word or phrase in vim can be accomplished by pressing
/
and then your word or phrase and pressingEnter
-
By pressing
n
andN
, you can cycle through the search results forward and backward respectively -
Set
:set hlsearch
to have vim highlight your search results- Typing
:noh
will turn of the currently highlighted text
- Typing
-
More info on Vim Regex can be found here: http://vimregex.com/
Find and Replace
Vim: Find and Replace
-
The syntax for find and replace:
:%s/[existing-word]/[new-word]
-
You can add modifiers on to end of the expression
-
’c’ will require a confirmation with each substitution
-
’g’ will make the change globally on each line
-
’i’ ignores case (default) and ‘I’ is case sensitive
-
-
Example: To replace all instances of ‘apple’ with ‘banana’, type
:%s/apple/banana/g
Configuration
Vim Configuration
-
Vim provides a lot of settings that you can customize
-
What you have seen so far:
:set hidden
:set hlsearch
-
But these all go away once you close vim, so how do we persist these settings?
Enter the .vimrc
Vim: The .vimrc
-
The
.vimrc
for you should live within your home directory.- With a fresh install of Vim, it might be blank or non existant, but you can create it there and Vim will recognize and respect it
-
In it, you can set all the defaults that you want when vim loads up
- Settings
- Custom keybindings
- Custom functions
- etc.
.vimrc settings
Useful settings:
- Turn on syntax highlighting:
syntax on
- Turn on line numbers:
set number
- Turn off line wrap:
set nowrap
- Avoid compatability errors:
set nocompatible
- Autocomplete menu:
set wildmenu
- Set command line height:
set cmdheigh=2
.vimrc keybindings
-
You are also able to create keybindings for the multiple modes of Vim in your .vimrc
-
To create a command, you need three things:
-
The ‘mapping’ type:
map
,nmap
,vmap
,imap
-
The key combination
-
The command as you would enter it in normal mode
-
-
Example to map ‘F4’ to
:bn
:nmap <F4> :bn<CR>
-
Non-alphanumeric keys usually are surrounded by
<
and>
-
<CR>
stands for ‘Carriage Return’, or ‘Enter’
-
My .vimrc
Plugins
Vim: Plugins
-
Plugins in Vim (and like in any other software ever) allow you to extend the functionality of Vim
-
The Plugins community in vim is incrediblely strong and active
-
There is a plugin for just about anything you want
Vim: Noteable Plugins
- NERDTree: File drawer
- NERDCommenter: Mass commenting
- minibufexpl: Vim and Tmux integration
- snipmate: Code snippets with tab completion
- ack.vim: Ack integration with Vim
- ctrlp: Fuzzy file finder
- pathogen: Plugin loader
- vimux: Vim and Tmux integration
What is Tmux?
What is Tmux?
-
Tmux stands for/is a terminal multiplexor
“it enables a number of terminals (or windows), each running a separate program, 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”
-
… huh?
So basically this:
Why Tmux?
and why not just use splits in iTerm2
Tmux: Why Tmux?
-
Runs on both Mac and Linux (sorry Windows)
-
Runs in it’s own seperate process
-
Lightweight
-
Customizable (or as customized a a terminal window manager can get)
-
Scriptable: You can script actions to be done in tmux like running a command in another window to setting up your environment on startup
-
Sharable: You can have tmux run on a shared socked that other users on the same machine can share
- AKA: Paired programming
- With ssh: Remote paired programming
Demo
Installation
Installation
-
Linux:
$ sudo apt-get install tmux
- Note, the current only version 1.1 is available using this method. Install from source to get the most up to date version
-
Mac:
brew install tmux
orsudo port install tmux
Install from source
-
Linux prereqs
$ sudo apt-get install build-essential
$ sudo apt-get install libevent-dev libncurses5-dev
-
Mac Prereqs: XCode
Install Instructions:
$ wget http://downloads.sourceforge.net/project/tmux/tmux/tmux-1.6/tmux-1.6.tar.gz
$ tar -zxvf tmux-1.6.tar.gz
$ cd tmux-1.6
$ ./configure
$ make
$ sudo make install
Attach and Detach
Tmux: Attach and Detach
-
To enter tmux, you can just type
tmux
from the command line-
This will attach you to a new session that is created with the name of ‘0’ (creative, I know)
-
If you want name it yourself, use
tmux new -s [name_of_session]
-
-
To leave a session, just close it by typing
exit
in the last open pane -
To detach without closing a session type
Ctrl-b d
ortmux detach
-
To attach to an existing session, use
tmux attach -t [name_of_session]
- To see a list of the open sessions, you can type
tmux list-sessions
ortmux ls
from outside of a tmux session
- To see a list of the open sessions, you can type
Windows and Panes
Tmux: Windows and Panes
-
To create a new tmux verticle split:
Ctrl-b %
-
To create a new tmux horizontal split:
Ctrl-b %
-
To move between pains, combine
Ctrl-b
withUp
,Down
,Left
orRight
-
To close a pain:
Ctrl-b x
Configuration
Tmux: Configuration
-
Just like the
.vimrc
, tmux also has a config file that you can add to your home directory:.tmux.conf
-
As with the
.vimrc
, you are able to customize the controls and the look and feel of the screen with the.tmux.conf
-
Another similarity is that everything you are doing in the
.tmux.conf
is just executing a command that you can otherwise just run using tmux’s command mode
Tmux: Configuration Cont.
We are about to make a lot of changes to the .tmux.conf
, and restarting tmux everytime is no picnic. To make this easier, we can reload tmux by running the following command:
source-file ~/.tmux.conf
This is similar to reloading your .bashrc
when you make changes. To make this even easier, you can bind this to a key mapping:
bind-key r source-file ~/.tmux.conf; display-message "~/.tmux.conf is reloaded"
Tmux: Configuration Cont.
-
The default prefix key is also not that easy to use, and while it doesn’t conflict with a whole lot, it still is nice to have a prefix key that is convenient to use.
-
To change the prefix key, add the following to your
.tmux.conf
unbind C-b set -g prefix C-a
Tmux: Configuration Cont.
Movement in tmux also doesn’t default to something that you are propbably already comfortable with: the Vim key bindings, so you can make some changes to make using tmux similar to Vim
Change movement keys:
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
Change how you resize panes:
bind-key J resize-pane -D 5
bind-key K resize-pane -U 5
bind-key H resize-pane -L 5
bind-key L resize-pane -R 5
Change copy mode: set-window-option -g mode-keys vi
Tmux: Configuration Cont.
These are just a few more configs that are nice:
set -g base-index 1 # start window index of 1
set -sg escape-time 1 # fixes some delay issues in vim
bind-key a send-key C-a # `Ctrl-a a` sends `Ctrl-a`
bind-key n command-prompt 'rename-window %%'
bind-key N command-prompt 'rename-session %%'
bind-key -r < swap-window -t :-
bind-key -r > swap-window -t :+
# Copy mode keybindings
unbind p
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
My .tmux.conf
Addons
Tmux: Addons
- Tmuxinator: Startup tool for tmux
- Brian’s Tmux Conf: Some more cool config options
There really aren’t a lot of addons that you need for tmux…
Tmux: Brian Hogan’s Tmux Book
This book is not only a great intro to tmux, but was also a great reference for me when writing these slides.
tmux: Productive Mouse-Free Development
It’s only $11, so you have no excuse not to get it!
… unless you don’t have $11…
Vim with Tmux
Nick LaMuro
Twitter @nicklamuro
github https://github.com/NickLaMuro
Slides http://talks.nicklamuro.com