Vim
Jump to navigation
Jump to search
Vim is a powerful text editor. Keystrokes can be chained together to combine actions, movements and selections into coolness. Folk using Vim 20 years still don't know it all.
todo; rearrange commands into motion, action, etc.
Quick
- Cheatsheet for programmers - Part of the deep end!
- Shortcut layout - a sample of movement/selection, layed out in their direction
- Vim Quick Reference Card
- bullium.com Cheat Sheet
- http://www.worldtimzone.com/res/vi.html
- vim tips and trick
Guides
- Vim Tips Wiki
- tuxfiles.org Vim commands
- Learn Vim Progressively
- Seven habits of effective text editing
- Learn to speak vim – verbs, nouns, and modifiers!
- Vim anti-patterns [1]
- http://symbolsystem.com/2010/12/15/this-is-your-brain-on-vim/
- Why Vim?
- Five Minute Vimscript
- Buffers, windows, and tabs
- http://www.openvim.com/
- http://stevelosh.com/blog/2010/09/coming-home-to-vim/
- http://haridas.in/vim-as-your-ide.html
- http://therandymon.com/papers/vimforwriters.pdf
Video
- Vimcasts.org by Drew Neil
- Tech Meetup Edinburgh Talk: Drew Neil on Vim
- 7 Habits For Effective Text Editing 2.0
- "A large percentage of time behind the computer screen is spent on editing text. Investing a little time in learning more efficient ways to use a text editor pays itself back fairly quickly. This presentation will give an overview of the large number of ways of using Vim in a smart way to edit programs, structured text and documentation. Examples will be used to make clear how learning a limited number of habits will avoid wasting time and lower the number of mistakes. Bram Moolenaar is mostly known for being the benevolent dictator of the text editor Vim."
Main stuff
vim filename open file (or buffer if file doens't exist) :e filename open filename in vim :w save :w filename save as :q quit if saved :x save if changed and quit, same as :wq ZZ save as above ZQ even if not saved (also :q!) :ex . - explore files in file directory. opens in split pans if file modified. :Sex - as above but forces split :Vex - as above but vertical split :Tex - as above but in new tab <enter> - open file o - open file in new split buffer
Ctrl-N autocomplete keyword forwards Ctrl-P autocomplete keyword backwards
Moving around
zz - centre screen on cursor zt - move screen top to cursor zb - move screen bottom to cursor
Ctrl-e Moves screen up one line Ctrl-y Moves screen down one line Ctrl-u Moves screen up ½ page Ctrl-d Moves screen down ½ page Ctrl-b Moves screen up one page Ctrl-f Moves screen down one page [2]
0 - line beginning ^ - first non-whitespace character $ - line end
e - forward word E - forward WORD b - back word B - back WORD { - beginning of previous } - next paragraph ( - beginning of previous ) - next sentence % - current brace / - find text * - jump to next occurrence of work n - repeat forwards N - repeat backwards H - cursor to top of screen (high) M - cursor to middle of screen (middle) L - cursor to bottom of screen (low) Ctrl-D move cursor down half-page Ctrl-U move cursor up half-page gg - start of file G - end of file 123G - move to line 123 :123 - move to line 123 (easier imo) ma - mark cursor position 'a' 'a - move to mark position 'a'
Operations
. - repeat last change u - undo last change Ctrl-u - undo whilst in insert mode U - undo all changes to current line Ctrl-r - redo i - insert at cursor I - insert at line beginning a - append after the cursor A - append at the end of the line o - add ('open') line below and insert O - add line above and insert x - delete character under cursor X - delete character before cursor y - yank (copy) current text yy - yank current line p - paste yanked text after cursor/line P - pasted yanked text before cursor/line d - delete and yank (cut) df* - delete to (find) and including * dl - delete character (alias: "x") dd - delete current line including linebreak dw - delete to end of word from cursor d$ - delete to end of line from cursor diw - delete inner word diW - delete inner WORD daw - delete word, up to delimiter daW - delete WORD, including previous space dis - delete inner sentence das - delete a sentence dib - delete inner '(' ')' block dab - delete a '(' ')' block dip - delete inner paragraph dap - delete a paragraph diB - delete inner '{' '}' block daB - delete a '{' '}' block c - change (delete and insert) cc - delete current line including linebreak, insert cw - delete to end of word from cursor, insert c$ - delete to end of line from cursor, insert ciw - delete inner word, insert ci" - change inner quoted string ci( - change inner brackets ci[ - change inner contents of [].. ci], ci) for insert on closing bracket caw - change an object caW - change an object, including space etc. v - visual select text viw - visual inner word viw~ - visual inner word, toggle case vip - visual inner paragraph vec - visual, end of word, change highlighted V - visual select lines Ctrl-v - visual select a block s - change one character and insert Ctrl-Y - copy character from line above Ctrl-E - copy character from line below Ctrl-N - auto-completion next match Ctrl-P - auto-completion previous match > - indent right < - indent left
V= - visual select lines, then reformat with = = - fix indentation for selection == - fix indentation for one line
:s/foo/bar/ - search and replace first occurrence :s/foo/bar/s - search and replace, global current line :%s/foo/bar/g - search and replace, global whole file :%s/foo/bar/gc - search and replace, with confirm :g/text string/d - delete all lines with text string :!g/text string/d - delete all lines without text string [3] :g!/^\s*#/d - delete all lines without a # qa - start recording macro 'a', q - stop recording qA - start appending to macro 'a' @a - play macro a @@ - execute again 3@a - play macro 3 thrice :let @a='macrogoeshere' - write macro manually Ctrl-R Ctrl-R a - insert mode :let @a='Ctrl-R Ctrl-R a - edit existing macro
Buffers
:b [buffer number of any part of name] switch to buffer
set wildchar=<Tab> wildmenu wildmode=full with this, :b [tab] gives a menu
Windows
Windows are like tmux panes, awesome clients. Windows can hold file buffers or things like Nerdtree. Tabs are a set of windows.
:sp [filename] open file in new window
Ctrl-W w move forward window Ctrl-W W move backwards window
Ctrl-W then h, j, k, l select window left, below, above, right
Ctrl-W h swap buffer in active window with the window below Ctrl-W k as above, to above Ctrl-W h as above, to left Ctrl-W l as above, to right
Ctrl-W x switch windows around
Ctrl-W _ maximize window horizontally Ctrl-W | maximize window vertically
Tabs
:tabs list tabs
gt go to next tab gT go to previous tab {i}gt go to tab in position i
:tab drop {file} open {file} in a new tab, or jump to a window/tab containing the file if there is one :tab split copy the current window to a new tab of its own :tabm [n] move tab to nth position :tabm move tab to last :tab ball split all buffers into tabs :tab help open a new help window in its own tab page
- TabBar - derived from miniBufExplorer
Git
:Gwrite - git add file :Gread - git checkout (revert) open to staged version :Gremove - git rm and close buffer :Gmove - git mv file. with /, relative to git root; without, relative to file :Gcommit - git commit, opens message buffer :Gblame - open split window with git blame details :Gbrowse - open Github, else git instaweb for local sevrer
vimdiff resolution:
:diffget [buffer] - get diff from another buffer :diffput [buffer] - put diff into another buffer :diffupdate - update diff colouring dg - get from other buffer pane (only 2 pane) dp - put to other buffer pane (works in 3 pane) [c - jump to previous changeset ]c - jump to next changeset :only - close buffers other than active
Folds
Folds are sections of text reduced to one line (based on brackets, indentation, etc.). Folding is on by default. I have the foldlevel dialed up to 20 to avoid them.
zo - open fold zO - open fold recursively zc - close fold zC - close fold resursive zR - open all folds zM - close all zj - go down and up a fold zk - go up a fold
Arrow keys
Handy
:e scp://root@example.com/~user/folder/.config - open remote file $ vim scp://root@example.com/~user/folder/.config :reg - list register contents K - in normal mode, run man for current word (opens "man word" in shell)
- http://www.bilalquadri.com/villustrator/ - create themes
- http://bytefluent.com/vivify/
- http://vimgolf.com/
- http://jeffkreeftmeijer.com/2012/relative-line-numbers-in-vim-for-super-fast-movement/
- http://vim.wikia.com/wiki/Toggle_auto-indenting_for_code_paste
- stackoverflow: How to paste text into Vim command line
- http://mit.edu/~georgiou/www/vi/ - interesting at least
Vimscripts
- Vimpusher - Vim setup sharing
Management
- https://github.com/tpope/vim-pathogen - vimscript management
- https://github.com/gmarik/vundle/ - like pathogen but can install/update
- https://github.com/Shougo/neobundle.vim - install straight from github, git, vimscripts.org, other http.
Git
- https://github.com/tpope/vim-fugitive - bestest gitwrapper
GitHub
Syntax
- https://github.com/ChrisYip/Better-CSS-Syntax-for-Vim - has vendor prefixes. breaks with scss [4] :/
- https://github.com/hail2u/vim-css3-syntax - recent
- http://www.vim.org/scripts/script.php?script_id=3042
Tools
To sort
- cmdalias.vim - Create aliases for Vim commands. [6]
to check;
- https://github.com/tomtom/tcomment_vim - comment add/remove
gc{motion} toggle comments (for small comments) gcc toggle comment for the current line gC{motion} comment region gCc comment the current line
- http://reluctanthacker.rollett.org/software/drupavim - post content using blog api
- https://github.com/scrooloose/nerdtree
- https://github.com/mattn/zencoding-vim
- https://github.com/Lokaltog/vim-easymotion
- https://github.com/Shougo/neocomplcache
- https://github.com/jceb/vim-orgmode
- https://github.com/sickill/vim-pasta - Pasting in Vim with indentation adjusted to destination context.
- https://github.com/ervandew/supertab
- https://github.com/msanders/snipmate.vim
- https://wincent.com/products/command-t [7]
- http://www.vim.org/scripts/script.php?script_id=1075
- http://mattn.github.com/zencoding-vim/
- http://www.vim.org/scripts/script.php?script_id=1984
- https://github.com/benmills/vimux
- https://github.com/gregsexton/gitv