Vim+TmuxMaster

Advanced Vim & Tmux Tricks

Power user techniques to maximize your efficiency

Vim Power User

Macros

q{register} - Start recording to register
q - Stop recording
@{register} - Play macro

Example: Convert list to JSON array

Starting with:
apple
banana
cherry

Record macro (qa):
i"A",j

Apply to all lines (@a@a)
Add [ at start, remove last comma, add ]

Result:
[
  "apple",
  "banana",
  "cherry"
]

Registers

"{register}y - Yank into register
"{register}p - Paste from register
:reg - View registers

Special registers:

"0 - Last yank
". - Last inserted text
"/" - Last search pattern
"+" - System clipboard

Text Objects

Operate on text objects with {operator}{a|i}{object}:

ci" - Change inside quotes
da( - Delete around parentheses
yi{ - Yank inside curly braces
vat - Select around HTML tag
dap - Delete around paragraph

Advanced .vimrc

" Basic settings
set nocompatible
filetype plugin indent on
syntax enable

" Key mappings
nnoremap  :Files
nnoremap w :w

" Plugin configuration
let g:NERDTreeWinSize=40
let g:ctrlp_working_path_mode='ra'

" Auto commands
autocmd FileType javascript setlocal ts=2 sts=2 sw=2
autocmd BufWritePre * :%s/\s\+$//e  " Remove trailing whitespace on save

" Custom functions
function! ToggleNumber()
  if(&relativenumber == 1)
    set norelativenumber
    set number
  else
    set relativenumber
  endif
endfunc
nnoremap n :call ToggleNumber()

Tmux Power User

Session Management

Prefix + ( - Previous session
Prefix + ) - Next session
Prefix + s - Choose session from list
Prefix + d - Detach from session

Save/restore sessions with tmux-resurrect plugin:

Prefix + Ctrl-s - Save session
Prefix + Ctrl-r - Restore session

Tmux Scripting

#!/bin/bash
# Create dev environment

# Create session if it doesn't exist
tmux has-session -t dev 2>/dev/null
if [ $? != 0 ]; then
  # Create new session
  tmux new-session -d -s dev -n 'editor'

  # Configure windows and panes
  tmux send-keys -t dev:editor 'cd ~/projects/app' C-m
  tmux send-keys -t dev:editor 'vim .' C-m

  # Create and split second window
  tmux new-window -t dev:1 -n 'server'
  tmux split-window -h -t dev:server
  tmux send-keys -t dev:server.0 'npm run dev' C-m
  tmux send-keys -t dev:server.1 'npm test -- --watch' C-m
fi

# Attach to session
tmux attach -t dev

Advanced .tmux.conf

# Vim-like pane navigation
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Copy mode with vi keys
set-window-option -g mode-keys vi
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-selection-and-cancel

# Smart pane switching with vim awareness
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h'  'select-pane -L'
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j'  'select-pane -D'
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k'  'select-pane -U'
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l'  'select-pane -R'

# Status bar customization
set -g status-style 'bg=#333333 fg=#5eacd3'
set -g status-left-length 30
set -g status-right-length 150
set -g status-left '#[fg=green](#S) '
set -g status-right '#[fg=yellow]#(whoami)@#H #[default]%a %d %b %H:%M'

Vim + Tmux Integration

The real power comes from seamless integration between Vim and Tmux.

Seamless Navigation

# In .vimrc
" Smart pane switching with awareness of Tmux panes
let g:tmux_navigator_no_mappings = 1

nnoremap   :TmuxNavigateLeft
nnoremap   :TmuxNavigateDown
nnoremap   :TmuxNavigateUp
nnoremap   :TmuxNavigateRight

# In .tmux.conf
# Smart pane switching with awareness of Vim splits
is_vim="..."
bind -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"

Now Ctrl + h/j/k/l navigates seamlessly between Vim splits and Tmux panes.

Shared Clipboard

# In .vimrc
" Vim and Tmux shared clipboard
set clipboard=unnamed

# In .tmux.conf
# Use vim keybindings in copy mode
setw -g mode-keys vi

# Setup 'v' to begin selection, 'y' to copy
bind-key -T copy-mode-vi v send -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy"

# Update default binding of `Enter` to use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "pbcopy"

Copy text in Vim and paste it in a Tmux pane, or vice versa.

Advanced Editing Techniques

Advanced Search & Replace

Find class properties in JavaScript:

/this\.\w\+

Replace with regex captures:

:%s/function \(\w\+\)()/\1 = () =>/g

Apply to specific lines:

:10,20s/foo/bar/g

Apply to all files in project:

:args **/*.js
:argdo %s/foo/bar/g | update

Combine with macros for complex transformations across multiple files.

Custom Vim Commands

" Define custom commands in .vimrc
command! -nargs=0 FormatJSON %!python -m json.tool
command! -nargs=0 FoldMethods :g/^\s*\(private\|public\|protected\).*function/normal! zf%

" Auto-detect file type
command! -nargs=0 DetectFileType :call system('file --mime-type % | grep -q "text/x-") ? set ft=sh : set ft=python')

" Toggle between absolute and relative line numbers
command! -nargs=0 ToggleLineNumbers :set relativenumber!

" Git integration
command! -nargs=0 GitBlame :call system('git blame ' . expand('%') . ' | grep ' . line('.') . ' | head -n 1')

Use : followed by the command name to execute.

Tmux Session Management

Using tmuxinator for project workspaces:

# ~/.tmuxinator/project.yml
name: project
root: ~/projects/myapp

windows:
  - editor:
      layout: main-vertical
      panes:
        - vim
        - git status
  - server: npm run dev
  - logs:
      layout: even-horizontal
      panes:
        - tail -f log/development.log
        - tail -f log/test.log

Start this workspace with tmuxinator start project.

Hidden Gems

Lesser-Known Vim Features

  • gf - Go to file under cursor
  • g& - Repeat last substitute on all lines
  • '. (dot) - Jump to last modified line
  • :earlier 5m - Revert to file 5 minutes ago
  • :put =system('ls -la') - Insert command output

Lesser-Known Tmux Features

  • Prefix + : - Enter command mode
  • Prefix + f - Find window by name
  • Prefix + i - Show window info
  • Prefix + } - Swap pane positions
  • Prefix + q - Show pane numbers and quickly select

Essential Power User Plugins

Vim Plugins

  • fzf.vim

    Fuzzy finder for files, buffers, lines, git commits, and more. Supercharges navigation.

    :Files - Find files
    :Rg - Ripgrep search
  • vim-fugitive

    Git integration for Vim. Run git commands and see diffs without leaving the editor.

    :Gstatus - Git status
    :Gdiff - Git diff
  • ALE (Asynchronous Lint Engine)

    Real-time linting and fixing. Integrates with ESLint, Prettier, and more.

    :ALEFix - Fix code issues

Tmux Plugins

  • tmux-resurrect

    Save and restore tmux sessions across system restarts.

    Prefix + Ctrl-s - Save session
    Prefix + Ctrl-r - Restore session
  • tmux-continuum

    Automatic saving and restoration. Works with tmux-resurrect.

    set -g @continuum-restore 'on' - Auto-restore on start
  • tmux-yank

    Copy to system clipboard. Seamless integration with OS clipboard.

    Prefix + y - Copy text from copy mode

Ready to Become a Power User?

Armed with these advanced techniques, you're ready to take your productivity to the next level.