LAMP

My tryst with Web Development

Friday, January 28, 2005

vimrc

Save the text as a file by name .vimrc and place it in your home directory. It would help you with the general PHP editing tasks

set nocompatible
"For Windows Users
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim

set guifont=Courier_New
set nowrap
set nobackup
set expandtab
set shiftwidth=4
set tabstop=4
set softtabstop=4
filetype on
set guioptions+=b
set linebreak
set autoindent
set fdm=marker
set sm
"Change the path to the ctags utility
"For Linux, do "whereis ctags"
let Tlist_Ctags_Cmd="C:\Program Files\Vim\ctags\ctags.exe"

set lines=30
set columns=120
set hidden
set autowriteall
"For Windows Users
set autochdir
behave mswin

set diffexpr=MyDiff()
function MyDiff()
let opt = ''
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
silent execute '!C:\VIM\VIM62\diff -a ' . opt . '"' . v:fname_in . '" "' . v:fname_new . '" > "' . v:fname_out . '"'
endfunction

Thursday, January 20, 2005

Vim Shortcuts

Based on a list compiled by Charles E Campbell, Jr.,12/99. Updated by Alan E Klietz 1/2005.

Important/frequently used commands are boxed.

-b binary mode

-n no swap file; use memory only

-r recover aborted session via swap file

-R readonly mode

-V[N] verbose mode (to troubleshoot .vim scripts)

: enter Command Mode

Esc return to Normal Mode

. repeat the last command

% jump to matching parenthesis, brace, or bracket

'a jump to line with mark a. See m.

( jump to previous sentence

) jump to next sentence

0 goto beginning of line

$ goto end of line

+ goto first character of next line

, reverse direction of last f, F, t, or T

- goto first character of previous line

/ search forward for text, / to repeat

? search backward text, ? to repeat

* search for word under cursor N times

# same as *, but search backwards

& synonym for ":s//~/" (repeat last substitution)

; repeat last f, F, t, or T

={motion} filter {motion} lines through 'equalprg' external program

@ execute command in register

