Saturday, August 7, 2010

Code highlighting on Blogger

SkyHi @ Saturday, August 07, 2010




Vim Tips

SkyHi @ Saturday, August 07, 2010
Tab operations

Vim tips: Using tabs
1.Open several files in tabs

vim -p [filename] [filename] ...

2.Open a new tab in vim

:tabnew [filename]
:tabe [filename]

3.Moving among different tabs

:tabn //previous tab
:tabp //next tab
:tabfirst //first tab
:tabfir //first tab
:tablast //last tab
gt //next tab
Ctrl+Page Dn/Up //works when you have only one "terminal tab" open
//It works too in gvim

4.Reposition tab

:tabm //move this tab to the last position
:tabm n //move this tab to the n-th position

File Explorer or File Manager

1. Vim built-in file explorer

2. VimExplorer (highly recommended!)
:VE //open VimExplorer
//to the parent in the file tree
//open the file using its associated default application
e //open selected file in a new tab
;c //start a shell from current path.
;e //start another file manager(nautilus,konquer)
dd //delete file under cursor
+f //create a new file.
+d //create a new directory.
m{a-z} //put current path to register(a-z).
'{a-z} //jump to path in register(a-z).
J //view path in register.

3. The NERD tree (highly recommended!)
:NERDTree //open the NERD tree
o.......Open selected file, or expand selected dir
go......Open selected file, but leave cursor in the NERDTree
t.......Open selected node in a new tab
T.......Same as 't' but keep the focus on the current tab
...Open selected file in a split window
g..Same as , but leave the cursor on the NERDTree
!.......Execute the current file
O.......Recursively open the selected directory
x.......Close the current nodes parent
X.......Recursively close all children of the current node
e.......Open a netrw for the current dir

4. vtreexplorer

Latex Editing

A (very) quick introduction to Latex-Suite
1. Debian has the package "vim-latexsuite", and the package installs files in system directory
/usr/share/vim/addons. I choose not to install the package but to download vim-latex and
put it to my ~/.vim folder.

2. Compile latex

3. View generated dvi file

4. Select dvi file viewer: add the following to .vimrc
let g:Tex_ViewRule_dvi = 'xdvi'

Python IDE

1. Python with a modular IDE (Vim)

Java IDE

1. Vim JDE
2. Vim JDE Manual

C/C++ Coding IDE

1. c.vim : C/C++-IDE

2. vim and linux CodingStyle
set noexpandtab                " use tabse, not spaces
set tabstop=8 " tabstops of 8
set shiftwidth=8 " indents of 8
set textwidth=78 " screen in 80 columns wide, wrap at 78
set autoindent smartindent " turn on auto/smart indenting
set smarttab " make and smarter
set backspace=eol,start,indent
" allow backspacing over indent, eol, & start

3.Fast Code Block Formatting
format the whole file

format code block: visual select, then "=="

4. Exuberant Ctags
ctag *.c *.h  // generate tag file
ctags -R // recursively
:set tags=/path/tags
ctrl + ] // jump into the tag
ctrl + t // jump back

5. Cscope
The Vim/Cscope tutorial
Using Cscope on large projects
Vim documentation: if_cscop
cscope -Rbq
:cs add /path/cscope.out

cs find c|d|e|f|g|i|s|t name
0 or s
1 or g find definition
2 or d find callee
3 or c find caller
4 or t find string
6 or e egrep mode
7 or f find this file
8 or i find files that contain this file

6. Vim Taglist plugin (highly recommended!)
//show the complete info for the item under cursor
//move to code

By default, the tag list will be sorted by the order in which the tags
appear in the file. You can sort the tags either by name or by order by
pressing the "s" key in the tag explorer window.

7. CRefVim: a C-reference manual especially designed for Vim
  cr normal mode:  get help for word under cursor
Memory aid cr: (c)-(r)eference
cr visual mode: get help for visually selected text
Memory aid cr: (c)-(r)eference
cw: prompt for word CRefVim should search for
Memory aid cw: (c)-reference (w)hat
cc: jump to table of contents of the C-reference manual
Memory aid cc: (c)-reference (c)ontents

8. OmniCppComplete: C/C++ omni-completion with ctags database

9. SuperTab continued: Do all your insert-mode completion with Tab.

10. Use sessions to save state of work
vim -S session.vim'
:mks[ession][!] '

11. minibufexpl.vim: Elegant buffer explorer - takes very little screen space
bf //go to the first buffer
bp //go to the previous buffer
bn //go to the next buffer
b[num] //go to the num-th buffer
bd[num] //remove the num-th buffer

