We Live By Code

18 July 2012

Effective emacs - Part:1

1 Effective emacs - Part:1

emacs is very powerful and effective operating system. Ah wait did I said "operating system", Yes emacs is such a powerful editor that it runs itself as operating system. Starting from web to mail to irc to terminal to music player to coding to blogging everything you can achieve with emacs. Here I a talking about shell or terminal. There are four terminal emulator programs in emacs; shell, term, ansi-term and emacs shell (eshell).

I have worked with all of them and evaluated and I found that eshell(emacs shell) is one of the best among other. I wouldn't mind to say that its the best shell in Unix/Linux world.

1.1 Why I have chosen eshell

  1. Its entirely written in lisp so I can eval and run.
  2. you can use all emacs function as command prompt.

for example generate html from org or starting [[http://www.gnus.org] gnus] from command prompt

1:  10:05 AM| gnmk@:/home/gnmk $ gnus

just type "gnus", it will open buffer with gnus.

  1. you can use all Linux/Unix command which are available in your box.

Please note that eshell itself has some inbuilt command like ls,cp etc.

1.2 Code snippet (from emacswiki and other website)

add below to your .emacs

1.2.1 setting up environment related variable

1:  (require 'eshell)
2:  ;; setting up environment related variable in eshell
3:  (setenv "JAVA_HOME" "/usr/local/java")
4:  (setenv "CATALINA_HOME" "/var/lib/tomcat4")
5:  (setenv "LC_ALL" "C")
6:  (setenv "PATH" "/usr/local/java/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/home/gnmk/bin")
7:  (setenv "LANG" "en_US.UTF8")
8:  (setenv "EDITOR" "emacsclient")

1.2.2 eshell history size and saving history while exit.

1:  (setq eshell-history-size 20000)
2:  (setq eshell-save-history-on-exit t)
3:  (setq eshell-hist-ignoredups t)
4:  (setq eshell-cmpl-cycle-completions nil)

1.2.3 scroll to the bottom

1:  (setq eshell-scroll-to-bottom-on-output t)
2:  (setq eshell-scroll-show-maximum-output t)

1.2.4 run top/ps from eshell, require term to be open

 1:  (eval-after-load 'esh-opt
 2:    '(progn
 3:       (require 'em-cmpl)
 4:       (require 'em-prompt)
 5:       (require 'em-term)
 6:       (add-hook 'eshell-mode-hook 
 7:                 '(lambda () (define-key eshell-mode-map "\C-a" 'eshell-bol)))
 8:       (add-to-list 'eshell-visual-commands "ssh")
 9:       (add-to-list 'eshell-visual-commands "tail")
10:       (add-to-list 'eshell-visual-commands "top")
11:       (add-to-list 'eshell-command-completions-alist
12:                    '("gunzip" "gz\\'"))
13:       (add-to-list 'eshell-command-completions-alist
14:                    '("tar" "\\(\\.tar|\\.tgz\\|\\.tar\\.gz\\)\\'"))
15:       (add-to-list 'eshell-output-filter-functions 'eshell-handle-ansi-color)))

1.2.5 eshell/clear function

1:  ;;from http://www.khngai.com/emacs/eshell.php
2:  (defun eshell/clear ()
3:  "04Dec2001 - sailor, to clear the eshell buffer."
4:  (interactive)
5:  (let ((inhibit-read-only t))
6:  (erase-buffer)))

1.2.6 eshell short and long prompt on request (from emacswiki)

 1:  ;; Returns the short propmpt string for eshell
 2:  (defun eshell-prompt-function-short ()
 3:    "Makes a short eshell prompt to avoid moving out of the buffer
 4:      window boundary"
 5:    (let* ((pwd (eshell/pwd))
 6:           (pwdlst (split-string pwd "/"))
 7:           (rpwdlst (reverse pwdlst))
 8:           (base (car rpwdlst)))
 9:      (concat (if (string= base "")
10:                  "/"
11:                (if (cdr pwdlst) "<...> /" ""))
12:              base
13:              (if (= (user-uid) 0) " # " " $ "))))
15:  ;; Returns the long prompt string for eshell
16:  (defun eshell-prompt-function-long ()
17:    "Makes a long standard eshell prompt"
18:    (concat (format-time-string "%l:%M %p") "| "
19:            (getenv "USER") "@" (getenv "HOSTNAME") ":"
20:            (eshell/pwd) (if (= (user-uid) 0) " # " " $ ")))
22:  (setq eshell-prompt-function
23:        'eshell-prompt-function-long)
25:  (defun eshell/sprompt ()
26:    "Makes a short eshell prompt to avoid moving out of the buffer
27:        window boundary (link to eshell-prompt-function-short)"
28:    (setq eshell-prompt-function
29:          'eshell-prompt-function-short))
31:  (defun eshell/lprompt ()
32:    "Makes a long standard eshell prompt (link to
33:        eshell-prompt-function-long)"
34:    (setq eshell-prompt-function
35:          'eshell-prompt-function-long))

1.2.7 find file when use emacs as one of the command

1:  (defun eshell/emacs (file)
2:  "find-file alias"
3:    (find-file file))

1.2.8 find file when use w3m

1:  (defun eshell/w3m (file)
2:  "w3m-find-file alias"
3:    (w3m-find-file file))

1.2.9 info command

1:  (defun eshell/info ()
2:    "info alias"
3:    (info))

1.2.10 display emacs manual when type einfo

1:  (defun eshell/einfo ()
2:    "Display emacs manual"
3:    (info-emacs-manual))

1.2.11 aliase

1:  (defun eshell/ll (&rest args)
2:    "ls -alh alias"
3:    (eshell/ls "-alh" args))
5:  (defun eshell/la (&rest args)
6:    "ls -a alias"
7:    (eshell/ls "-a" args))

1.2.12 convert ansi color faces in eshell

 1:  ;; This will transform ansi color to faces in Emacs shell!
 2:  (ansi-color-for-comint-mode-on)
 3:  (defun eshell-handle-ansi-color ()
 4:    (ansi-color-apply-on-region eshell-last-output-start
 5:                                eshell-last-output-end))
 6:  (add-hook 'eshell-mode-hook
 7:            '(lambda ()
 8:               (add-to-list
 9:                'eshell-output-filter-functions
10:                'eshell-handle-ansi-color)))

1.2.13 compile process in background

 1:  ;;Here’s how to compile in the background, also by Kai.
 2:  (defun eshell/ec (&rest args)
 3:    "Use `compile' to do background makes."
 4:    (if (eshell-interactive-output-p)
 5:        (let ((compilation-process-setup-function
 6:               (list 'lambda nil
 7:                     (list 'setq 'process-environment
 8:                           (list 'quote (eshell-copy-environment))))))
 9:          (compile (eshell-flatten-and-stringify args))
10:          (pop-to-buffer compilation-last-buffer))
11:      (throw 'eshell-replace-command
12:             (let ((l (eshell-stringify-list (eshell-flatten-list args))))
13:               (eshell-parse-command (car l) (cdr l))))))
14:  (put 'eshell/ec 'eshell-no-numeric-conversions t)

Type ec <command> from eshll prompt will open another buffer with command output/status.

Date: 2012-07-18 10:50:47 India Standard Time

Author: Deepak(gnumonk)

Org version 7.8.11 with Emacs version 24

Validate XHTML 1.0

02 December 2011

Working on remote files using ssh and sudo in emacs/tramp

Working with emacs is always fun.
To work on remote files you will find enough information at Tramp tutorial, here i am talking about working with remote server and then use sudo to write some file.

For example : You are connecting remote machine using ssh as user1 and then you need to write file using user2 (actually sudo -u user2 filename) or better to say multiple hoping.

| user1@remotehost ---> sudo -u user2 at remotehost filename

Cool then, you need to add following line in your .emacs file.

set-default 'tramp-default-proxies-alist (quote ((".*" nil "/ssh:user1@%h:"))))
after saving and evaluating use

C-x C-f sudo:user2@remotehost:filename_path

Happy Hacking

15 November 2011

Installing Emacs 24 from source in FreeBSD

I have installed emacs 24 from bzr in FreeBSD. Here it is goes.
  1. Install bzr from portage.
    • %cd /usr/ports/devel/bzrtools && sudo make install clean
  2. Install depended software from default emacs portage.
    • %cd /usr/ports/editor/emacs && sudo make
  3. Create emacs checkout director for bzr checkout
    • %mkdir ~/gnmk_emacs
  4. Pull the updated branch from emacs source
    • %cd ~/gnmk_emacs
    • %bzr branch http://bzr.savannah.gnu.org/r/emacs/trunk emacs_trunk
  5. Configure
    • %cd ~/gnmk_emacs/emacs_trunk/
    • %./Configure --prefix=~/gnmk_emacs/bsd_emacs/
  6. Make bootstrap to generate all lisp
    • %gmake bootstrap
  7. Finally Install
    • %gmake install
  8. Run 
    • %~/gnmk_emacs/bsd_emacs/bin/emacs

13 September 2011

Hardware beep on/off in FreeBSD

To enable or disable hardware beep in FreeBSD operating systems use MIB hw.syscons.bell. Type the following command to disable for current session:
$sudo sysctl hw.syscons.bell=0
To keep the setting permanently use following in your sysctl.conf file
#echo "hw.syscons.bell=0" >> /etc/sysctl.conf

04 July 2011

FreeBSD 8.2

After reading this http://www.over-yonder.net/~fullermd/rants/bsd4linux/01 , I am planning to give a chance to FreeBSD.

Let see how it goes and how long i will stay with this.

26 May 2011

Encrypted partition in Ubuntu/Debian/Linux

Setup and encrypted partition in Linux(Debian/Ubuntu and variant) is quite easy.

Please follow the  below steps.

I have physical partition sda1 available.

Step 1: Install cryptsetup
| sudo apt-get install cryptsetup

Step 2: Set up encryption on the partition
| sudo cryptsetup luksFormat /dev/sda1
| ========
| This will overwrite data on /dev/sda1 irrevocably.
| Are you sure?
| (Type uppercase yes): YES
| Enter LUKS passphrase:
| Verify passphrase:
| Command successful

Step 3: Open the mount the device
| sudo cryptsetup luksOpen /dev/sda1 crypt_disk
| Enter LUKS passphrase:
| Command successful.

Step 4: Create the file system( I am creating ext4)
| mkfs.ext4 /dev/mapper/crypt_disk

Step 5: Add entry to /etc/crypttab
| crypt_disk /dev/sda1 luks

Step 6: Create mount point for automate the process in future
 and add entry to /etc/fstab
| mkdir /mnt/crypt
| /dev/mapper/crypt_disk /mnt/crypt ext4 defaults 0 2

12 February 2011

Blogging from Emacs.

Working with emacs is always fun, starting from programming to mail to web to irc etc. I have learned new thing that you can update the google blog from emacs using e-blog library.
For setting up you need to have e-blog, curl and ofcource emacs. Here is the setup
1) Download e-blog from http://code.google.com/p/e-blog/
2) Untar it to any directory. For example .emacs.d
open emacs to edit .emacs file and add the path
(load "~/.emacs.d/e-blog/e-blog.el")
Load the file or restart emacs. Use Alt+x and type e-blog-new-post to start posting. Have fun, Happy emacs, Happy Hacking :)