[( goto previous unmatched '('

[[ previous section

[] go backwards count sections or to previous '}' in 1st column

[{ goto previous unmatched '{'

[# goto previous unmatched "#if" or "#else"

[* goto previous start of a C comment (/*)

[i display 1st line that contains keyword under cursor

[I display all lines that contain keyword under cursor

[^I jump to 1st line that contains keyword under cursor

[d display 1st macro definition that contains macro under cursor

[D display all macro definition that contain macro under cursor

[^D jump to 1st macro definition that contains keyword under cursor

][ go forwards count sections to to next '}' in 1st column

]] next section

]} goto next unmatched '}'

]# goto next unmatched "#if" or "#else"

]* goto next end of a C comment (*/)

]i like '[i', but start at current cursor position

]I like '[I', but start at current cursor position

^] jump to file having tag under cursor. See ^t

]^I like '[^I', but start at current cursor position

]d like '[d', but start at current cursor position

]D like '[D', but start at current cursor position

^ first non-whitespace character

_ move (count-1) lines downward on first non-blank character

` goto mark

{ previous paragraph

| to screen column [count] in current line

} next paragraph

~ switch case of current character

'' go to start of line of previous mark or location before search

`` return to previous mark or location before a search

"return key" next line

"spacebar key" next character

!!{filter} filter [count] lines through the external program {filter}

!{cmd}{filter} send {motion} text through external program {filter}

!!scramble toggle encryption of C/C++ string (AEK)

<’a shift left up to mark

<% shift left until matching (, [, or {

<< shift line one shiftwidth to the left

>’a shift right up to mark

>% shift right until matching (, [, or {

>> shift line one shiftwidth to the right

a append after the current location

A append at the end of the line

^a add [count] to the number at or after the cursor

b beginning of previous word

B beginning of previous word, ignore punctuation

^b scroll back one screen; see ^f

c delete {motion} text (into register) and begin insert

C change to end of line

^c ends insert mode, unused in command mode

d{motion} delete text covered by a {motion}

d’a delete up to mark

Ndd delete N lines

D delete to end of line

^d scroll down half a window (N^d sets new default); see ^u

e end of word

E end of word, ignore punctuation

^e scroll screen down one line

fx find given character forwards

F find given character backwards

^f scroll forward one screen; see ^b

g^g show information about current cursor position

g^h start Select block mode

g^] :tjump to tag under the cursor

g# like "#", but without using "\<" and "\>"

g$ wrap off: goto rightmost onscreen character of current line

g* like "*", but without using "\<" and "\>"

g0 wrap off: goto leftmost onscreen character of current line

g? rot13 encoding operator

g?? rot13 encode current line

g?g? rot13 encode current line

gD goto definition of word under cursor in current file

gE go backwards to end of previous WORD

gH start Select line mode

gI like "I", but always start in column 1

gJ join lines without inserting space (like :j!)

["x]gP put text (from register x) N times

gR enter virtual replace mode

gU{motion} make Nmove text uppercase

gV don't reselect previous Visual area (maps, menus) in Select mode

g] :tselect on tag under cursor

g^ wrap off: goto leftmost non-white onscreen char on current line

ga print ascii value of character under cursor

gd goto definition of word under cursor in current function

ge go backwards to end of previous word

gf start editing file whose name is under cursor

gg cursor to line N (default: 1) ; start Select mode

gh start Select mode

gj wrap on: like "j", but go N screen lines down

gk wrap on: like "k", but go N screen lines up

gm goto character at middle of screenline

go cursor to byte N in buffer

["x]gp put text (from register x) after cursor N times

gq{motion} format text

gr{char} virtual replace N chars with given char

gs goto sleep for N (1) seconds

gu{motion} make Nmove text lowercase

gv reselect previous Visual area

g~{motion} swap case for Nmove text

g{Down} same as gj

g{End} same as g$

g{Home} same as g0

g{LeftMouse} same as g{LeftMouse}

g{RightMouse} same as g{RightMouse}

g{Up} same as gk

G goto line (100G); defaults to EOF

^g show status line

h left

H goto first line on screen

^h backspace in insert mode, left in command mode

i insert before current location (until Esc)

I insert before first non-whitespace character on line

^I goto [count] cursor position in jump list

j down

J join next line with current line

^j down in command, create newline in insert mode

Monday, January 10, 2005

PHP Development

I started my corporate career as a PHP Developer Trainee. My first day in the office. First thing first, I started with the selection of a good PHP editor. I googled for PHP editors and found some good editors, but each editor I tried missed on something, either heavy on system's resources or low on features or providing too much unwanted functions. Finally I settled down with VIM, for it was low on resources, and high on customization and quick editing shortcuts (most needed in PHP like scripting).

VIM proved good enough as my editor of choice, though I had just started using it in place of Emacs. It helped me with a lot of things, like:

  • It offers a lot of quick shortcuts that are great for editing any PHP-script. Here is a list of the most commonly used VIM shortcuts
  • Automatic Code Indenting, file saving, parenthesis matching, navigating the different PHP scripts while debugging.
  • Viewing the PHP manual within VIM is a great help when you are looking for the PHP-functions their parameters, return types or even the correct name of the function. Just type "K" on the function name and it displays the documentation for the chosen PHP function.
  • Here is my .vimrc

After developing some initial test PHP pages, I started my work on the live PHP websites. I learned about the smarty PHP templates and other details about the LAMP (Linux, Apache, MySQL and PHP) Platform. 1 week passed by and I started looking for ways of writing more efficient PHP scripts and reusing the code for performing common repeated tasks.

I learned following PHP tips:

  • For those PHP websites that are database-driven (that most of them are), never connect to the mysql database on any individual PHP script. Rather create a separate script to connect to mysql and include thatscript in all your other PHP scripts.
  • There is a lot of content repeated on a website like following the website layout on all the pages of the website. For all such repeated content,create separate PHP pages and include successively for every page.
  • There are some repeated tasks as well. For example, displaying a list of records from a mysql table in an HTML table. For such things, I need not say that you should write functions in a separate script andinclude that script in a common file including all other includes.
  • Try to reuse every line of code, that you have written. The idea is to minimize the time spent in doing repeat stuff. Moreover, it allows for single point of control over the content and reflect the change overthe complete website.
  • Try using smarty PHP templates for all your PHP scripts, for they allow you to separate your code from your design. Even if you want to change the design later, you need not worry about the code. Moreover, smarty compiles each template page, so don't worry much about the performanceissues.
  • Keep writing the documentation alongwith the code. It might appear as an additional task initially but proves to be a great help later when youread the code, believe me.