Showing posts with label comp. Show all posts
Showing posts with label comp. Show all posts

2009-01-29

Vim - basics - copy-paste

Useful shortcuts for Vim beginners: 

mark text 
v ... start highlighting text 
+y ... end highlighting text 

d ... cut 
P ... P-paste above cursor (capital P)
p ... paste below 

u ... undo 
Ctrl+R ... redo 
source: source web 
other: other web 
Vim macro for copy-paste with Ctrl+C and Ctrl+W: abclinuxu.cz (Czech version)

VI shourtcuts:

Go to
Shift g    = end of file
gg    = start beginning of file
GG    = start of file
:22    = go to line 22
0 = start of line
^ = first letter on line
$ = end of line

Delete 
x = delete
X = backspace
dw = delete word
d$ = delete to EOL $
d0 = delete to start 0

Copy to buffer
:t.    = duplicate line
:t7 = copy line after line 7

yy = copy current line
ayy = copy current line to buffer a
a5yy = copy 5 lines into buffer a

Paste from buffer
y7yp = copy and paste 7 lines
p = paste line
ap = paste buffer a at the cursor


Search
n = search next
N = search previous
* = go to next occurrence of word under cursor
# = go to previous occurence

Less shortcuts

NoHup
# Nohup process id pid :
nohup my_command > my.log 2>&1 &
echo $! > save_pid.txt

2009-01-12

No sound in Flash under Linux

Problem description:
Using Debian or Ubuntu Linux, I experienced problem with no sound
using Flash player in Firefox.

Solution:
# apt-get install alsa-oss

Details:
alsa-oss package creates interface between Alsa sound libraries and OSS libraries. Latest systems usually support Alsa drivers while Flash requires OSS. This package interconnects both of them to play together.

2008-11-11

Faster scp with tar

scp using tar:

Following sequence retrieves data from remote server immediately without need to store compressed tar to file.

ssh root@server tar -c /www/domain.com | tar -x

Following sequence sends local data via ssh to the remote server.

tar -c /www/domain.com | \
ssh root@server cd /www/domain.com '&&' tar -x


Bash - functions, timestamp, date diff

Bash function - command line calculator
calcfn () { echo "$*" | bc -l; }

Bash timestamp of specific date
date +%s -d 20070103

Date difference in Bash
dateOfPast='20080103'
today=`date +%Y%m%d`
timeStampToday=`date +%s -d $today`
timeStampOfPast=`date +%s -d $dateOfPast`
secondsInDay=86400
dayDiff=`echo \($timeStampToday - $timeStampOfPast\) / $secondsInDay | bc`
echo $dayDiff

Convert Unix timestamp to local date time
echo $timeStamp | perl -n -e 'chop; print localtime( ($_)[0] ) . "\n";'

Convert Unix timestamp to GMT
echo 1234567890 | perl -n -e 'chop; print gmtime( ($_)[0] ) . "\n";'
Unix Timestamp
1234567890 ... Friday The 13th.

2008-11-10

Vim - File Differences with vimdiff

Brief usage of vimdiff. For detailed description see man, info and online docs.

Start vim in diff mode
vimdiff file1.php file1.php~

Alternatives:
gvimdiff
vimdiff -O ==horizontal windows
vim -d

Switch between windows
Ctrl+w Ctrl+w

Put new lines to other window
dp

Obtain new lines (from another window)
do

Skip between diff groups
previous group
[c

next group
]c

(Un)folding
open folded difference
zo

close folded diff
zc

Source: vimdoc

2008-11-06

Czech Weather Forecast - Pocasi.cz

Our team was hard working last weeks. And here is the result:
Pocasi.cz - Czech and European Weather Forecast (Czech language)

2008-10-12

Automated build with Autotools

Automake, Autoconf, Libtool - simply Autotools...

Quick way to start autotools

Programmer's command sequence:

autoscan
autoreconf --install --force
./configure
make clean


install
builds the configuration scripts by calling the sequence of particular autotools commands. The advantage is that we do not have to remember the whole sequence
and we do it in one simple command.

