Skip to content

Instantly share code, notes, and snippets.

@mattjmorrison
Last active January 3, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattjmorrison/69329b96217b384eb5a8 to your computer and use it in GitHub Desktop.
Save mattjmorrison/69329b96217b384eb5a8 to your computer and use it in GitHub Desktop.
Tool Instructions
.vagrant/
*.swp

Vim Instructions

  1. Install Vagrant
  2. Run vagrant up
  3. Run vagrant ssh
  4. Type cd tutorial
  5. Type vim vim.md

Starting tmux

  • tmux

Tmux config file

  • ~/.tmux.conf

Tmux "leader"

  • Default is <C-b>
  • I recommend using <C-Space>

Splits

  • <C-">
    • split the window horizontally
  • <C-%>
    • split the window vertically

Windows

  • <Leader>-c
    • open a new window
  • <Leader>-n
    • move to the next window
  • <Leader>-#
    • move to window number # (where # is a number)

To change the leader

  • <C-b>:set -g prefix C-Space or add set -g prefix C-Space in ~/.tmux.conf
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
config.vm.box = "ubuntu-14.04"
config.vm.synced_folder ".", "/home/vagrant/tutorial"
config.vm.provision "shell",
inline: "sudo apt-get install git -y"
end

Starting vim

  • vim
  • vim name_of_file_to_open
  • vim **/*.py open all python files
    • in bash this may require shopt -s globstar
  • vimdiff first secondm
    • run this command passing it 2 files to see a diff of the 2 files

Opening Files (from within vim)

  • :e name_of_file_to_open

Saving Files

  • :x
    • save the current file and exit
  • :w
    • save the current file
  • :wa
    • save all open files
  • :wq
    • save current file and quit
  • :waq
    • save all files and quit
  • :q!
    • quit without saving any files
  • :qa!
    • quit and do not save or prompt to save any open files

Basic Movement

  • hjkl
    • left,down,up,right
  • w or b
    • move (forward/backward) to the beginning of a word (delimited by blanks & special characters)
  • W or B
    • move (forward/backward) to the beginning of a word (delimited by blanks)
  • e or ge
    • move (forward/backward) to the end of a word (delimited by blanks & special characters)
  • ge or E
    • move (forward/backward) to the end of a word (delimited by blanks)
  • <C-u> or <C-d>
    • page up/down by a half screen
  • <C-f> or <C-b>
    • page up/down a screen
  • gg or G
    • go to beginning/end of document
  • f or F
    • jump to a character (forward/backward from the cursor) on current line
  • t or T
    • jump until a character (forward/backward from the cursor) on current line
  • ; or ,
    • replay last T, t, F, or f
  • %
    • move to matching '{', '[', '(', closing html/xml tags
  • * or #
    • move cursor to next/previous occurance of word under cursor
  • ( or )
    • move cursor forward/backward a sentence
  • { or }
    • move cursor forward/backward a paragraph
  • zt
    • scrolls the document so your cursor is at the top of the screen
  • zb
    • scrolls the document so your cursor is at the bottom of the screen
  • zz
    • scrolls the document so your cursor is in the middle of the screen
  • <C-o>
    • move cursor to older cursor position
  • <C-i>
    • move cursor to newer cursor position

Searching Text

  • :q/MATCH_THIS/
    • Show everything in the current buffer that matches the given regex
  • :v/MATCH_THIS/
    • Show everything in the current buffer that does NOT match the given regex
  • vimgrep
    • :vimgrep /something/g **/*.* find something in all files
    • :vimgrep /something/g ## find something in all opened files
  • / or ?
    • search document (forward/backward) for given regex
    • n/N will go (forward/backward) to next search result
  • q/
    • show search history for searches done using / or ?
  • :set hlsearch / :set nohlsearch
    • highlight search matches in file
  • :set incsearch / :set noincsearch
    • highlight search maches as you type

Selecting Text

  • V
    • select a line
  • <C-v>
    • visual block
  • V{/}
    • select expanding blocks up/down
  • p
    • paragraph

Editing Text

  • u
    • undo last change
  • <C-r>
    • redo last undo

Entering Insert Mode

  • i
    • 1 character before the cursor
  • I
    • at the first character of the current line
  • o
    • 1 line after the cursor
  • O
    • 1 line above the cursor
  • a
    • 1 character after the cursor
  • As
    • after the last character of the current line

Cut, Copy & Paste

  • y
    • 'yank' (or copy) the selected text
  • yy
    • 'yank' (or copy) the current line
  • p
    • 'paste' the last yanked text one character after the cursor
  • "?y
    • 'yank' into '?' register ( do not use numbers for ? )
  • "?p
    • 'paste' from '?' register ( do not use numbers for ? )

Advanced text editing

  • <C-v>(select column)I(enter text)<ESC>
    • prepend some text to all columns
  • <C-v>$(select column)A(enter text)<ESC>
    • append some text to the end of each line

