Saari Development

Vim: .gvimrc on Mac

Posted in Uncategorized by imsaar on May 22nd, 2007
" Copied from $VIMRUNTIME/gvimrc_example.vim

" An example for a gvimrc file." The commands in this are executed when the GUI is started."" Maintainer: Bram Moolenaar <Bram@vim.org>" Last change:  2001 Sep 02"" To use it, copy it to"     for Unix and OS/2:  ~/.gvimrc"       for Amiga:  s:.gvimrc"  for MS-DOS and Win32:  $VIM\_gvimrc"     for OpenVMS:  sys$login:.gvimrc

" Make external commands work through a pipe instead of a pseudo-tty"set noguipty

" set the X11 font to use" set guifont=-misc-fixed-medium-r-normal--14-130-75-75-c-70-iso8859-1

set ch=2    " Make command line two lines high

set mousehide   " Hide the mouse when typing textset antialias   " setting antialiasing for guifont

" Make shift-insert work like in Xtermmap <S-Insert> <MiddleMouse>map! <S-Insert> <MiddleMouse>

" Only do this for Vim version 5.0 and later.if version >= 500

  " I like highlighting strings inside C comments  let c_comment_strings=1

  " Switch on syntax highlighting if it wasn't on yet.  if !exists("syntax_on")    syntax on  endif

  " Switch on search pattern highlighting.  set hlsearch

  " For Win32 version, have "K" lookup the keyword in a help file  "if has("win32")  "  let winhelpfile='windows.hlp'  "  map K :execute "!start winhlp32 -k <cword> " . winhelpfile <CR>  "endif

  " Set nice colors  " background for normal text is light grey  " Text below the last line is darker grey  " Cursor is green, Cyan when ":lmap" mappings are active  " Constants are not underlined but have a slightly lighter background  highlight Normal guibg=grey90  highlight Cursor guibg=Green guifg=NONE  highlight lCursor guibg=Cyan guifg=NONE  highlight NonText guibg=grey80  highlight Constant gui=NONE guibg=grey95  highlight Special gui=NONE guibg=grey95

  " over riding the above settings with my preferred settings  colorscheme torte  set background=darkendif

Vim: .vimrc on Mac

Posted in Uncategorized by imsaar on May 22nd, 2007
" Copied from $VIMRUNTIME/vimrc_example.vim

" An example for a vimrc file."" Maintainer: Bram Moolenaar <Bram@vim.org>" Last change:  2002 Sep 19"" To use it, copy it to"     for Unix and OS/2:  ~/.vimrc"       for Amiga:  s:.vimrc"  for MS-DOS and Win32:  $VIM\_vimrc"     for OpenVMS:  sys$login:.vimrc

" When started as "evim", evim.vim will already have done these settings.if v:progname =~? "evim"  finishendif

" Use Vim settings, rather then Vi settings (much better!)." This must be first, because it changes other options as a side effect.set nocompatible

" allow backspacing over everything in insert modeset backspace=indent,eol,start

if has("vms")  set nobackup    " do not keep a backup file, use versions insteadelse  set backup    " keep a backup fileendifset history=50    " keep 50 lines of command line historyset ruler   " show the cursor position all the timeset showcmd   " display incomplete commandsset incsearch   " do incremental searchingset number    " show line numberset tabstop=2set shiftwidth=2set expandtabset textwidth=80set background=dark

set gfn=Monaco:h14  " setting to a good Mac OS X font

" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries" let &guioptions = substitute(&guioptions, "t", "", "g")

" Don't use Ex mode, use Q for formattingmap Q gq

" This is an alternative that also works in block mode, but the deleted" text is lost and it only works for putting the current register."vnoremap p "_dp

" Switch syntax highlighting on, when the terminal has colors" Also switch on highlighting the last used search pattern.if &t_Co > 2 || has("gui_running")  syntax on  set hlsearchendif

