Power user techniques to maximize your efficiency
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"
]
Special registers:
Operate on text objects with {operator}{a|i}{object}:
" 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()
Save/restore sessions with tmux-resurrect plugin:
#!/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
# 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'
The real power comes from seamless integration between Vim and Tmux.
# 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.
# 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.
Find class properties in JavaScript:
Replace with regex captures:
Apply to specific lines:
Apply to all files in project:
Combine with macros for complex transformations across multiple files.
" 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.
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.
Fuzzy finder for files, buffers, lines, git commits, and more. Supercharges navigation.
Git integration for Vim. Run git commands and see diffs without leaving the editor.
Real-time linting and fixing. Integrates with ESLint, Prettier, and more.
Save and restore tmux sessions across system restarts.
Automatic saving and restoration. Works with tmux-resurrect.
Copy to system clipboard. Seamless integration with OS clipboard.
Armed with these advanced techniques, you're ready to take your productivity to the next level.