Chaining Commands

  • yiw
    • 'yank inner word'
      • ie: with your cursor on a 'word' yiw will copy that word
  • ciw
    • 'change inner word'
      • ie: with your cursor on a 'word' ciw will replace that word and put your into insert mode
  • diw
    • 'delete inner word'
  • caw
    • 'change around word'
      • ie: with your cursor on a 'word' caw will replace the word (including surrouding whitespace)
  • yaw
    • 'yank around word'
  • daw
    • 'delete around word'
  • ci?
    • 'change inner ?'
      • ie: where '?' is a character like (

Help

  • :help
    • documentation
  • <C-]>
    • jump in tags (used in help docs)

Misc

  • <C-r>= (in insert mode)
    • will allow you to evaluate math expressions

Power Tools

  • :arg
    • show or set the current argument list
      • :arg will show it
      • :arg **/*.py will set it to all python files
  • :argdo
    • perform an operation on each file in the argument list
    • :argdo %g/something/d will delete all lines containing the word something in all files in the arugment list
  • :bufdo
    • perform an operation on each open buffer
    • :bufdo g/something/d will delete all lines containing the word something in all open buffers
  • :tabdo
    • perform an operation on each open tab
  • ##
    • is the arglist
  • %
    • is the current file

Quickfix

  • :copen
    • open quickfix
  • :cnext
    • go to next quickfix result
  • :cprev
    • go to previous quickfix result
  • :cold
    • go to previous quickfix buffer
  • :cnew
    • go to next quickfix buffer
  • :cclose
    • close quickfix

Commands

  • .
    • replay last normal mode command
  • i
    • inner
  • a
    • around
  • c
    • change

Settings

  • :set showcmd / :set noshowcmd
    • shows commands as they are being typed
  • :set number / :set nonumber
    • shows line numbers
  • :set relativenumber / :set norelativenumber
    • shows line numbers relative to your cursor
  • :set wrap / :set nowrap
    • turn on line wrapping
  • :set spell / :set nospell
    • turn on spell checking

History

  • q:
    • show command history

Splitting

  • :vs
    • vertial split
  • :split
    • horizonal split
  • <C-w>hjkl
    • move cursor to pane to left,below,above,right
  • <C-w><>
    • resize pane to the left or right
  • <C-w>+-
    • resize pane to the top or bottom
  • <C-w>J
    • switch from vertical to horizonal split
  • <C-w>L
    • switch from horizonal to vertical split
  • <C-w><C-r>
    • swap pane positions

Executing External Commands

  • :! external_command

Registers

  • :reg
    • show all registers

Buffers

  • :ls
    • show all current buffers
  • :bn
    • go to next buffer
  • :bp
    • go to previous buffer
  • :b#
    • go to buffer # where # is the number of the buffer from :ls
  • :bf
    • go to the first buffer
  • :bl
    • go to the last buffer

Marks

  • m{a-z}
    • will set a mark called whatever you typed after the m which can be jumped to from within the file
  • m{A-Z}
    • will set a mark called whatever you typed after the m which can be jumped to from another file
  • '{a-zA-Z}
    • will jump to a mark called whatever you typed after the ' (lower case within the file upper case across files)
  • :marks
    • will show all current marks

Macros

  • q?
    • begin recording a macro into register named ?
  • q
    • stop recording a macro (must have been previously recording)
  • @?
    • play a recorded macro from register ?
  • x@?
    • play a recorded macro from register ? x number of times

Folding

  • zf
    • with text selected in visual mode
    • followed by x{direction} where x is the number of lines you want folded
  • zc or zC
    • close a fold (one level/recursive)
  • zo or zO
    • open a fold (one level/recursive)
  • za or zA
    • toggle a fold (one level/recursive)
  • zR
    • open all folds
  • zM
    • close all folds

Tabs

  • vim -p file1 file2 file3
    • open each file in a separate tab
  • :tabnew
    • create a new tab
  • :tabe
    • open a file in a new tab
  • :tabn
    • go to next tab
  • :tabfirst
    • go to first tab
  • :tabc
    • close current tab

Tips

  • Set the argument list to all modified files
    • ":arg git diff --name-only"

Mapping Shortcuts

  • Example: Easier quickfix keys
    • :map <F1> :copen<CR>
    • :map <F2> :cnext<CR>
    • :map <F3> :cprev<CR>
    • :map <F12> :cclose<CR>
  • Example: Basic Emacs Navigation (in insert mode)
    • :imap <C-e> <End>
    • :imap <C-a> <C-o>0
    • :imap <C-f> <Right>
    • :imap <C-b> <Left>
    • :imap <C-p> <Up>
    • :imap <C-n> <Down>
    • :imap <Esc>f <C-o>W
    • :imap <Esc>b <C-o>B

Vimdiff Commands

  • ]c
    • Jump forwards to the next start of a change.
  • [c
    • Jump backwards to the previous start of a change.

Starting zsh

  • zsh

Defult Shell

  • chsh zsh (this may require a system reboot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment