Sunday, March 31, 2013

More Vim stuff, fix indentation and and remove blank lines

I've been using Vim as my ide, and it's been wonderful.

some additional cool things I've found.

you can have your whole file's indentation fixed with gg=G
it may be helpful to :set sw= and :set tabstop= to 4 or 2, and turn cindent on :set cindent, to handle brackets.

gg goes to the top of the file = fixes indentation, and GG does the same to the bottom of the file

you can erase blank lines with :g/^$/d

:g makes it a global command executed on all instances which match your search pattern, ^$ (start of line and end of line with nothing in between) matches blank lines, and d deletes

you might also use something like :g/^\s*$/d to remove all lines with either blanks or only whitespace

for Perl, I recently used :%s/\(\S\+\)\s*\(#.*\)/\r\U\2/g which cuts any comments from the end of a line and inserts them on a new line, in uppercase and :%s/\s*\(#.*\)/U\1/g which removes any spaces between a comment and the beginning of the line and makes the comment uppercase to be useful.



Wednesday, March 13, 2013

new VIM version autocomment fix

I compiled the most recent version of VIM.  It has an annoying behavior

if I start a comment in insert mode and press enter in insert mode, the next line is automatically a comment too.

This means cutting and pasting from outside vim will fail at the first comment.  I guess it's useful for long comments, but mostly it's an annoyance.

However, by piecing together stuff from a few web pages and vim doc you can easily fix this.

You need to change your format options

you can do this from inside vim with set formatoptions. you can see your current formatoptions with :setlocal

So briefly:

To turn off, you need to disable the q option in formatoptions 
:setlocal find your format options then in your .vimrc au FileType * set formatoptions= after the =, put your format options without the q 
see http://vimdoc.sourceforge.net/htmldoc/change.html#format-comments for more details


* after FileType can be a language... so c, perl, ruby, python, etc.  

Thanks to http://www.kevinslonka.com/index.php?section=1&blog=179&page=1 for getting me started.  The comments have several clever fixes, including a compromise to stop commenting if the commented line ends with a space, but I haven't gotten that working myself.  You can also fix just the pasting with :set paste, if you prefer.