force forces the autoreconf to rebuild the configuration even if it already exists.

make clean cleans up build environment

The autoreconf command supplies following sequence
libtoolize --force
aclocal
automake --foreign --add-missing
autoconf


User's command sequence:

./configure --prefix=/usr/local
make
make check # not neccessary, just a test
DESTDIR=/tmp/package make install

Resources:
AutoBook book for Auto* Libtool, Gettext, ... (latest)
AutoTools for beginners - quick intro
Wikipedia: GNU build system - short description

2008-09-27

Processing mail with procmail

Sometimes I change my procmail rules and I want to force procmail to process my inbox again with these new rules. The inbox is usually in mbox format, therefore I have to split it to particular emails.

In this case I use formail which splits inbox to particular messages and calls specified program to serve each message independently.

Process allready received mail with procmail rules again:
formail -s procmail < $MAIL

2008-09-10

AJAX Exception: Permission denied

While experimenting with AJAX via iFrame, I received following exception:

Error: Permission denied to get property Window.document

It was caught while accessing iFrame's body from the main page by the following way:
document.getElementById(frameId).contentWindow.document.body;

The problem was that I loaded some foreign site into my iFrame and then I triend to access it's body object.

Firefox prevents Javascript from accessing objects (like window's document) of sites different from the original document site. The solution is to place both the main page and content of the iFrame to the same server site.

2008-08-21

Python Unicode exception

While using Python I encounter one common exception:

Traceback (most recent call last):

File "./unicode-test.py", line 564, in ?
main()
File "./unicode-test.py", line 553, in main
print "Value: %s" % unicodeString
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 87: ordinal not in range(128)

The reason is that we want to print Unicode object but it cannot be converted to appropriate characters automaticaly. Solution is easy, we have to convert the object before printing it:

print ("Value: %s" % unicodeString).encode('utf-8')

optionaly:
# visible char
print ("Value: %s" % unicodeString).encode('ascii', 'replace')
# zero char
print ("Value: %s" % unicodeString).encode('ascii', 'ignore')
# exception (default)
print ("Value: %s" % unicodeString).encode('ascii', 'strict')

Additional reading: source

Note: We have to convert the whole object after joining ascii with unicode. Otherwise Python would join the ascii part and utf8 part and convert the result to Unicode and the error appears again.

You can use unicodedata to normalize string:
import unicodedata
unicodedata.normalize('NFKD', 'p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88'.decode('utf-8')).encode('utf-8')

Source: Python doc

2008-08-15

Mixed notes

Python lacks good XML support in standard distribution. I found a list of Python XML libraries comparison:
http://www.somebits.com/weblog/tech/python/xpath.html

It is possible to convert DOM to dictionary records with following functions:
http://code.activestate.com/recipes/116539/

or use module xml2dict:
http://code.google.com/p/xml2dict/

Sometimes I do ssh to some development machine which I am not quite familiar with. I usualy want to detect Linux distribution and version. The following description is very helpful:
http://www.novell.com/coolsolutions/feature/11251.html

Czech trafic navigator:
http://www.dopravniinfo.cz/default.aspx

Python date and time manipulation:
http://seehuhn.de/pages/pdate

2008-08-14

Dancing in Samba rhythm

Sometimes I want to use Samba as fast as possible. Unfortunately there may occur unexpected problems during user authentication. Following sequence of commands solves many of them:

smbpasswd user_name
pdbedit -a user_name

In other words, adding user and setting up password may not have been enough, we need to add him/her to user database.

Mount:
sudo mount -t cifs //netbiosname/sharename /media/sharename

Options:
-o \
username=winusername, \
password=winpassword, \
iocharset=utf8, \
file_mode=0777, \
dir_mode=0777 \

Or:
guest, \
rw

2008-08-11

Debian X Fonts

How to install nice X fonts in Debian quick and easily?

In the root console exec command:
apt-get install msttcorefonts

If the following error occur:
E: Couldn't find package msttcorefonts

you should add "contrib" section to your /etc/apt/sources.list:
deb http://ftp.us.debian.org/debian etch main contrib non-free

and then execute - as usually:
apt-get update

and do again:
apt-get install msttcorefonts

Then select these fonts as default.

If you use KDE open Control Center then select:
Appearance & Themes then Fonts. In the right dialog select default fonts for each font type.

Appropriate font names are: Times, Arial, Courier, Courier New, Courier 10 Pitch, Lucida Typewriter

Done. Note that font settings take effect after restart of each application. You also have to select fonts for specific applications such as Firefox, Thunderbird, Kwrite, etc.

2008-07-28

Playing Oracle and MySQL game

Sometimes we want to escape characters in Oracle SQL Plus console

Set the escape character
set escape \

Send command (in the same session)
INSERT INTO customer (url) VALUES('http://www.example.com/redir.php?id=1234\&log=true');

Select rows recursevely

SELECT LPAD(' ', 2 * (LEVEL - 1)) || TO_CHAR(child) s
FROM test_table
START WITH parent IS NULL
CONNECT BY PRIOR child = parent;

This has just one restriction: no joins allowed.
source: http://www.adp-gmbh.ch/ora/sql/connect_by.html

Insert-update with MySQL

INSERT INTO table_name (col_names)
VALUES (values)
ON DUPLICATE KEY UPDATE col=val, col2=val2;
source: http://devblog.billism.com/?p=12

2008-07-18

Starting with MySQL

All the beginning are terrible and slow... again and again looking into the manual for the same commands.

Install
apt-get install mysql-client mysql-server mysql-common php5-mysql

Set root password
mysqladmin password 'new_root_password'

Login to DB
mysql -u root -p

Create database
CREATE DATABASE db_name;

Create regular user and grant appropriate rights
SELECT PASSWORD('secret_password');
CREATE USER user_name IDENTIFIED BY 'secret_password';
GRANT ALL ON db_name.* TO 'user_name'@'host_name' IDENTIFIED BY 'secret_password';
^D

Login under the regular user
mysql -u user_name -p db_name

Create table
DROP TABLE IF EXISTS users;
CREATE TABLE users (
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
created_date DATETIME,
name VARCHAR(200),
phone VARCHAR(200)
);

Create index
CREATE INDEX index_users_name ON users (name);
details

Firefox: Open new window in new tab

How to force open new window to new tab in Firefox 2.0?

Open location
about:config

Set Filter
browser.link.open

Variables:
browser.link.open_external
Rule for opening links from external programs (e.g. Thunderbird, Psi, ...)
browser.link.open_newwindow
Rule for opening links targeted to new window by the 'target' attribute
browser.link.open_newwindow.restriction
Rule for opening restrictions by Javascript

Values:
The value sets opening links in:
1 current tab
2 new window
3 new tab in current window

2008-07-10

Xlib: connection refused by server

Trying run under root:
su
swscanner
I received following error message:
Xlib: connection to ":0.0" refused by server

Easy solution, run under regular user:
xhost +localhost
OR:
xhost local:root

Then run under root:
export XAUTHORITY=/home/regular_user_name/.Xauthority

Where
regular_user_name is name of the user doing command su.


2008-07-07

Acer TravelMate 5720 - video drivers

How To install video drivers to Acer TravelMate 5720 (thanks to Miro):
Proprietary binary drivers for Debian:
apt-get install fglrx-driver fglrx-driver-dev fglrx-control fglrx-kernel-src
cd /usr/src/
tar -xjvf fglrx.tar.bz2
cd modules/
./make.sh
mkdir -p /lib/modules/`uname -r`/misc
cp fglrx.ko /lib/modules/`uname -r`/misc/
depmod -ae

Oracle DB server notes

Oracle Error:
Warning: ocifetch(): OCIFetch: ORA-24374: define not done before fetch or execute and fetch in /home/martin/dev/www/inc/ora_funct.php on line 15

Reason and solution:

sometimes wrong syntax but mostly missing access rights to accessed table. You can solve this problem by granting sufficient rights to the user which is accessing the table.

Oracle sql date comparison:
SELECT column
FROM table
WHERE start_date <= TO_DATE('20080401', 'yyyy-mm-dd');