Thursday, February 14, 2013

Linux terminal prompt (PS1)

I wanted to change a little bit on appearance of my terminal prompt. Don't need username and host to see but the path should be clean and clear. Also if there is a big path I find it hard to make commands. So need the path upside and the command just below that. Again some times I go root user, so lets make root user as red and normal user as green. Get my favourite PS1:

For normal user add the following line at the end of /home/<user>/.bashrc
# My custom prompt for normal user
PS1="\[$(tput setaf 2)\]\n[\w]\n# \[$(tput sgr0)\]"

Example output:
[~/path/to/dir]

For root user add the following line at the end of /root/.bashrc
# My custom prompt for root user
PS1="\[$(tput setaf 1)\]\n[\w]\n# \[$(tput sgr0)\]"

Example output:
[/etc/path/to/dir]

How is that :) ?


Tuesday, February 12, 2013

“Vi” Editor

vi filename
vi editor is a default editor of all Unix systems. It has several modes. Make sure that your terminal has correct settings, vt100 emulation works good if you are logged in using pc.
  • i for insert mode.
    • I inserts text at the curson
    • A appends text at the end of the line.
    • a appends text after cursor.
    • O open a new line of text above the curson.
    • o open a new line of text below the curson.
  • : for command mode.
    • <escape> to invoke command mode from insert mode.
    • :!sh to run unix commands.
    • x to delete a single character.
    • dd to delete an entire line
    • ndd to delete n number of lines.
    • d$ to delete from cursor to end of line.
    • yy to copy a line to buffer.
    • P to paste text from buffer.
    • nyy copy n number of lines to buffer.
    • :%s/stringA/stringb /g to replace stringA with stringB in whole file.
    • G to go to last line in file.
    • 1G to go to the first line in file.
    • w to move forward to next word.
    • b to move backwards to next word.
    • $ to move to the end of line.
    • J join a line with the one below it.
  • /string to search string in file.
  • n to search for next occurence of string.
Once you are done typing then to be in command mode where you can write/search/ you need to hit  :w filename to write, and in case you are done writing and want to exit :w! will write and exit.

Vim 7: Turning auto completion on

Omni completion isn't automatically turned on for Vim 7, and it's super useful if you code in HTML, CSS, Javascript, XML, Python etc.

In your vimrc you can add following to turn completion on:

autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete
autocmd FileType c set omnifunc=ccomplete#Complete

You complete things with CTRL-X O.


VIM Quick Command Tutorial #2

DESCRIPTION: Marks are useful when you are working in various places of a file or working in various files. You may need to jump to that place quite frequently. At this situation marks just do great.
1. List all marks
 :marks - Shows a list of marks

2. Marked file places, and global file places
 m - start marking
  a-z - for places within file
  A-Z - for places within global

3. Go to specified mark
 g' - start mark selection
  a-z - for places within file
  A-Z - for places within global
 i.e. g'C will go to C marked file place
      g'c will go to c marked place within the file

4. Deleting marks
 :delmarks a b C V - deletes the provided marks i.e. a b C and V
DESCRIPTION: Now about some buffers. These are files loaded in your current session of VIM.
5. List all buffers
 :buffers - to see all buffers list in current session

6. Working with buffer(s)
 :b 1 - b stands for buffer and 1 stands for the buffer number.
  This will load the buffer number 1 to your VIM window
  Alternatively you can give the name of the file and
  press <tab> to crawl all the relative names.

7. Deleting buffer(s)
 :bdelete! N1 N2 ...
 :bdelete! {bufname}
  N1 N2 stands for buffer number to delete
  alternatively you can give the buffer name and press
   <tab> to select the exact file you want to unload from
   buffer list

VIM Quick Command Tutorial #1

There are 3 basic modes in VIM.
a) <Esc> - Normal
b) i - Insert
c) v - Visual
1. Opening Vim
 vim            - open
 vim [filename] - open a file

2. Quit, and save
 :q! - quit without saving
 :q - quit
 :w - write
 :wq or :x - write and quit

3. Cursor moving
  k       up
 h l  left right
  j      down

 ^ - content beginning of the line
 $ - end of line
 w - move by a word
 b - move backward by a ward
 gg - go to the head of the file
 <shift>+g - go to the end of the file
 :number - to line number of 

 { - previous paragraph
 } - next paragraph

4. Editing feature
 i - insert before the cursor
 a - append after the cursor
 o - new line below
 O - new line above

5. Copy(Yank), paste, and delete
 yy - copy the whole line
 yw - copy the word
 y^ - copy to the beginning of line from cursor position
 y$ - copy to the end of the line from cursor position

 p - paste
 np - where n represents number of times to paste

 dd - delete the current line
 dw - delete the current word
 d^ - delete to the beginning of the line from cursor position
 d$ - delete to the end of the line from cursor position

6. Visual mode
 v - select within line
 V - select lines

7. Formating
 < - shift back  > - shift forward
 == auto indent

8. Repeating
 . - repeats the previous command

Format JSON string in VIM

I need to format a JSON string in a better human readable way. How do I do it? It's easy, use the following Vim command:

:%!python -m json.tool

That all :).


Monday, February 11, 2013

Disable Ubuntu One, MySql, Apache services on boot – Ubuntu 12.04

Ubuntu One is great, but when I am on battery power it just drains a lot. So what I need is, disable Ubuntu One service at startup. I can start it any time I like. But the problem is Ubuntu One is a hidden service. So let's unlock all the startup hidden services.

Open your terminal and fire up the following command:
$ sudo sed -i "s/NoDisplay=true/NoDisplay=false/g" /etc/xdg/autostart/*.desktop

Now open your "Startup Applications" GUI and you will be able to see a lot of things including Ubuntu One. Uncheck to make it disabled at startup. Enjoy :)

Use the follwing command to start or stop or to get more help later on for Ubuntu One:
$ u1sdtool --start
$ u1sdtool --quit
$ u1sdtool --help

Two other services I need on my system, but don’t want them to start unless I require them. They are running MySql and Apache.

To stop mysql from starting on boot:

sudo vim /etc/init/mysql.conf
# Comment out the following line
start on runlevel [2345]

To stop apache2 from starting on boot:
sudo update-rc.d -f apache2 remove

To enable apache2 on startup
sudo update-rc.d -f apache2 defaults