Sunday, 26 February 2012

Working in Terminal



There are several keyboard shortcuts in Linux. Learning them can make your life a lot easier! This tuXfile discusses mainly command line shortcuts but some X Window System shortcuts are also included.


Virtual terminal

Ctrl + Alt + F1
Switch to the first virtual terminal. In Linux, you can have several virtual terminals at the same time. The default is 6.
Ctrl + Alt + Fn
Switch to the nth virtual terminal. Because the number of virtual terminals is 6 by default, n = 1...6.
tty
Typing the tty command tells you what virtual terminal you're currently working in.
Ctrl + Alt + F7
Switch to the GUI. If you have the X Window System running, it runs in the seventh virtual terminal by default in most Linux distros. If X isn't running, this terminal is empty.

Command line - input

Home or Ctrl + a
Move the cursor to the beginning of the current line.
End or Ctrl + e
Move the cursor to the end of the current line.
Alt + b
Move the cursor to the beginning of the current or previous word. Note that while this works in virtual terminals, it may not work in all graphical terminal emulators, because many graphical applications already use this as a menu shortcut by default.
Alt + f
Move the cursor to the end of the next word. Again, like with all shortcuts that use Alt as the modifier, this may not work in all graphical terminal emulators.
Tab
Autocomplete commands and file names. Type the first letter(s) of a command, directory or file name, press Tab and the rest is completed automatically! If there are more commands starting with the same letters, the shell completes as much as it can and beeps. If you then press Tab again, it shows you all the alternatives.
This shortcut is really helpful and saves a lot of typing! It even works at the lilo prompt and in some X applications.
Ctrl + u
Erase the current line.
Ctrl + k
Delete the line from the position of the cursor to the end of the line.
Ctrl + w
Delete the word before the cursor.


Command line - output

Shift + PageUp
Scroll terminal output up.
Shift + PageDown
Scroll terminal output down.
clear
The clear command clears all previously executed commands and their output from the current terminal.
Ctrl + l
Does exactly the same as typing the clear command.
reset
If you mess up your terminal, use the reset command. For example, if you try to cat a binary file, the terminal starts showing weird characters. Note that you may not be able to see the command when you're typing it.


Command line - history

history
When you type the history command, you'll see a list of the commands you executed previously.
ArrowUp or Ctrl + p
Scroll up in the history and edit the previously executed commands. To execute them, press Enter like you normally do.
ArrowDown or Ctrl + n
Scroll down in the history and edit the next commands.
Ctrl + r
Find the last command that contained the letters you're typing. For example, if you want to find out the last action you did to a file called "file42.txt", you'll press Ctrl + r and start typing the file name. Or, if you want to find out the last parameters you gave to the "cp" command, you'll press Ctrl + r and type in "cp".



Command line - misc

Ctrl + c
Kill the current process.
Ctrl + z
Send the current process to background. This is useful if you have a program running, and you need the terminal for awhile but don't want to exit the program completely. Then just send it to background with Ctrl+z, do whatever you want, and type the command fg to get the process back.
Ctrl + d
Log out from the current terminal. If you use this in a terminal emulator under X, this usually shuts down the terminal emulator after logging you out.
Ctrl + Alt + Del
Reboot the system. You can change this behavior by editing /etc/inittab if you want the system to shut down instead of rebooting.

Monday, 20 February 2012

Rescuing Grub

Here is how you  can rescue your grub....


You can use a LiveCD or a USB to boot in rescue mode .... 


In Terminal tpye sudo fdisk -l   .
    It will display all partiton of the disk.

