Search this site


[prev]  Page 2 of 2

Metadata

Articles

Projects

Presentations

programming sanity

A project I'm working on at work contains piles of some of the worst-written code I've seen in a long time. Top that with three tons of commented-out source code and you've got me angry.

I use vim (NOT gvim). I don't use syntax highlighting. The bright colors available in the terminal are too contrasting to be useful for syntax highlighting, so I have it off at all times. However, with this new project, there are so many lines of code that are just commented out it becomes extremely difficult to read said code.

Enter vim's syntax highlighting. I hate coloring of most things, but having comments a particular color is acceptable. I would prefer these comments be dark-but-visible-on-a-black-background shade of grey, so I try this in vim:

hi Comments ctermfg=grey
This makes comments appear grey. However, they grey is not quite dark enough, so I'll need to fix that. The ANSI color number for grey is 7. So in my ~/.Xresources file, I need to redefine what this color really looks like, specifically making it darker. I used xcolors(1) to show me a list of colors with names that X would recognize, and I saw that 'grey' was a decent shade of, you guessed it, grey. So I put this in my .Xresources file:
XTerm*color7: grey
Now run xrdb -merge ~/.Xresources and new xterms will use this color. Openning up vim and it looks just fine.

One special mention here, is the difference between my CRT monitor and the LCD on my laptop. The default grey color (7) in X on my laptop is perfectly visible. However, on my CRT I need to darken this color to make it visible to me. Your mileage may vary - the CRT I'm currently using is a pretty crappy 19" trinitron with some contrast issues, so using another monitor may clear this up.

updated vimrc and zshrc available

I recently made a few changes to my vimrc and zshrc. The changes are somewhat trivial, but made working in zsh and vim easier. You can get the files here:

Downloadables:

vim autoindenting

My hatred for other people's indenting habbits has now been calmed by the awesomeness that is vim. The command, =, will let you reindent the current line (or the whole file, or whatever) to the settings you use in vim already.
So, instead of dealing with people being dumb and using spaces for *everything* this magical keystroke series will fix my woes:
1G=G
This will cause reindentation of every line in the file you are editing.

Woot.

vim - indenting

So rather than start writing my own indent function, I decided to read more about cindent. The option, cinoptions, lets you tell vim how to react to things. What I was looking for was:
When I am typing a function call, and break the line because it's too long, I'd like the new line to start aligned with the open parenthesis, like this:

System.out.println("Hello there, how are you? kthx." + 
                   "Java is fun");
		   ^

I want vim to automatically indent to the column position marked by the caret, ^ - Turns out vim has this functionality already (duh, it has almost evrything).

:set cinoptions=(0

This, when cindent is enabled, will cause vim to auto indent to match your parentheses usage.

vim rocks me.

So yeah - I was explaining the beauties of cindent and folding in vim to a werkt today and when I went back to my room decided to pull up the help on cindent, etc. Looked at indentexpr and decided I needed to learn more vim. What indentexpr lets you do is set a vim function (Or perl/python/ruby) tell vim how much to indent.

More on this later after I write a vim script to do exactly what I want...

Neat vim/screen trick.

For some time I've been wanting to be able to pop open a small split-pane in vim to run simple commands like, oh.. I don't know.. javac and such. Just for temporary, quick shell access.
Tonight, as I was browsing through the screen manpage, I saw that screen allowed you to split the window into different regions. Using this and a quick vim key mapping, I was able to do it without much trouble!

The vim keymapping:

map \s :silent !screener.sh<CR>
Contents of screener.sh
screen -X split
screen -X focus down
screen -X screen sh -c "cd $PWD; $SHELL; screen -X remove"

Thanks to screen being smart, you don't have to specify what screen session to send these commands to, which made it 10000% easier, as we don't have to bother trying to figure out which screen session is running vim. You could optionally add a :!screen -X resize <whatever> before to resize the lower frame to a specific size. See the screen manpage for more information on how to use screen -X and the resize command.

The results of hitting \s in vim can be found in the screenshot on the right. After logging out of that shell, the newly-split screen disappears and returns you to your original, full-screen vim.

More vim fun...

the ! command is awesome. It lets you filter a given number of lines (or characters) through an external program. Here's an example: Let's say we have a file that has blank lines on it and we want to remove those blank lines? Here's what the file I made looks like:
Hello there

How are you



Plz kthx?

So, we want those blank lines gone, right? We can use the ! command with grep to do that! Type this:
!Ggrep -v '^$'

Now we're set, the result should look like this:
Hello there
How are you
Plz kthx?

More vim tricks..

I've often wanted a command in vi and vim that'll increment a number that the cursor is over.
In nvi (freebsd), the command is #(+#|-) where ## and #+ will increment, #- will decrement. In vim the command is ^A and ^X. ^A increments while ^X decrements Both versions of vi let you add|subtract a given amount, for example: 300## will add 300 to the number. Replace ## with ^A in vim for the same effect. Nvi has a maximum of (+/-)2^31, while VIM has a range of (+/-)2^32.

Cool vim command of the day.

Accidentally hit # in vim. :help # showed that this goes in reverse looking for the word the cursor is on. * is the opposite and will jump forward. In vim type :help * for more info! I love vim :(

VIM Advetures

Was thumbing through some .vim files I'd downloaded and currently use (like the cvscommand one, it rocks me) and saw that it had somehow managed to collapse sections into single lines that I was able to expand.
After perusing the disgustingly thorough documentation for information on this, i found a section on folding (:help fold in vim).

Folding makes code quite easy to view. I'm currently using it to section-out piles of documentation that I've written and would prefer not seeing as I'm editing.
VIM rocks me.
Also found out how to properly use registers (I always accidentally hit 'q' in my speedytyping and it'd say "recording" and I never bothered looking up what was going on)
I think i'm going to do two or so vi seminars early next year to prevent the CS department from turning everyone into emacs kiddies.