12. a.vim: Alternate Files quickly (.c --> .h etc)
:A switches to the header file corresponding to
the current file being edited (or vise versa)
:AS splits and switches
:AV vertical splits and switches
:AT new tab and switches
:AN cycles through matches

13. Automatic folding (add the following in .vimrc)
set foldmethod=syntax

command in vim
zr //unfold one level
zR //unfold all levels
zm //fold one level
zM //fold all levels

14. QuickFix Window
:cw //open
:cn //next item
:cp //previous item

15. Visual Mark: Visual mark, similar to UltraEdit's bookmark

(1) For gvim, use "Ctrl + F2" to toggle a visual mark.
For both vim and gvim, use "mm" to toggle a visual mark.
(2) Use "F2" to navigate through the visual marks forward in the file.
(3) Use "Shift + F2" to navigate backwards.

16. Organize/Navigate projects of files

17. Manage Sessions in Vim

Debugging in Vim

1. Clewn and vimGdb (highly recommended!)
I prefer using vimGdb although it requires you to patch vim source
and compile. In order to use the key mappings provided by vimGdb, in your .vimrc
file, add
source /home/username/.vim/macros/gdb_mappings.vim

or in vim session
run /home/username/.vim/macros/gdb_mappings.vim

List of key mappings in 'gdb_mappings.vim':
 launch the interactive gdb input-line window
CTRL-Z send an interrupt to GDB and the program it is running
B info breakpoints
L info locals
A info args
S step
I stepi
CTRL-N next: next source line, skipping all function calls
X nexti
F finish
R run
Q quit
C continue
W where
CTRL-U up: go up one frame
CTRL-D down: go down one frame
CTRL-B set a breakpoint on the line where the cursor is located
CTRL-E clear all breakpoints on the line where the cursor is located
CTRL-P Normal mode: print value of word under cursor
Visual mode: GDB command "createvar" selected expression
CTRL-X print value of data referenced by word under cursor

In gdb window, you need to first "file [name of the executable]" to enable
the debugging.

Other tips

1. Your personal vim setup is in ~/.vimrc and ~/.vim/...
System vim files are in /usr/share/vim

2. Copy & Paste between different sessions
"*yy //copy the current line to the clipboard
//you can use visual mode (ESC,v) to choose several lines

"*p //put whatever is on the clipboard in the document.

3. Select All
ggVG  //select all

4. Convert code to HTML


Tuesday, August 3, 2010


SkyHi @ Tuesday, August 03, 2010

Programmers are often told not to reinvent the wheel, and that the best programmers borrow from others before they start to build something themselves. PHP, being primarily a web language, sees a lot of reinventing in regards to form display, processing and validation. However, there's a great PEAR package that needs a little more exposure: HTML_QuickForm. It makes handling the rendering and display of forms, and more usefully, both client and server-side validation, quick and easy. This article will take you through the basics of that package. It assumes familiarity with HTML form elements, and reasonably basic PHP skills.

Installing HTML_QuickForm

The package has only two requirements: a version of PHP that is at least 4.2, and the HTML_Common package. At the time of writing, HTML_QuickForm 3.2.7 is the newest version of the package, and it requires HTML_Common 1.2.1. There are moves underfoot to rewrite both of these packages for PHP 5 (in the form of HTML_QuickForm2, and HTML_Common2), but as yet neither has been released.

You can check what PEAR packages are already installed by using pear list:

pear list
Installed packages:
Package Version State
Archive_Tar 1.1 stable
Console_Getopt 1.2 stable
DB 1.6.2 stable
Date 1.4.6 stable
HTTP 1.2.2 stable
Image_Canvas 0.3.0 alpha
Image_Color 1.0.2 stable
Image_Graph 0.7.2 alpha
Mail 1.1.3 stable
Net_SMTP 1.2.6 stable
Net_Socket 1.0.1 stable
PEAR 1.3.2 stable
Validate 0.6.3 beta
XML_Parser 1.0.1 stable
XML_RPC 1.1.0 stable

This machine doesn't have either HTML_QuickForm or HTML_Common, so they'll need to be installed.

#pear install HTML_Common
downloading HTML_Common-1.2.3.tgz ...
Starting to download HTML_Common-1.2.3.tgz (4,746 bytes)
.....done: 4,746 bytes
install ok: HTML_Common 1.2.3

#pear install HTML_QuickForm
downloading HTML_QuickForm-3.2.7.tgz ...
Starting to download HTML_QuickForm-3.2.7.tgz (102,475 bytes)
........................done: 102,475 bytes
install ok: HTML_QuickForm 3.2.7