Difference between revisions of "Vim"
m (Editing encrypted files with vim moved to Vim: Do the intellectual shuffle!) |
|||
Line 1: | Line 1: | ||
+ | = VIM = | ||
+ | [http://vim.org Vim] ('''V''' '''IM'''proved) is a more featureful clone of the venerable unix text editor vi. It fucking rocks. | ||
+ | |||
+ | |||
+ | '''NOTE''': the literal paste-job I've done here fucks with the alt and ctrl bindings, you'll most likely have to retype them yourself in vim (in insert mode press ctrl-v and then the keycombo (eg. alt-q)). | ||
+ | |||
+ | == Highlighting long lines == | ||
+ | Ever needed to make sure you lines fit on an 80-column line even though you prefer editing in a larger terminal? Here's some vimscript that when toggled with F2 will set the textwidth to 80 and highlight any characters that overflow the 80 character limit: | ||
+ | |||
+ | " Global tracker var | ||
+ | let g:HighlightLongLines = 0 "set to 0 so we can run it to enable | ||
+ | " Function to toggle highlighting of lines longer than 80 character | ||
+ | " It also toggles textwidth to 80 so newlines are forced when typing | ||
+ | fu! ToggleHighlightLongLines() | ||
+ | if(g:HighlightLongLines == 1) | ||
+ | " Disable highlighting and textwidth | ||
+ | highlight clear rightMargin | ||
+ | set textwidth=0 | ||
+ | let g:HighlightLongLines = 0 | ||
+ | else | ||
+ | " Enable highlighting and textwidth | ||
+ | set textwidth=80 | ||
+ | highlight rightMargin ctermbg=LightRed guibg=LightRed | ||
+ | match rightMargin /\%>81v/ | ||
+ | let g:HighlightLongLines = 1 | ||
+ | endif | ||
+ | endfunction | ||
+ | |||
+ | map <F2> :call ToggleHighlightLongLines()<CR> | ||
+ | |||
+ | == Simple commenting script == | ||
+ | All the vim commenting scripts I tried sucked and were like 400 lines of code. So, I made a '''very''' simple one: | ||
+ | |||
+ | " try to detect and set the comment type | ||
+ | autocmd BufRead,BufNewFile *.* call SetCommentType() | ||
+ | |||
+ | vmap ^[q <Esc>:call VisualComment(line("'<"), line("'>"))<CR> | ||
+ | nmap ^[q :call Comment(line('.'))<CR> | ||
+ | |||
+ | function SetCommentType() | ||
+ | " bail if no syntax type is defined | ||
+ | if !exists('b:current_syntax') | ||
+ | return | ||
+ | end | ||
+ | "hmm, this seems to be the only var we can use... | ||
+ | let syn = b:current_syntax | ||
+ | "no switch statement, feh | ||
+ | if syn == 'ruby' | ||
+ | let b:comment_begin = '#' | ||
+ | elseif syn == 'vim' | ||
+ | let b:comment_begin = '"' | ||
+ | elseif syn == 'c' | ||
+ | let b:comment_begin = '/*' | ||
+ | let b:comment_end = '*/' | ||
+ | elseif syn == 'xml' || syn == 'html' | ||
+ | let b:comment_begin = '<!--' | ||
+ | let b:comment_end = '-->' | ||
+ | " TODO - other languages | ||
+ | else | ||
+ | "Unhandled syntax | ||
+ | end | ||
+ | endfunction | ||
+ | |||
+ | " (Un)Comment all selected lines | ||
+ | function VisualComment(b, e) | ||
+ | if !(exists('b:comment_begin')) | ||
+ | return | ||
+ | end | ||
+ | "iterate over selected lines | ||
+ | for i in range(a:b, a:e) | ||
+ | call Comment(i) | ||
+ | endfor | ||
+ | endfunction | ||
+ | |||
+ | " (Un)Comment out a line using the comment type defined for this syntax type | ||
+ | function Comment(i) | ||
+ | " comment details not defined... | ||
+ | if !exists('b:comment_begin') | ||
+ | return | ||
+ | end | ||
+ | |||
+ | " test for an empty line | ||
+ | if match(getline(a:i), "^$") == 0 | ||
+ | return | ||
+ | end | ||
+ | if exists('b:comment_end') | ||
+ | let comment_begin_esc = escape(b:comment_begin, '*') | ||
+ | let comment_end_esc = escape(b:comment_end, '*') | ||
+ | let regex = ('^\s*'.comment_begin_esc.'.\+'.comment_end_esc.'\s*$') | ||
+ | else | ||
+ | let comment_begin_esc = escape(b:comment_begin, '*') | ||
+ | let regex = '^\s*'.comment_begin_esc.'.\+$' | ||
+ | end | ||
+ | |||
+ | let idx = match(getline(a:i), regex) | ||
+ | |||
+ | if idx >= 0 | ||
+ | let newline = substitute(getline(a:i), comment_begin_esc, '', '') | ||
+ | if exists('b:comment_end') | ||
+ | let ridx = strridx(newline, comment_end_esc) | ||
+ | let part1 = strpart(newline, 0, ridx) | ||
+ | let part2 = strpart(newline, ridx) | ||
+ | let newline = part1.substitute(part2, comment_end_esc, '', '') | ||
+ | end | ||
+ | call setline(a:i, newline) | ||
+ | else | ||
+ | let newline = substitute(getline(a:i), '^', comment_begin_esc, '') | ||
+ | if exists('b:comment_end') | ||
+ | let newline = substitute(newline, '$', comment_end_esc, '') | ||
+ | end | ||
+ | call setline(a:i, newline) | ||
+ | end | ||
+ | endfunction | ||
+ | |||
+ | '''NOTE''': it should be trivial to add new comment types, just be aware that any special characters (like the * in the C mode) will need to be escaped. I was too lazy to add all the potentials to the escape() call but I couldn't think of any others that might be used in any comment system I'm familliar with anyway... | ||
+ | |||
+ | == Editing mcrypted files with vim == | ||
Ever wanted to edit some files you've encrypted with mcrypt(1), but didn't want nasty unencrypted files lying around? With Vim and a little autocommands magic you can: | Ever wanted to edit some files you've encrypted with mcrypt(1), but didn't want nasty unencrypted files lying around? With Vim and a little autocommands magic you can: | ||
Revision as of 21:39, 15 September 2006
VIM
Vim (V IMproved) is a more featureful clone of the venerable unix text editor vi. It fucking rocks.
NOTE: the literal paste-job I've done here fucks with the alt and ctrl bindings, you'll most likely have to retype them yourself in vim (in insert mode press ctrl-v and then the keycombo (eg. alt-q)).
Highlighting long lines
Ever needed to make sure you lines fit on an 80-column line even though you prefer editing in a larger terminal? Here's some vimscript that when toggled with F2 will set the textwidth to 80 and highlight any characters that overflow the 80 character limit:
" Global tracker var let g:HighlightLongLines = 0 "set to 0 so we can run it to enable " Function to toggle highlighting of lines longer than 80 character " It also toggles textwidth to 80 so newlines are forced when typing fu! ToggleHighlightLongLines() if(g:HighlightLongLines == 1) " Disable highlighting and textwidth highlight clear rightMargin set textwidth=0 let g:HighlightLongLines = 0 else " Enable highlighting and textwidth set textwidth=80 highlight rightMargin ctermbg=LightRed guibg=LightRed match rightMargin /\%>81v/ let g:HighlightLongLines = 1 endif endfunction
map <F2> :call ToggleHighlightLongLines()<CR>
Simple commenting script
All the vim commenting scripts I tried sucked and were like 400 lines of code. So, I made a very simple one:
" try to detect and set the comment type autocmd BufRead,BufNewFile *.* call SetCommentType()
vmap ^[q <Esc>:call VisualComment(line("'<"), line("'>"))<CR> nmap ^[q :call Comment(line('.'))<CR>
function SetCommentType() " bail if no syntax type is defined if !exists('b:current_syntax') return end "hmm, this seems to be the only var we can use... let syn = b:current_syntax "no switch statement, feh if syn == 'ruby' let b:comment_begin = '#' elseif syn == 'vim' let b:comment_begin = '"' elseif syn == 'c' let b:comment_begin = '/*' let b:comment_end = '*/' elseif syn == 'xml' || syn == 'html' let b:comment_begin = " TODO - other languages else "Unhandled syntax end endfunction
" (Un)Comment all selected lines function VisualComment(b, e) if !(exists('b:comment_begin')) return end "iterate over selected lines for i in range(a:b, a:e) call Comment(i) endfor endfunction
" (Un)Comment out a line using the comment type defined for this syntax type function Comment(i) " comment details not defined... if !exists('b:comment_begin') return end
" test for an empty line if match(getline(a:i), "^$") == 0 return end if exists('b:comment_end') let comment_begin_esc = escape(b:comment_begin, '*') let comment_end_esc = escape(b:comment_end, '*') let regex = ('^\s*'.comment_begin_esc.'.\+'.comment_end_esc.'\s*$') else let comment_begin_esc = escape(b:comment_begin, '*') let regex = '^\s*'.comment_begin_esc.'.\+$' end
let idx = match(getline(a:i), regex)
if idx >= 0 let newline = substitute(getline(a:i), comment_begin_esc, , ) if exists('b:comment_end') let ridx = strridx(newline, comment_end_esc) let part1 = strpart(newline, 0, ridx) let part2 = strpart(newline, ridx) let newline = part1.substitute(part2, comment_end_esc, , ) end call setline(a:i, newline) else let newline = substitute(getline(a:i), '^', comment_begin_esc, ) if exists('b:comment_end') let newline = substitute(newline, '$', comment_end_esc, ) end call setline(a:i, newline) end endfunction
NOTE: it should be trivial to add new comment types, just be aware that any special characters (like the * in the C mode) will need to be escaped. I was too lazy to add all the potentials to the escape() call but I couldn't think of any others that might be used in any comment system I'm familliar with anyway...
Editing mcrypted files with vim
Ever wanted to edit some files you've encrypted with mcrypt(1), but didn't want nasty unencrypted files lying around? With Vim and a little autocommands magic you can:
" ###### LOADING ###### autocmd BufReadPre,FileReadPre *.nc set bin "binary mode autocmd BufReadPre,FileReadPre *.nc set noshelltemp "force the use of pipes autocmd BufReadPre,FileReadPre *.nc set noswapfile "disable swapfiles "decrypt the file autocmd BufReadPost,FileReadPost *.nc '[,']!mcrypt -d autocmd BufReadPost,FileReadPost *.nc set nobin "turn off binary mode
" ###### SAVING ###### autocmd BufWritePre,FileWritePre *.nc set bin "binary mode "encrypt the data autocmd BufWritePre,FileWritePre *.nc '[,']!mcrypt "decrypt the buffer again autocmd BufWritePost,FileWritePost *.nc '[,']!mcrypt -d autocmd BufWritePost,FileWritePost *.nc set nobin "turn off binary "trigger a screen redraw cuz things are fucked up autocmd BufWritePost,FileWritePost *.nc normal ^L
NOTE: this may not be entirely secure, this is just something I pulled out of my ass while offline and browsing the vim documentation. It seems to work as desired, but you never know.
TODO: make the save part suck less (currently you need to type the passphrase *3* times, and vim gets all screwed up (hence the ctrl-l as the last step which forces a redraw). I'm not sure exactly how to do this though.
--Andrew 22:17, 15 September 2006 (EDT)