" Only do this part when compiled with support for autocommands.if has("autocmd")

  " Enable file type detection.  " Use the default filetype settings, so that mail gets 'tw' set to 72,  " 'cindent' is on in C files, etc.  " Also load indent files, to automatically do language-dependent indenting.  filetype plugin indent on

  " Put these in an autocmd group, so that we can delete them easily.  augroup vimrcEx  au!

  " For all text files set 'textwidth' to 78 characters.  autocmd FileType text setlocal textwidth=78

  " When editing a file, always jump to the last known cursor position.  " Don't do it when the position is invalid or when inside an event handler  " (happens when dropping a file on gvim).  autocmd BufReadPost *    \ if line("'\"") > 0 && line("'\"") <= line("$") |    \   exe "normal g`\"" |    \ endif  autocmd BufRead *.rhtml set filetype=eruby

  augroup END

else

  set autoindent    " always set autoindenting on

endif " has("autocmd")

Vim: _vimrc on windows

Posted in Uncategorized by imsaar on May 22nd, 2007
set nocompatiblesource $VIMRUNTIME/vimrc_example.vimsource $VIMRUNTIME/mswin.vimbehave mswin

set diffexpr=MyDiff()function MyDiff()  let opt = '-a --binary '  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif  let arg1 = v:fname_in  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif  let arg2 = v:fname_new  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif  let arg3 = v:fname_out  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif  let eq = ''  if $VIMRUNTIME =~ ' '    if &sh =~ '\<cmd'      let cmd = '""' . $VIMRUNTIME . '\diff"'      let eq = '"'    else      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'    endif  else    let cmd = $VIMRUNTIME . '\diff'  endif  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eqendfunction

set guifont=Lucida_Console:h10:cANSIcolorscheme torte

Find all writeable files in a directory tree

Posted in Uncategorized by imsaar on March 20th, 2007

I needed to find if I forgot to p4 edit (or add) any of the files I was working with and needed to find all writeable files in my current directory structure (recursively). I did a quick search and found a newsgroup entry that helped (referenced below) but here is what you need to do :

find . -type f -perm +200

4 is for readable(r)
2 is for writeable (w)
1 is for executable (x)

Do

man find

and search for perm to find more of what more you can do.

Ref: http://www.cygwin.com/ml/cygwin/2003-05/msg00502.html
Here is a better reference: Searching for files by permission

Windows: Making my laptop *nix friendly

Posted in Uncategorized by imsaar on November 29th, 2006

I usually do most of my development on my Linux machine and only use my Windows XP laptop for using Outlook (mail and calendaring) and browsing (Firefox, of course).

I decided to work from home due to weather conditions and had to use my laptop more and finally took the steps to make my laptop more unix friendly.

I already had my favorite editor Vim installed on the machine and ruby one click install (which did not required the forbidden admin priviliges thank God) made quick experimenting easy while I was commuting to work. Also rubygems installation work pretty smoothly (similar to on a linux/mac osx system) on windows also.

I already had configured my laptop to use have tab completion for windows lesser shell (cmd or Command Prompt) so cd into directories was a breeze.

Two things that I missed the most still were ls (which I often typed and got error on windows shell) and typing vi filename and opening file and I was fed up with using edit to open files all the time which is far from a decent text editor at least by my standards.

Finally I was motivated enough to do something about these. Here is what I did:

Added the path to the directory where vim was installed to my environment variable:

1. Right Click on ‘My Computer’ and click ‘Properties’
2. Click on ‘Advanced’ tab and click ‘Environment Variables’ button, click ‘PATH’ if already exists or create new if it is not already there.
3. Add the absolute path of directory where vim.exe exists at the end (semi-colo {;} separated list).

Create shortcuts for vi and ls

1. Now open command prompt (Start -> Run -> cmd -> OK)
2. cd into the directory where vim is installed (e.g. C:\>cd “Program Files\Vim\vim70″)
3. edit vi.bat
4. type “C:\Progra~1\Vim\vim70\vim.exe %1 %2 %3″ save and exit (Alt - F - X) the file
4a. %1, %2 … are the arguments passed to the bat (shell) script
Since I am lazy I will create the ls bat also here but you can create it in any directory in your path
5. vi ls.bat (now you can use your new shortcut)
6. type “dir %1″ save and exit (:wq) the file

