Difference between revisions of "Vim"

From WTFwiki
Jump to navigation Jump to search
(Wordwrapping in vim)
(add brief details on marks)
Line 165: Line 165:
 
   :set tw=80
 
   :set tw=80
 
   gggqG
 
   gggqG
 +
 +
== Using "marks" to move around ==
 +
 +
If you're editing a large file (say, a few hundred/thousand lines..) and need to move back and forth between functions, etc:
 +
 +
Set a mark on the current position:
 +
 +
  ma
 +
 +
'a' in that case will be the name of your mark; and can be any alpha[numerical?] character.
 +
 +
To later reference it, you can move back to that position by typing:
 +
 +
  'a
  
 
== Resources ==
 
== Resources ==

Revision as of 10:18, 21 September 2007

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 your 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 BufReadPost,BufNewFile,FileReadPost * call SetCommentType()
 vmap ^[q <Esc>:call VisualComment(line("'<"), line("'>"))<CR>
 nmap ^[q :call Comment(line('.'))<CR>
 function SetCommentType()
       "no switch statement, feh
       if &ft == 'ruby'
               let b:comment_begin = '#'
       elseif &ft == 'vim'
               let b:comment_begin = '"'
       elseif &ft == 'c'
               let b:comment_begin = '/*'
               let b:comment_end = '*/'
       elseif &ft == 'xml' || &ft == '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:

 " ###### 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 ######
 "grab the contents of the file to register 'j'
 autocmd BufWritePre,FileWritePre *.nc normal gg"jyG
 autocmd BufWritePre,FileWritePre *.nc set bin "binary mode
 "encrypt the data
 autocmd BufWritePre,FileWritePre *.nc '[,']!mcrypt
 autocmd BufWritePost,FileWritePost *.nc set nobin "turn off binary
 "restore the contents of the register 'j'
 autocmd BufWritePost,FileWritePost *.nc normal ggdG"jp
 "delete the contents of the register 'j'
 autocmd BufWritePost,FileWritePost *.nc :let @j = ""
 "trigger a screen redraw cuz things are fucked up
 autocmd BufWritePost,FileWritePost *.nc normal ^L
 " ###### SAVING - OLD WAY ######
 autocmd BufWritePre,FileWritePre *.nc set bin "binary mode
 "encrypt the data
 autocmd BufWritePre,FileWritePre *.nc '[,']!mcrypt
 " decrypt the data 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

WARNING: Don't use :wq with this, if your 2 passphrases don't match the save will fail and it'll write gibberish to the file and then exit. ALWAYS save and make sure that shit went OK before you quit!

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.

UPDATE': new version that uses registers, it doesn't need you to enter the passphrase an additional time to save the file.

TODO: can I pipe the contents of the register to a command instead?

Making comments more readable in colored terminals

The default vim-color scheme (which is very nice in most cases) makes comments dark-blue by default. This is damn hard to read on some screens (especially LCDs that you aren't in the optimal viewing angle for). Here's a quick one-liner to bold commented out lines (which makes them easier to read):

 autocmd BufReadPre,FileReadPre * hi! Comment ctermfg=blue cterm=bold

Of course, you can change the highlight mode as you wish (:help highlight).

Wordwrap an entire file

 :set wm=4
 :set tw=80
 gggqG

Using "marks" to move around

If you're editing a large file (say, a few hundred/thousand lines..) and need to move back and forth between functions, etc:

Set a mark on the current position:

 ma

'a' in that case will be the name of your mark; and can be any alpha[numerical?] character.

To later reference it, you can move back to that position by typing:

 'a

Resources

Vim.org - Vim's home page, lots of tips and scripts of sometimes dubious value. Vi-Improved.org - Contains example .vimrc and an excellent walkthrough of vim's features

--Andrew 22:44, 15 September 2006 (EDT)