Vim

From Things and Stuff Wiki
Revision as of 17:34, 5 August 2013 by Milk (talk | contribs) (→‎System)
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. See also Emacs (and take care of your wrists (and posture)).

todo; big refactor to rearrange commands into motion, action, etc.

Quick

Images

  • Shortcut layout - a sample of movement/selection, layed out in their direction

Learning

Video

  • 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."

Install

Some scripts require Vim 7.3, but some distros have older builds. You can build 7.3 locally though or use a provision system.

Had syntax issue errors. Rumtime path also should include distro base config..; http://www.linuxquestions.org/questions/linux-software-2/vim-syntax-highlight-fails-after-debian-upgrade-378073/

Packages and support

Building

Modes

Ctrl-o
  perform normal mode command in insert mode

Visual

File operations

Command line

vim filename
  edit file (or create a buffer if file doesn't exist)

If you want to start vim with several files in a splitted window, just type;

vim -o a b c

for the horizontal split, and

vim -O a b c

for the vertical split.

In Vim

: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

File navigation

Motion

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 [4]
gg - start of file
G - end of file

123G - move to line 123
:123 - move to line 123 (easier imo)
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

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 

ma - mark cursor position 'a'
'a - move to mark position 'a'
/ - search for text
* - search for text under cursor
n - repeat search forwards
N - repeat search backwards
Ctrl-O
  move back location
Ctrl-I
  move forward location
gj
  move down a soft linebreak line

Finding things

  • EasyMotion provides a much simpler way to use some motions in vim. It takes the <number> out of <number>w or <number>f{char} by highlighting all possible choices and allowing you to press one key to jump directly to the target.
  • Seek is a vim plugin that aims to make inline navigation effortless. The motion seek, summoned with s by default, is similar to f, but instead of one it expects two characters.
  • Command-T plug-in for VIM provides an extremely fast, intuitive mechanism for opening files with a minimal number of keystrokes. It's named "Command-T" because it is inspired by the "Go to File" window bound to Command-T in TextMate. Files are selected by typing characters that appear in their paths, and are ordered by an algorithm which knows that characters that appear in certain locations (for example, immediately after a path separator) should be given more weight. [6]
  • FuzzyFinder - buffer/file/command/tag/etc explorer with fuzzy matching
  • ctrlp.vim - Full path fuzzy file, buffer, mru, tag, ... finder for Vim.

Line numbers

Operations

.
  repeat last change. doesn't work with plugin actions without script.
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
:%y+
  yank all lines to clipboard [8]
p
  paste yanked text after cursor/line
P
  paste 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 [9]
  :g!/^\s*#/d - delete all lines without a #

Macros

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

(macros are saved in registers)

Registers

"adw
  delete word into the a register
"_dw
  delete word into no register

"*
  system clipboard

Spaces

Buffers

A single window.

:b [buffer number of any part of name]
  switch to buffer
set wildchar=<Tab> wildmenu wildmode=full
  with this, :b [tab] gives a menu
  • spinner.vim : fast buffer/file/tab/window switching plugin with only 3 keys.

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
:qall
  quit all buffer windows on current tab
  • golden-ratio : Resize windows automatically using the Golden Ratio

dwm.vim

map <silent> <C-J> <C-W>w
map <silent> <C-K> <C-W>W
map <silent> <C-,> :call DWM_Rotate(0)<CR>
map <silent> <C-.> :call DWM_Rotate(1)<CR>

map <silent> <C-N> :call DWM_New()<CR>
map <silent> <C-C> :call DWM_Close()<CR>
map <silent> <C-Space> :call DWM_Focus()<CR>
map <silent> <C-@> :call DWM_Focus()<CR>

map <silent> <C-H> :call DWM_GrowMaster()<CR>
map <silent> <C-L> :call DWM_ShrinkMaster()<CR>

Tabs

:tab new
  or
:tabe
  create new tab

:tabc
  close tab
:tabs
  list existing 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
:bufdo qall
  send qall command to all tabs
:wqall!
:xall!
  to check

Client/server

Sessions

:mks sessionsave.file
  save session

vim -S sessionsave.file
  load session from cli
  • session.vim improves upon Vim's built-in :mksession command by enabling you to easily and (if you want) automatically persist and restore your Vim editing sessions. It works by generating a Vim script that restores your current settings and the arrangement of tab pages and/or split windows and the files they contain.

Coding

  • TextObjectify is a Vim plugin which improves text-objects
  • repmo.vim - repeat motions for which a count was given
  • movar is a Vim plugin that adds a couple of movements that make working with variables easier.
  • CamelCaseMotion - A vim script to provide CamelCase motion through words
  • textobj-word-column.vim - word-based column text-object makes operating on columns of code conceptually simpler and reduces keystrokes.
  • Surround.vim is all about "surroundings": parentheses, brackets, quotes, XML tags, and more. The plugin provides mappings to easily delete, change and add such surroundings in pairs.
  • vim-pasta - Pasting in Vim with indentation adjusted to destination context.
  • delimitMate provides insert mode auto-completion for quotes, parens, brackets, etc.

Undo

u
  undo last change
Ctrl-u
  undo whilst in insert mode 
U
  undo all changes to current line
Ctrl-r
  redo
Ctrl-g
  create new undo point
  • Gundo is a plugin to make browsing the undo tree less painful.

Completion

Ctrl-n
  autocomplete keyword forwards
Ctrl-p
  autocomplete keyword backwards
Ctrl-x 
  completion mode
  • YouCompleteMe YouCompleteMe is a fast, as-you-type, fuzzy-search code completion engine for Vim. It has several completion engines: an identifier-based engine that works with every programming language, a semantic, Clang-based engine that provides native semantic code completion for C/C++/Objective-C/Objective-C++ (from now on referred to as "the C-family languages"), a Jedi-based completion engine for Python and an omnifunc-based completer that uses data from Vim's omnicomplete system to provide semantic completions for many other languages (Ruby, PHP etc.).[13]
  • neocomplcache is the abbreviation of "neo-completion with cache". It provides keyword completion system by maintaining a cache of keywords in the current buffer. neocomplcache could be customized easily and has a lot more features than the Vim's standard completion feature.
  • Supertab is a vim plugin which allows you to use <Tab> for all your insert completion needs (:help ins-completion).

Interface

  • vim-pad - A quick notetaking plugin for vim.
  • vimroom - Simulating a vaguely WriteRoom-like environment in Vim.
  • scratch.vim - Plugin to create and use a scratch Vim buffer

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

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

vim#diff 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
  • gitgutter.vim - shows a git diff in the 'gutter' (sign column). It shows whether each line has been added, modified, and where lines have been removed. breaks vim on script load on server side git repos for me.
  • vim-signify - Sy shows all added, deleted and modified lines since the last commit via Vim its sign column. It supports several version control systems.
  • gitv is a ‘gitk clone’ plugin for the text editor Vim. The goal is to give you a similar set of functionality as a repository viewer. Using this plugin you can view a repository’s history including branching and merging, you can see which commits refs point to. You can quickly and easily view what changed to which files and when. You can perform arbitrary diffs (using Vim’s excellent built in diff functionality) and you can easily check out whole commits and branches or just individual files if need be.
  • gitsessions.vim - Automatically saves and loads sessions based on the current working directory and git branch name after the first manual save.

Syntax

General

  • Syntastic is a syntax checking plugin that runs files through external syntax checkers and displays any resulting errors to the user. This can be done on demand, or automatically as files are saved. If syntax errors are detected, the user is notified and is happy because they didn't have to compile their code or execute their script to find them. At the time of this writing, syntax checking plugins exist for applescript, c, coffee, cpp, css, cucumber, cuda, docbk, erlang, eruby, fortran, gentoo_metadata, go, haml, haskell, html, javascript, json, less, lua, matlab, perl, php, puppet, python, rst, ruby, sass/scss, sh, tcl, tex, vala, xhtml, xml, xslt, yaml, zpt
  • taglist.vim - Source code browser (supports C/C++, java, perl, python, tcl, sql, php, etc)
    • Taglist-plus provides excellent Javascript support via jsctags- best fork of the fork

HTML

Highlighting

Creating and editing

  • xml.vim : helps editing xml (and [x]html, sgml, xslt) files

CSS

  • https://github.com/ap/vim-css-color - underlays the hexadecimal CSS colorcodes with their real color. The foreground color is selected appositely. So #FF0000 will look as hot as a fire engine! + Highlighting multiple colors on the same line (not sure if anyone uses it though), rgb and rgba color notation for all those fancy CSS3 niceties

SCSS

JavaScript

PHP

Other

Comments

Doesn't work with some languages

gc{motion}
  toggle comments (for small comments)
gcc
  toggle comment for the current line
gC{motion}
  comment region
gCc
  comment the current line

Arrow keys

Config

I chose /usr/share/config/vim

/usr/local/share/vim is a default $VIMRUNTIME though

Vimscripts

Script Management

  • NeoBundle is Vim plugin manager based on Vundle.
  • VAM is short name for vim-addon-manager. You declare a set of plugins. VAM will fetch & activate them at startup or runtime depending on your needs. Activating means handling runtimepath and making sure all .vim file get sourced.

Dashboard

  • https://github.com/mhinz/vim-startify - If you start Vim without giving any filenames to it (or pipe stuff to it so it reads from STDIN), startify will show a small but pretty start screen which shows recently used files (using viminfo) and sessions by default. Additionally, you can define bookmarks, thus entries for files that always should be available in the start screen. It also eases handling of loading and saving sessions by only working with a certain directory.

GitHub

Status

  • Powerline is a utility plugin which allows you to create better-looking, more functional Vim statuslines.
  • SmartusLine is Vim plugin that changes the color of the statusline of the focused window according with the current mode (normal/insert/replace)

Cursors

Services

  • Vmail is a Vim interface to Gmail.

To sort


  • Vim-OrgMode - Text outlining and task management for Vim based on Emacs’ Org-Mode


  • netrw.vim - Network oriented reading, writing, and browsing (keywords: netrw ftp scp)



  • ZoomWin - Zoom in/out of windows (toggle between one window and multi-window)


  • subvim Vim customized to be like SublimeText


  • spf13-vim is a distribution of vim plugins and resources for Vim, GVim and MacVim. It is a completely cross platform distribution that stays true to the feel of vim while providing modern features like a plugin management system, autocomplete, tags and tons more.


Creating

System

  • Conque is a Vim plugin which allows you to run interactive programs, such as bash on linux or powershell.exe on Windows, inside a Vim buffer. In other words it is a terminal emulator which uses a Vim buffer to display the program output.
:ConqueTerm zsh

:ConqueTermSplit <command>
:ConqueTermVSplit <command>
:ConqueTermTab <command>
  • browser-connect.vim - implements a VIM interface for browser-connect-server in order to provide a live coding environment similar to the one currently available in LightTable.

Colour themes

Mouse

Voice

Tmux integration

  • vimux - Easily interact with tmux from vim.

Remote

  • netrw.vim : Network oriented reading, writing, and browsing

Collaboration

Encryption

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)

Vim everywhere

Javascript

hmm.

Textaid

Chrome extension. As Chrome can't spawn child processes, text is passed to a local server which launches Vim.

It's All Text

Firefox extension. Only does textarea, no WYSIWYG support. Doesn't launch Vim correctly be default if FF not launched from terminal.

Other