Enjoy!

Ruby: Finding the absolute path of running script

Posted in Uncategorized by imsaar on November 7th, 2006

I needed to use the full/absolute path of the script/program to a library for access control. I had not done something like this before so here is my trail of discovery:

$0

I started with $0, which in ruby is one of those magic variables that contain the name of the program that was executed from the command line.

The problem with $0 is that it does not necessary have the absolute path, it has whatever the user used which could be relative or absolute.

__FILE__

Then I found __FILE__ which was not exactly what I was looking for as it would give you the file name of the current file so when executing library routine it would be the name of the library file not the original executable file that the user ran.

File.expand_path

After some research I found File.expand_path which essentially did the trick if you use it with $0. So File.expand_path $0 would give you the absolute path of the calling program.

Pathname.new.realpath.to_s

On a parallel note I found Pathname.new.realpath.to_s also but that require you to require ‘pathname’ and also it resolved a symbolic link to real path which was not desired in my case.

Dir.chdir

One caveat with File.expand_path which an experienced rubyist pointed out that if a script does a Dir.chdir then File.expand_path would not work as it essentially prepends the cwd (current working directory) to $0 but even the fellow rubyist couldn’t think of a better way to do this so File.expand_path was my eventual solution and it works!

*nix shell: stty for terminal line settings

Posted in Uncategorized by imsaar on October 9th, 2006

I don’t think this is a new discovery but just a reminder for myself when I encounter this problem again.

I encountered this problem when I was using irb (interactive ruby shell) and for some odd reason when I typed backspace it was printing ^H and worse when I hit enter irb crashes. When I returned to my shell backspace seems to be working fine.

This is not exclusive problem with irb although the crashing part is but the solution I think would work in cases where shell is demonstrating this wierd behavior.

I did a quick *nix command:

stty

and displayed that I have

erase ^?

In order to fix this problem here is what I did:

stty erase ^H

Note:^H are not the two characters but typed Ctrl-V Ctrl-H

Ruby: ri Command Line Help

Posted in Uncategorized by imsaar on September 28th, 2006

Looking up help on ruby methods and classes from the command line sounds simple but can be very useful if you don’t know or don’t remember.

ri is the equivalent of ‘man’ command in unix (I always confuse this with rdoc which is for generating html documentation, I think perldoc has something to do with this confusion).

Some example commands are

prompt> ri Classprompt> ri alias_method

In case multiple classes have the same method and you are searching for a method

prompt> ri method_missing

ri will show a message like this:

More than one method matched your request. You can refineyour search by asking for information on one of:

   Delegator#method_missing, Kernel#method_missing

You can then look for the specific method using:

prompt> ri Kernel#method_missing

Update: I just discovered that Class#method is for instance methods, if you want to look help class methods then it is done using Class::method.

Example:

ri Hash::new        # shows details of the Hash class's new methodri Hash#delete      # shows details of the hash instance's delete method

Also found this interesting reference:
Ruby RI - Using Ruby’s RI Documentation Reader

Tools: Saving Putty Configuration

Posted in Uncategorized by imsaar on July 11th, 2006

Putty is a decent terminal emulation program for MS Windows operatings system:

It can be downloaded from:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

At the time of this writing the latest release version was 0.58.

Putty does not need to be installed and does not create seperate directory to store its configuration, all its configuration is stored in the windows registry.

Start -> Run -> regedit (OK/Enter)

HKEY_CURRENT_USER -> Software -> SimonTatham -> Putty

You can save the configuration by right clicking on SimonTatham and selecting Export from the context menu.

To restore setting, transport the file to the new machine and double click on it after downloading Putty.

Ruby: Searching for a variable string using grep

Posted in Uncategorized by imsaar on April 29th, 2006

I was looking for a way to search for a variable string inside a ruby method and I think I have found _a_ solution.

def print_line_containing(file, str)  File.open(file).grep(/#{str}/).each do |line| puts "#{line}" endend

def print_line_containing(file, str1, str2)  File.open(file).grep(/#{str1}\s+#{str2}/).each do |line| puts "#{line}" endend