Mount the ubuntu partition drive
       sudo mount /dev/sdXX /mnt  (example 'sudo mount /dev/sda11 /mnt' ,don't miss the spaces.)


Only if you have a separate boot partition:
            sudo mount /dev/sdYY /mnt/boot.
Mount the virtual filesystems:
           
            sudo mount --bind /dev /mnt/dev
            sudo mount --bind /proc /mnt/proc
            sudo mount --bind /sys /mnt/sys


To ensure that only the grub utilities from the LiveCD get executed, mount /usr    
     sudo mount --bind /usr/ /mnt/usr
     sudo chroot /mnt 

If there is no /boot/grub/grub.cfg or it's not correct, create one using
           update-grub
      or  update-grub2
Now reinstall Grub 
         grub-install /dev/sdX  (eg. grub-install  /dev/sdaDo not specify the partition number.  
Verify the install
        sudo grub-install --recheck /dev/sdX
Exit chroot : CTRL-D on keyboard.
Unmount virtual filesystems: 
           sudo umount /mnt/dev
           sudo umount /mnt/proc
           sudo umount /mnt/sys



Unmount the LiveCD's /usr directory:
          sudo umount /mnt/usr

Unmount last device:
           sudo umount /mnt
 Reboot.
         sudo reboot.

Monday, 13 February 2012

VIM a Powerful Text Editor


VIM a Powerful Text Editor


Vim is a Text editor available in Linux as well as windows and mac platforms.

You can configure it by the following way.


Settings for vim which can be stored in .vimrc in your home flolder in Linux.

These settings are taken from Mr. Mason's webpage, so I am really thankfull to Seth for this wonderful document.

" File: _vimrc            
" Version: 1
" Author: Seth Mason
" Created: 19 Nov 2003 10:20:19
" Last-modified: 22 Dec 2011 11:00:34  IST
" All my Vim commands for the taking
" Works on cygwin but not very well on unix machines...still trying to figure
" it out


" Use Vim settings, rather then Vi settings (much better!).
set nocompatible

" Turn on the verboseness to see everything vim is doing.
"set verbose=9

" allow backspacing over everything in insert mode
set backspace=indent,eol,start

" I like 4 spaces for indenting
set shiftwidth=4

" I like 4 stops
set tabstop=4

" Spaces instead of tabs
set expandtab

" Always  set auto indenting on
set autoindent

" select when using the mouse
set selectmode=mouse


" do not keep a backup files
set nobackup
set nowritebackup

if has('gui_running')
    " i like about 80 character width lines
    set textwidth=78
    " Set 52 lines for the display
    set lines=52
    " 2 for the status line.
    set cmdheight=2
    " add columns for the Project plugin
    set columns=110
    " enable use of mouse
    set mouse=a
    " for the TOhtml command
    let html_use_css=1
endif

" keep 50 lines of command line history
set history=50 

" show the cursor position all the time
set ruler      

" show (partial) commands
set showcmd    

" do incremental searches (annoying but handy);
set incsearch

" Show  tab characters. Visual Whitespace.
set list
set listchars=tab:>.

" Set ignorecase on
set ignorecase

" smart search (override 'ic' when pattern has uppers)
set scs

" Set 'g' substitute flag on
" set gdefault

" Set status line
set statusline=[%02n]\ %f\ %(\[%M%R%H]%)%=\ %4l,%02c%2V\ %P%*

" Always display a status line at the bottom of the window
set laststatus=2

" Set vim to use 'short messages'.
" set shortmess=a

" Insert two spaces after a period with every joining of lines.
" I like this as it makes reading texts easier (for me, at least).
set joinspaces

" showmatch: Show the matching bracket for the last ')'?
set showmatch

" allow tilde (~) to act as an operator -- ~w, etc.
set notildeop


" Java specific stuff
let java_highlight_all=1
let java_highlight_debug=1
let java_ignore_javadoc=1
let java_highlight_functions=1
let java_mark_braces_in_parens_as_errors=1

" highlight strings inside C comments
let c_comment_strings=1

" Commands for :Explore
let g:explVertical=1    " open vertical split winow
let g:explSplitRight=1  " Put new window to the right of the explorer
let g:explStartRight=0  " new windows go to right of explorer window


if has("gui")
  " set the gui options to:
  "   g: grey inactive menu items
  "   m: display menu bar
  "   r: display scrollbar on right side of window
  "   b: display scrollbar at bottom of window
  "   t: enable tearoff menus on Win32
  "   T: enable toolbar on Win32
  set go=gmr
  set guifont=Courier
endif

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

" for cygwin
set shell=C:/cygwin/bin/bash
set shellcmdflag=--login\ -c
set shellxquote=\"


" ************************************************************************
" C O M M A N D S
"

"switch to directory of current file
command! CD cd %:p:h

" ************************************************************************
" K E Y   M A P P I N G S
"
map <Leader>e :Explore<cr>
map <Leader>s :Sexplore<cr>

" pressing < or > will let you indent/unident selected lines
vnoremap < <gv
vnoremap > >gv

" Don't use Ex mode, use Q for formatting
map Q gq

" Make p in Visual mode replace the selected text with the "" register.
vnoremap p <Esc>:let current_reg = @"<CR>gvs<C-R>=current_reg<CR><Esc>

" Make tab in v mode work like I think it should (keep highlighting):
vmap <tab> >gv
vmap <s-tab> <gv

" map ,L mz1G/Last modified:/e<Cr>CYDATETIME<Esc>`z
map ,L    :let @z=TimeStamp()<Cr>"zpa
map ,datetime :let @z=strftime("%d %b %Y %X")<Cr>"zpa
map ,date :let @z=strftime("%d %b %Y")<Cr>"zpa

" Map <c-s> to write current buffer.
map <c-s> :w<cr>
imap <c-s> <c-o><c-s>
imap <c-s> <esc><c-s>

" Buffer naviation
map <M-Left> :bprevious<CR>
map <M-Right> :bnext<CR>


" Select all.
"map <c-a> ggVG

" Undo in insert mode.
imap <c-z> <c-o>u

" Load my color scheme
"colorscheme slack

" ************************************************************************
" B E G I N  A U T O C O M M A N D S
"
if has("autocmd")

  " Enable file type detection.
  " Use the default filetype settings, so that mail gets 'tw' set to 72,
  " 'cindent' is on in C files, etc.
  " Also load indent files, to automatically do language-dependent indenting.
  filetype plugin indent on

  " When editing a file, always jump to the last known cursor position.
  " Don't do it when the position is invalid or when inside an event handler
  " (happens when dropping a file on gvim).
  autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal g`\"" |
    \ endif

  " Normally don't automatically format 'text' as it is typed, only do this
  " with comments, at 79 characters.
  au BufNewFile,BufEnter *.c,*.h,*.java,*.jsp set formatoptions-=t tw=79

  " add an autocommand to update an existing time stamp when writing the file
  " It uses the functions above to replace the time stamp and restores cursor
  " position afterwards (this is from the FAQ)
  autocmd BufWritePre,FileWritePre *   ks|call UpdateTimeStamp()|'s


endif " has("autocmd")

" GUI ONLY type stuff.
if has("gui")
  :menu &MyVim.Current\ File.Convert\ Format.To\ Dos :set fileformat=dos<cr> :w<cr>
  :menu &MyVim.Current\ File.Convert\ Format.To\ Unix :set fileformat=unix<cr> :w<cr>
  :menu &MyVim.Current\ File.Remove\ Trailing\ Spaces\ and\ Tabs :%s/[    ]*$//g<cr>
  :menu &MyVim.Current\ File.Remove\ Ctrl-M :%s/^M//g<cr>
  :menu &MyVim.Current\ File.Remove\ All\ Tabs :retab<cr>
  :menu &MyVim.Current\ File.To\ HTML :runtime! syntax/2html.vim<cr>
  " these don't work for some reason
  ":amenu &MyVim.Insert.Date<Tab>,date <Esc><Esc>:,date<Cr>
  ":amenu &MyVim.Insert.Date\ &Time<Tab>,datetime <Esc><Esc>:let @z=YDATETIME<Cr>"zpa
  :amenu &MyVim.Insert.Last\ &Modified<Tab>,L <Esc><Esc>:let @z=TimeStamp()<CR>"zpa
  :amenu &MyVim.-SEP1- <nul>
  :amenu &MyVim.&Global\ Settings.Toggle\ Display\ Unprintables<Tab>:set\ list!    :set list!<CR>
  :amenu &MyVim.-SEP2- <nul>
  :amenu &MyVim.&Project :Project<CR>

  " hide the mouse when characters are typed
  set mousehide
endif

" ************************************************************************
" A B B R E V I A T I O N S
"
abbr #b /************************************************************************
abbr #e  ************************************************************************/

abbr hosts C:\WINNT\system32\drivers\etc\hosts

" abbreviation to manually enter a timestamp. Just type YTS in insert mode
iab YTS <C-R>=TimeStamp()<CR>

" Date/Time stamps
" %a - Day of the week
" %b - Month
" %d - Day of the month
" %Y - Year
" %H - Hour
" %M - Minute
" %S - Seconds
" %Z - Time Zone
iab YDATETIME <c-r>=strftime(": %a %b %d, %Y %H:%M:%S %Z")<cr>


" ************************************************************************
"  F U N C T I O N S
"

" first add a function that returns a time stamp in the desired format
if !exists("*TimeStamp")
    fun TimeStamp()
        return "Last-modified: " . strftime("%d %b %Y %X")
    endfun
endif

" searches the first ten lines for the timestamp and updates using the
" TimeStamp function
if !exists("*UpdateTimeStamp")
function! UpdateTimeStamp()
   " Do the updation only if the current buffer is modified
   if &modified == 1
       " go to the first line
       exec "1"
      " Search for Last modified:
      let modified_line_no = search("Last-modified:")
      if modified_line_no != 0 && modified_line_no < 10
         " There is a match in first 10 lines
         " Go to the : in modified:
         exe "s/Last-modified: .*/" . TimeStamp()
     endif
 endif
 endfunction
endif

Friday, 14 October 2011

Setting Aliases in Linux ............

Here is list My aliases ..........

First have to source the file called .bash_aliases in .bashrc in your home directory.

i.e. 


if [ -f ${HOME}/.bash_aliases ]; then
        . ${HOME}/.bash_aliases
fi

Contents of my .bash_aliases file are:



# enable color support of ls and also add handy aliases
#
# if [ "$TERM" != "dumb" ]; then
#     eval "`dircolors -b`"
#     alias ls='ls --color=auto'
#     #alias dir='ls --color=auto --format=vertical'
#     #alias vdir='ls --color=auto --format=long'
# fi

# unalias ll la lla l rm brm cp bcp mv bmv

 alias code='export PS1=$\>\>'
 alias matlab='/usr/local/matlab/bin/./matlab'
 export PATH=$PATH:/opt/intel/bin/
 export PATH=$PATH:/opt/intel/inspector_xe/bin32/
 export PATH=$PATH:/opt/intel/vtune_amplifier_xe/bin32/


 alias cro='chromium-browser &'
 alias halfterminal='xfce4-terminal --geometry=68x44'
 alias 1='ls -ltr'
 alias ll='ls -l'
 alias la='ls -A'
 alias lla='ls -la'

 alias lc='ls -p --color=never --group-directories-first'

 alias brm="`which rm`"
 alias bmv="`which mv`"
 alias bcp="`which cp`"

 alias rm='rm -i'
 alias mv='mv -i'
 alias cp='cp -i'

 alias m='more'

 alias q='clear; logout'
 alias x='clear; exit'

 alias h='history'
 alias s='echo $?'

 alias mail='pine -i'

 alias r='R'
 alias rn='R --no-save'
 alias rs='R --save'
 alias Rn='R --no-save'
 alias Rs='R --save'

 alias gv='gv --watch'

 alias gcc='gcc -Wall'
 alias g++='g++ -Wall'
 alias g77='g77 -Wall'

alias 13='ssh test@thirteen'

alias ff='~/fir/./firefox &'
alias o='~/.local/bin/./opera-opera &'
alias 3t='cl ~/Documents/3semister/theory'
alias 3p='cl ~/Documents/3semister/programming'
alias 2p='cl ~/Documents/2semister/programming'
alias 2t='cl ~/Documents/2semister/theory'
alias 1t='cl ~/Documents/1semister/theory'
alias 1p='cl ~/Documents/1semister/programming'
alias nd='nautilus ~/Download &'
alias dc='cl ~/Documents'
alias c='cl'
alias cms='ssh -X mdinesh@cms'
alias 15='ssh mdinesh@fifteen'
alias dw='cl ~/Downloads'
alias dk='cl ~/Desktop'
alias space='cl /d_drive/space/'
alias b='cd ..'
alias cmd='vim ~/.bash_aliases'
alias cmds='source ~/.bashrc'
alias ri='rm -i'
alias tata='exit'
alias lgout='gnome-session-save --kill --silent'
alias l='lc'
alias mt='matlab &'
alias e='exit'
alias gc='google-chrome &'
alias grm='rm .config/google-chrome/SingletonLock'
alias grm1='rm .config/chromium/SingletonLock'
alias timetable='lynx cms.unipune.ernet.in/~coordinator/time-table/time-table.php?p=2010-11-Y2'
alias ali='vim .bash_aliases'
alias mountedinfo='df -hT'
# functions
#
 cl()
{
if [ -d $1 ]
then
cd $1
lc
else
cd $HOME
lc
fi
}

dellock()
{
rm -rf /nfs/home/mtech10/mdinesh/.mozilla/firefox/1fhbqf6x.default/lock
rm -rf /nfs/home/mtech10/mdinesh/.mozilla/firefox/1fhbqf6x.default/.parentlock
}
#source /opt/openfoam201/etc/bashrc

function prompt {
  local BLUE="\[\033[0;34m\]"
  local GREEN="\[\033[1;32m\]"
  local DARK_BLUE="\[\033[1;34m\]"
  local RED="\[\033[0;31m\]"
  local DARK_RED="\[\033[1;31m\]"
  local NO_COLOR="\[\033[0m\]"
  case $TERM in
    xterm*|rxvt*)
      TITLEBAR='\[\033]0;\u@\h:\w\007\]'
      ;;
    *)
      TITLEBAR=""
      ;;
  esac
  PS1="%\u@\h[\t]> "
  PS1="${TITLEBAR}\
$GREEN\h$DARK_RED|$GREEN\W$DARK_RED->$NO_COLOR "
  PS2='continue-> '
  PS4='$0.$LINENO+ '
}

##################################################
netinfo ()
{
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
echo ""
/sbin/ifconfig | awk /'Bcast/ {print $3}'
cho ""
/sbin/ifconfig | awk /'inet addr/ {print $4}'

# /sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
echo "---------------------------------------------------"
}

spin ()
{
echo -ne "${RED}-"
echo -ne "${WHITE}\b|"
echo -ne "${BLUE}\bx"
sleep .02
echo -ne "${RED}\b+${NC}"
}

copy ()
{
scp -r "$@" mdinesh@192.168.38.254:
}

ccode()
{
if [ -e "$@" ];
then
echo "File already exists using VIM to open it"
vim "$@"
else
cat ~/.ctemplate > "$@";
vim "$@";
fi
}

## Moving around & all that jazz
alias back='cd $OLDPWD'
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ......="cd ../../../../.."

music()
{
ssh test@192.168.38.13 "cat /var/www/music/"$@"/*.mp3" | mplayer -
}


mktar() { tar cvf  "${1%%/}.tar"     "${1%%/}/"; }
mktgz() { tar cvzf "${1%%/}.tar.gz"  "${1%%/}/"; }
mktbz() { tar cvjf "${1%%/}.tar.bz2" "${1%%/}/"; }


extract () {
  if [ -f $1 ] ; then
      case $1 in
          *.tar.bz2)   tar xvjf $1    ;;
          *.tar.gz)    tar xvzf $1    ;;
          *.bz2)       bunzip2 $1     ;;
          *.rar)       rar x $1       ;;
          *.gz)        gunzip $1      ;;
          *.tar)       tar xvf $1     ;;
          *.tbz2)      tar xvjf $1    ;;
          *.tgz)       tar xvzf $1    ;;
          *.zip)       unzip $1       ;;
          *.Z)         uncompress $1  ;;
          *.7z)        7z x $1        ;;
          *)           echo "don't know how to extract '$1'..." ;;
      esac
  else
      echo "'$1' is not a valid file!"
  fi
}
#define Colors
CK='\e[0;30m'
BLUEF='\e[0;34m'
BLUEP='\e[1;34m'
GREENF='\e[0;32m'
GREENP='\e[1;32m'
CYANF='\e[0;36m'
CYANP='\e[1;36m'
REDF='\e[0;31m'
REDP='\e[1;31m'
PURPLEF='\e[0;35m'
PURPLEP='\e[1;35m'
BROWNF='\e[0;33m'
BROWNP='\e[1;33m'
GRAYF='\e[0;37m'
GRAYP='\e[1;37m'
YELLOW='\e[1;33m'


After Saving this file source the .bashrc file in the shell.....