This shows you the differences between two versions of the page.
vim [2009/01/22 10:59] |
vim [2009/01/22 10:59] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== VIM snippets ====== | ||
+ | ===== Redirect Command output ===== | ||
+ | '': | ||
+ | ===== Align columns in ASCII-art table (ala wiki syntax) ===== | ||
+ | |||
+ | < | ||
+ | |||
+ | To repeat this command numerous times, record executing it as a macro, then use normal macro repetition. | ||
+ | |||
+ | ==== Wtf? ==== | ||
+ | |||
+ | The break down is below, but the gist is that we find lines of text which don't have a " | ||
+ | |||
+ | < | ||
+ | '<,'> | ||
+ | |||
+ | v/ --- / Check each line against Regular Expression " | ||
+ | | ||
+ | |||
+ | ^|[^|]\+\%27v| | ||
+ | characters ( [^|]\+ ), followed by an assertion at we're in | ||
+ | | ||
+ | |||
+ | s/ --- / ### / | ||
+ | |||
+ | \(^|[^|]\+\)| | ||
+ | or more not-vertical-bars. This capture should be immediately | ||
+ | | ||
+ | |||
+ | \1 | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ===== A complete function for aligning ASCII-art tables ===== | ||
+ | |||
+ | To use: source this into Vim (": | ||
+ | |||
+ | <code vim> | ||
+ | " Align a single ASCII-art table column | ||
+ | " tcol: Which column of the table to align (i.e. first column = 1, second = 2 etc) | ||
+ | " vcol: Align table column to which virtual/ | ||
+ | function! AlignTableColumn ( tcol, vcol ) range | ||
+ | let i = a:vcol - a:tcol " Worst case | ||
+ | while i | ||
+ | " Edge one screen column closer to desired location | ||
+ | execute ' | ||
+ | let i -= 1 | ||
+ | endwhile | ||
+ | endfunction | ||
+ | |||
+ | " Align a whole ASCII-art table | ||
+ | " Tables are expected to be drawn using " | ||
+ | " | ||
+ | " Usage: Invoke this function with the range of lines the table occupies, | ||
+ | " including the header - which will be used as the " | ||
+ | " | ||
+ | " TODO: Tally the maximum width for each column by scanning all rows, and use | ||
+ | " these values to calculate new column boundry locations | ||
+ | function! AlignTable() range | ||
+ | let header = getline(a: | ||
+ | let bars = 0 | ||
+ | |||
+ | let i = 0 | ||
+ | " Count number of " | ||
+ | while i < strlen(header) | ||
+ | if header[i] == ' | ||
+ | let bars += 1 | ||
+ | endif | ||
+ | let i += 1 | ||
+ | endwhile | ||
+ | unlet i | ||
+ | unlet header | ||
+ | |||
+ | " Now get column widths for each row, keeping the maximum in colwidth | ||
+ | let colwidth = [] | ||
+ | let i = bars | ||
+ | " Init list of correct length | ||
+ | while i | ||
+ | let colwidth += [0] | ||
+ | let i -= 1 | ||
+ | endwhile | ||
+ | unlet i | ||
+ | |||
+ | let i = a:firstline | ||
+ | while i <= a:lastline | ||
+ | let row = getline(i) | ||
+ | let curbar = 0 | ||
+ | let pos = 0 | ||
+ | let poslist = [] | ||
+ | while curbar < bars | ||
+ | let pos = match(row, ' | ||
+ | let poslist += [pos] | ||
+ | let pos += 1 " Move past current match, ready for next | ||
+ | if len(poslist) > 1 | ||
+ | if colwidth[curbar] < ( poslist[curbar] - poslist[(curbar-1)] ) | ||
+ | let colwidth[curbar] = ( poslist[curbar] - poslist[(curbar-1)] ) | ||
+ | endif | ||
+ | endif | ||
+ | let curbar += 1 | ||
+ | endwhile | ||
+ | let i += 1 | ||
+ | endwhile | ||
+ | |||
+ | let poslist = [ match(getline(a: | ||
+ | call remove(colwidth, | ||
+ | for width in colwidth | ||
+ | let poslist += [ (poslist[(len(poslist)-1)] + width) ] | ||
+ | endfor | ||
+ | |||
+ | " NB There is one more bar than there are cols, and we ignore the first | ||
+ | " bar | ||
+ | let i = 1 | ||
+ | call remove(poslist, | ||
+ | for vcol in poslist | ||
+ | " Note that every bar must be moved at least once to avoid bugs, hence | ||
+ | " the (vcol+1) below | ||
+ | execute (a: | ||
+ | let i += 1 | ||
+ | endfor | ||
+ | endfunction | ||
+ | </ | ||
+ | |||
+ | ===== XMLFolding ===== | ||
+ | Adapted from [[http:// | ||
+ | |||
+ | <code vim> | ||
+ | " Basic vim commands for folding definition | ||
+ | syn sync fromstart | ||
+ | set foldmethod=syntax | ||
+ | |||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | syn region XMLFold start=#< | ||
+ | " | ||
+ | " | ||
+ | |||
+ | syn match XMLCData "< | ||
+ | syn match XMLCommentFold "< | ||
+ | |||
+ | |||
+ | " Label shown for folded lines | ||
+ | set foldtext=XMLFoldLabel() | ||
+ | fun! XMLFoldLabel() | ||
+ | let getcontent = substitute(getline(v: | ||
+ | let linestart = substitute(v: | ||
+ | return linestart . " " . getcontent | ||
+ | endfunction | ||
+ | </ | ||
+ | (The line drawings were made with the excellent [[http:// | ||
+ | |||
+ | ===== Win32 Paths ===== | ||
+ | When under Cygwin, ''' | ||
+ | |||
+ | You can fix this with: | ||
+ | < | ||
+ | set isfname=@, | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | ===== Pop-up menu colours ===== | ||
+ | The default pop-up menu colour for auto-complete (e.g. '' | ||
+ | |||
+ | <code vim> | ||
+ | " ~/ | ||
+ | "Pmenu | ||
+ | call < | ||
+ | call < | ||
+ | call < | ||
+ | call < | ||
+ | </ | ||
+ | |||
+ | See also [[http:// | ||
+ | |||
+ | Here's how it looks: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | I really like the function summary in the lower pane, and all this for a Python module I wrote myself! I'm amazed that vim can work out the type of '' |