Tuesday, April 19, 2011
copy text from Vim in PuTTY by highlighting text
Method 1:
Holding shift while highlighting.
Method 2:
In ~/.vimrc
" Set mouse equals to nothing stopping vim from interpreting the mouse clicks.
set mouse=
Method 3:
Using Putty ... .to copy whole contains to your clipboard then ... what you do is ...
1. Clear the screen ..
2. Clear the scrollback by right click on the top bar and select the option "clear scrollback".
3. From the same option you will find "change settings" --> select this option and againg select "window" option where specify the huge no. say 99999 in no. line for scrollback where it is 200 by default.
4. Now run the file by using the cat <filename>
5. Right click on the top bar and select the "copy all to clipboard"
6. paste it in the notepad.
Method 4:
you get file in the other way also.
1. You can select the "logging " -> and select the radio button "Log all session output" in Session logging.
2. In the log file name .. specify the log file location by clicking the browse button.
3. Now run the file by cat <filename>.
4. After the file contains end. you can see the the log file which you specified in the log file.
Reference:
https://bbs.archlinux.org/viewtopic.php?id=52862
http://www.unix.com/unix-dummies-questions-answers/31386-copy-text-file-vi-editor-windows-clipboard-2.html
Sunday, April 17, 2011
running wireshark as root on Mac OSX
In Python on February 22, 2010 by Tzury Bar Yochay Tagged: mac, wireshark
After installing the .dmg packge you should run
$ sudo /Applications/Wireshark.app/Contents/MacOS/Wireshark
In order to capture traffic on the interfaces
Reference:
http://evalinux.wordpress.com/2010/02/22/running-wireshark-as-root-on-mac-osx/
Monday, April 11, 2011
相信我,有一天你會遇到一個讓你願意付出一切的女人,你會不自覺再度流露出純真的笑容
這是在一個討論區看到有人的回文:「以前我沒錢 也被拋棄過 現在我有點錢了 卻也不想再交女友 (雖然前鎮子有脫團過一個月 就分了) 因為 我會很敏感的猜測 他是不是只>想要我的錢說實在的 想起來還真有點可悲 有時候我在想 我寧願回到以前有穩定工作但不是很有錢的時候 至少我會覺得比較開心 笑的時
候 是發自內心的笑 那純真好像已經回不來了」
然後,底下有另外一個人的回覆,讓我印象深刻:「我不認識你,但是看你這樣,也會覺得很難過. 相信我,有一天你會遇到一個讓你願意付出一切的女人,你會不自覺再度流露出純真的笑容.」
這幾天,獨自走在路上的時候,常常會想起這句話 ↑↑
Thursday, April 7, 2011
Theming the Drupal 6 User Login Form
=====================
There are a great deal of articles out there on theming the Drupal User Login Form, but most don't give you granular control over each individual form element. Rather, you are restricted to mainly editing the css. Here is a way to have complete granular control over each element within the user login form...
1. Place this function either in a module or in your template.php:
function get_user_login_form() {
$form_id = 'user_login';
$form = array();
$form['name'] = array(
'#type' => 'textfield',
'#maxlength' => USERNAME_MAX_LENGTH,
'#required' => TRUE,
'#attributes' => array('tabindex' => '1'),
);
$form['pass'] = array(
'#type' => 'password',
'#required' => TRUE,
'#attributes' => array('tabindex' => '2'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Log in'),
'#weight' => 2,
'#attributes' => array('tabindex' => '3')
);
$form['#validate'] = user_login_default_validators();
$form['#build_id'] = sprintf('form-%s', md5(uniqid(mt_rand(), TRUE)));
$form_state = array(
'storage' => NULL,
'submitted' => FALSE,
);
$form['#post'] = $_POST;
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form, $form_state);
$out = new stdClass;
$out->form_start =
sprintf("<form method='post' accept-charset='UTF-8' action='%s'>",
url('user/login'));
$out->form_end = "</form>";
$out->name = drupal_render($form['name']);
$out->pass = drupal_render($form['pass']);
$out->submit =
drupal_render($form['submit']) .
drupal_render($form['form_id']) .
drupal_render($form['form_build_id']) .
drupal_render($form['form_token']);
return $out;
}
2. In your .tpl.php file, output the fields
<?php
$login_form = get_user_login_form();
?>
<?php print $login_form->form_start; ?>
Username: <?php print $login_form->name; ?><br />
Password: <?php print $login_form->pass; ?><br />
<?php print $login_form->submit; ?>
<?php print $login_form->form_end; ?>
Reference:
http://blog.aphexcreations.net/2009/04/theming-drupal-user-login-form.html
Sunday, March 27, 2011
programmatically create a node with file upload attachment
After much sweat and frustration, I've finally figured out how to tweak my tried
and true Drupal import script to work for Drupal 6.
I often find myself creating Drupal sites for groups with either existing web
sites (in other content management systems) or with file libraries that they
want to use to import into their new site.
Drupal's bootstrap function and node api makes it really easy to create a
script that can be run from the command line to handle the import.
Here are the key components for making it work with Drupal 6.
The import script has to be located in the web directory. Since I don't want
people to accidentally or on purpose run it from a web session, I include the
followig line:
// prevent this from running under apache:
if (array_key_exists('REQUEST_METHOD', $_SERVER)) {
echo 'nope. not executing except from the command line.';
exit(1);
}
Unfortunately, Drupal's boostrap function will complain if it doesn't detect
the HTTP_HOST variable, so I add it here. It doesn't matter what that variable
is:
// set HTTP_HOST or drupal will refuse to bootstrap
$_SERVER['HTTP_HOST'] = 'example.org';
Next comes the boostrap function will brings in all the Drupal libraries:
include_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Although not stricly necessary, some modules require a $user variable, so it
makes sense to create one. Setting the user id to 1 will guarantee that you'll
have access to do what you need to do. Depending on what you're doing, you
might want to use a user with less privileges to prevent any bug from
destroying all your data.
global $user;
$user = user_load(1);
Here is the basic node creation:
$node = new stdClass();
$node->type = 'story';
$node->status = 1;
$node->uid = 1;
$node->title = 'My Title';
$node->body = 'My body;
$node->created = time();
$node->changed = $node->created;
$node->promote = 1;
$node->sticky = 0;
$node->format = 1;
$node->language = 'en';
Here's an example of a CCK field:
$node->field_date = array(
0 => array(
'value' => '2009-02-09T00:00:00',
),
);
And at last, here's the elusive file attachment code:
$file = '/path/to/your/file.odt';
// Get the file size
$details = stat($file);
$filesize = $details['size'];
// Get the path to your Drupal site's files directory
$dest = file_directory_path();
// Copy the file to the Drupal files directory
if(!file_copy($file,,$dest)) {
echo "Failed to move file: $file.\n";
return;
} else {
// file_move might change the name of the file
$name = basename($file);
}
// Build the file object
$file_obj = new stdClass();
$file_obj->filename = $name;
$file_obj->filepath = $file;
$file_obj->filemime = file_get_mimetype($name);
$file_obj->filesize = $filesize;
$file_obj->filesource = $name;
// You can change this to the UID you want
$file_obj->uid = 1;
$file_obj->status = FILE_STATUS_TEMPORARY;
$file_obj->timestamp = time();
$file_obj->list = 1;
$file_obj->new = true;
// Save file to files table
drupal_write_record('files', $file_obj);
// change file status to permanent
file_set_status($file_obj, 1);
// Attach the file object to your node
$node->files[$file_obj->fid] = $file_obj;
Lastly, save the node:
node_save($node);
upload_save($node); // to upload table.
echo "Savied node: $node->nid\n";
That's it. Below I've copied a real life working version that takes all the
files in a given directory and creates a node for each file in which the node's
title is the title of the file, the date of the file is entered as a CCK date
field, and the body of the node is a text version of the document (if it's pdf,
doc, or wpd).
<?php
/*
* This script is used to manually import files
*
*/
// edit the following two lines
// set the path where the files you want to import exist.
$target = '../import-files-from-mbox/files';
// what user id should the files be imported as?
$uid = 1;
// prevent this from running under apache:
if (array_key_exists('REQUEST_METHOD', $_SERVER)) {
echo 'nope. not executing except from the command line.';
exit(1);
}
// set HTTP_HOST or drupal will refuse to bootstrap
$_SERVER['HTTP_HOST'] = 'example.org';
include_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// create a user that will be the author of the files/nodes
// created
global $user;
$user = user_load($uid);
// iterate over directory
$d = dir($target);
while (false !== ($name = $d->read())) {
if($name != '.' && $name != '..') {
$errors = file_validate_name_length($name);
if(count($errors) > 0) {
echo "Invalid name length, skipping: $name\n";
continue;
}
// set some defaults for the file we will be importing
$file = "$target/$name";
$details = stat($file);
$filesize = $details['size'];
$mtime = $details['mtime'];
$date_value = date('Y-m-d\T00:00:00',$mtime);
// create the node object
$node = new stdClass();
$node->type = 'lib_item';
$node->status = 1;
$node->uid = 1;
$node->title = $name;
$node->body = extract_body($file);
$node->created = time();
$node->changed = $node->created;
$node->promote = 1;
$node->sticky = 0;
$node->format = 1;
$node->language = 'en';
// custom node fields
$node->field_date = array(
0 => array(
'value' => $date_value,
),
);
// handle the file upload
$dest = file_directory_path();
// copy the file to the files directory
if(!file_copy($file,$dest)) {
echo "Failed to move file: $file\n";
continue;
} else {
// file_move might change the name of the file
$name = basename($file);
}
// build file object
$file_obj = new stdClass();
$file_obj->filename = $name;
$file_obj->filepath = $file;
$file_obj->filemime = file_get_mimetype($name);
$file_obj->filesize = $filesize;
$file_obj->filesource = $name;
$file_obj->uid = 1;
$file_obj->status = FILE_STATUS_TEMPORARY;
$file_obj->timestamp = time();
$file_obj->list = 1;
$file_obj->new = true;
// save file to database
drupal_write_record('files', $file_obj);
// change file status to permanent (default is temporary)
file_set_status($file_obj, 1);
$node->files[$file_obj->fid] = $file_obj;
node_save($node);
upload_save($node); // to upload table.
echo "Savied node: $node->nid\n";
exit;
}
}
function extract_body($name) {
$pos = strrpos($name,'.');
$ext = strtolower(substr($name,$pos+1));
$cmd = '';
if($ext == 'doc') {
$cmd = 'antiword';
} elseif($ext == 'pdf') {
$cmd = 'pdftotext';
} elseif($ext == 'wpd') {
$cmd = 'wpd2text';
} else {
return '';
}
exec(escapeshellcmd($cmd) . ' ' . escapeshellarg($name),$ret,$error);
if($error != 0) return '';
return implode("\n",$ret);
}
?>
Reference:
http://workingdirectory.net/posts/2009/attach-file-to-node-drupal-6/
Sunday, March 20, 2011
copy selection to OS X system clipboard in Vim
To copy the output in Mac OSX Terminal to clipboard, you can use the the command pbcopy:
Add this to your ~/.vimrc:
" ctrl-x for cut
vmap <C-x> :!pbcopy<cr>
" ctrl-c for copy
vmap <C-c> :w !pbcopy<cr><cr>
Then, on your desktop application, press command-v to paste.
Note: pbcopy is a system command. For example:
# cat fileName.txt | pbcopy
Method 2:
Download and install MacVim
For MacVim and Windows Gvim, simply add the following to your ~/.vimrc:
set clipboard=unnamed
Now all operations such as yy, D, and P work with the clipboard. No need to prefix them with "* or "+.
Method 3:
If the clipboard is enabled, you can copy a selected region to the clipboard by hitting:
"*y
or
"+y
Note: In X11, Vim's "* is PRIMARY, "+ is CLIPBOARD, and SECONDARY doesn't get a named register. (Not that anybody uses it...).
To see if it is enabled, execute vim --version and look for +clipboard or -clipboard. For example, it's not enabled by default on my 10.6.0 box:
# vim --version | grep clipboard
-clipboard
For MacVim and Windows Gvim, simply add the following to your ~/.vimrc:
set clipboard=unnamed
Now all operations such as yy, D, and P work with the clipboard. No need to prefix them with "* or "+.
Method 4:
If you are using MacPorts you can upgrade your VIM to include clipboard support via:
port install vim +x +x11
Now you use the "+ register to yank your text directly to your Mac clipboard. Works like a charm.
Reference:
http://stackoverflow.com/questions/677986/vim-copy-selection-to-os-x-clipboard
Saturday, March 19, 2011
Disable auto indent when pasting code text into vim
Paste toggle
Put the following in your vimrc (change the <F2> to whatever key you want):
set pastetoggle=<F2>
Note: if F2 key does not work, you might need to press ctrl-v F2 keys instead of just "F2" key.
To paste from another application:
Press (toggles the 'paste' option on).
Use your terminal to paste text from the clipboard. (Shift - Insert key)
Press (toggles the 'paste' option off).
Then the existing indentation of the pasted text will be retained.
If you have a mapping for , that mapping will apply (and the 'pastetoggle' function will not operate).
Some people like the visual feedback shown in the status line by the following alternative for your vimrc:
nnoremap <F2> :set invpaste paste?<CR>
imap <F2> <C-O><F2>
set pastetoggle=<F2>
The first line sets a mapping so that pressing <F2> in normal mode will invert the 'paste' option, and will then show the value of that option. The second line does the same in insert mode (but insert mode mappings only apply when 'paste' is off). The third line allows you to press <F2> when in insert mode, to turn 'paste' off.
http://gala4th.blogspot.com/2009/11/toggle-auto-indenting-for-code-paste.html
Method 2:
When you copy multiple lines text and paste it into the vim , sometimes you might find the text is aligned wrongly as the following:
Line1
Line 2
Line 3
Line 4
To solve this problem, enter the paste mode by
: set paste
Then do all the pasting works, and when finished, type
: set nopaste
Reference:
http://www.linuxask.com/questions/disable-auto-indent-when-pasting-text-into-vim