Skip to content

Instantly share code, notes, and snippets.

@ciaranarcher
Last active November 12, 2020 14:12
Show Gist options
  • Save ciaranarcher/5561113 to your computer and use it in GitHub Desktop.
Save ciaranarcher/5561113 to your computer and use it in GitHub Desktop.
vim

Some vim-go tips

  • gd jumps to the definition of a type, variable
  • C-t jumps you back to where you where.
  • Use <tab> to autocomplete snippets in insert mode. Example snippets.
  • :A open alternate (i.e. test for current file). Also :AH and :AV for horizontal and vertical splits.
  • gt will run the tests for the current file.
  • :GoDeclsDir will show all functions in current directory.
  • K open GoDoc for symbol under the cursor.
  • <leader>i over a function or method name will show function information.
  • <leader>c will run GoCoverage for the current file.
  • <leader>m will run the GoMetaLinter for the current file.
  • :GoReferrers will find anything calling the function under the cursor.
  • :GoRename will allow you rename any symbol in the package.

Reloading vimrc

  • While editing: :so %
  • :edit Reload current file

Moving About

  • http://naleid.com/blog/2010/10/04/vim-movement-shortcuts-wallpaper/
  • >> and << for indenting text
  • J Join next line with current line
  • C-f Forward a page
  • C-b Back a page
  • # Find word under cursor
  • <leader>space Clear search
  • 0 Go to start of line
  • <number>j /<number>j Move number of lines down/up
  • _ Go to the start of the first word on the line

Editing

General Tips

  • :enew Open no name buffer in the current window
  • u Undo
  • C-R Redo
  • e Jump to end of word (inc. puncuation)
  • O Insert line before (and go to edit mode)
  • o Insert line after (and go to edit mode)
  • cs"' Change quotes (that's c, s, double quote, single quote)
  • vi<surrounding>p Replace into surround chars from clipboard
  • yss<character> Surround a while line with the particular character (e.g. quotes)
  • ci<character> Change text within
  • = Indent selected lines
  • . Repeat previous command
  • D Delete until end of line in normal mode
  • C Change until the end of the line in normal mode
  • yg_ Copy to end of line excluding line break
  • ~ Change case of single character under cursor

Ruby block editing

  • ir and ar are used for a block, e.g. vir
  • if and af for a function, e.g. vif or cif
  • ic, ac for a class, e.g. cic
  • an for a name, e.g. cin for something like MyName::Project
  • Once selected o will jump between start and end of selected block

Commenting

Alignment

Buffers & Windows

Windows

  • <leader>v Vertical split
  • <leader>h Horizontal split
  • C-<direction> Navigate windows
  • <leader>ww Swap buffers between windows (https://github.com/wesQ3/vim-windowswap)
  • C-w then q Close the currently selected split
  • :on Close all splits except the current selection

Buffers

  • :ls List buffers
  • :b <name> Tab complete buffer name
  • :bn Next buffer
  • :bp Previous buffer
  • C-6 Previous buffer
  • <leader>b Previous buffer
  • :bd Delete current buffer

Tabs

  • gt Next tab
  • gT Previous tab
  • ZZ Write and close a tab

Quick list (NERDTree too)

  • q Close window
  • <direction> Move up/down the list
  • o Open
  • t Open as tab
  • v Open as vertical split
  • i Open as horizontal split

File Search

  • C-p Open Search Dialogue
  • C-d Only match file names
  • Esc Close Dialogue
  • C-j C-k Navigate results
  • C-o Choose how to open (vertical split etc.)
  • Enter Open result

Find / Search in Files

  • \ Search shortcut
  • :Ag -i <pattern> for a literal search (use quotes if there are spaces in the search term)
  • K Search for word under cursor
  • :%s/search/replace/
  • :%s/star/blackhole/gic Confirm each replacement (global, case-insensitive, confirm)
  • SHIFT-3 Match all under cursor
  • :CtrlSF <pattern> then in window do a search replace as per above and save it to save changes to all files.

Ctags

  • ctags -R --exclude=.git --exclude=log * Generate tags for this project.
  • set tags=./tags; Add to .vimrc to help it find the tags.
  • C-] Jump to 'tag' under cursor
  • C-t Jump back to previous tag
  • <leader>. Search ctags file using Ctrl-P

Git

  • :OpenGithubFile Open current file on Github
  • :Gitv Open Git explorer
  • Glog -- % Commits that touched the current file
  • :Gblame Run Git blame on the current file
  • :Gstatus Status
  • :Glog To view log then ]Q to go to start of list and ]q to step through revisions
  • :Gedit to revert to working copy
  • :Glog -10 Last 10 revisions

Testing

  • RunTests Runs mspec test for the current file
  • RunNearestTest Runs mspec test for current test
  • ruby % --line 57

Registers

http://www.brianstorti.com/vim-registers/

  • :reg <register> <register> List register contents
  • "0p Paste previously pasted content
  • :let @+=@% Copy current file path to clipboard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment