Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Thursday, June 6, 2013

How to use VIM editor

VIM Editor Commands
Vim is an editor to create or edit a text file. There are two modes in vim. One is the command mode and another is the insert mode.
  1. In the command mode, user can move around the file, delete text, etc.
  2. In the insert mode, user can insert text.
Changing mode from one to another
From command mode to insert mode type a/A/i/I/o/O (see details below)
From insert mode to command mode type Esc (escape key)

Some useful commands for VIM
Text Entry Commands (Used to start text entry)
  • a     Append text following current cursor position
  • A    Append text to the end of current line
  • i      Insert text before the current cursor position
  • I     Insert text at the beginning of the cursor line
  • o    Open up a new line following the current line and add text there
  • O   Open up a new line in front of the current line and add text there
The following commands are used only in the commands mode.
Cursor Movement Commands
  • h                   Moves the cursor one character to the left
  • l                    Moves the cursor one character to the right
  • k                   Moves the cursor up one line
  • j                    Moves the cursor down one line
  • nG or :n       Cursor goes to the specified (n) line (ex. 10G goes to line 10)
  • ^F (CTRl F) Forward screenful
  • ^B                Backward screenful
  • ^f                 One page forward
  • ^b                One page backward
  • ^U               Up half screenful
  • ^D               Down half screenful
  • $                  Move cursor to the end of current line
  • 0 (zero)        Move cursor to the beginning of current line
  • w                 Forward one word
  • b                  Backward one word
Exit Commands
  • :wq  Write file to disk and quit the editor
  • :q!   Quit (no warning)
  • :q     Quit (a warning is printed if a modified file has not been saved)
  • ZZ   Save workspace and quit the editor (same as :wq)
Text Deletion Commands
  • x       Delete character
  • dw    Delete word from cursor on
  • db     Delete word backward
  • dd     Delete line
  • d$     Delete to end of line
  • d^     (d caret, not CTRL d) Delete to beginning of line
  • Yank (has most of the options of delete)-- VI's copy commmand
  • yy      yank current line
  • y$      yank to end of current line from cursor
  • yw     yank from cursor to end of current word
  • 5yy    yank, for example, 5 lines
  • Paste (used after delete or yank to recover lines.)
  • p       paste below cursor
  • P       paste above cursor
  • "2p   paste from buffer 2 (there are 9)
  • u       Undo last change
  • U      Restore line
  • J       Join next line down to the end of the current line
File Manipulation Commands
  • :w    Write workspace to original file
  • :w    file Write workspace to named file
  • :e     file Start editing a new file
  • :r     file Read contents of a file to the workspace
  • To create a page break, while in the insert mode, press the CTRL key and l. ^L will appear in your text and will cause the printer to start A new page.
Other Useful Commands
Most commands can be repeated n times by typing a number, n, before the command. For example 10dd means delete 10 lines.
  • .      Repeat last command
  • cw   Change current word to a new word
  • r      Replace one character at the cursor position
  • R     Begin overstrike or replace mode – use ESC key to exit
  • :/      pattern Search forward for the pattern
  • :?     pattern Search backward for the pattern

Examples:
Opening a New File
  1. type vim filename (create a file named filename)
  2. type i ( switch to insert mode)
  3. enter text (enter your Ada program)
  4. hit Esc key (switch back to command mode)
  5. type :wq (write file and exit vim)
Editing the Existing File
  1. type vim filename (edit the existing file named filename)
  2. move around the file using h/j/k/l key or any appropriate command
  3. edit required text (replace or delete or insert)
  4. hit Esc key (exit from insert mode if you insert or replace text)
  5. type :wq

Wednesday, June 5, 2013

How to exit from an unix manual page

Though the answer is simple, I have struggled a bit to get out of the man page. Thought this could help for unix beginners like me.

"man" command is very useful to read the manual of any commands that you use from terminal on a linux/mac machines.

From your terminal "man command-name" takes you to the manual page.

In order to navigate in the pan page use "up" or "down" arrow keys for navigation. In certain cases you may have to use enter key.

To get out of the man page simply type "q", this will take you to the command prompt from open terminal.

Ex: man ssh, man chmod, man ls

Wednesday, May 22, 2013

Collection of very interesting unix commands

How to get your top 100 unix commands

history | sed "s/^[0-9 ]*//" | sed "s/ *| */\n/g" | awk '{print $1}' | sort | uniq -c | sort -rn | head -n 100 > commands.txt

How to copy a file to all subfolders of a directory using unix command

find . -type d -exec cp pathtofile {}/ \;

How to delete all .svn folders from current directory using unix command

find . -type d -name '.svn' -print -exec rm -rf {} \;

How to list all network connections (including which app they belong to)

lsof -i -nP

How to run the last command as root

sudo !!

How to execute the previous command until it is successful

until !!; do :; done

How to display summary of git commit ids and messages for a given branch

git log --pretty='format:%Cgreen%H %Cred%ai %Creset- %s'

How to list all file extensions present in the current directory

ls | perl -lne '++$x{lc $1} if /[.](.+)$/ }{ print for keys %x'

How to get your top 10 commands with number of times you have used it

cat .bash_history | cut -f 1 -d\ | sort | uniq -c | sort -r | head

To generate list of usernames from an svn logs (Run from top level of the svn project)

svn log | grep -E "r[0-9]+ \| .+ \|" | awk -F"|" '{print $2}' | sort | uniq > ~/authors.txt

How to get process details from its pid

ps -p $pid

How to open firefox profile manager

/Applications/Firefox.app/Contents/MacOS/firefox-bin -p

Credits:
https://docs.google.com/forms/d/1XNMoSdfYFe_WkPfU--M88oL00PDLIOAo1HxjhZvZYJ4/viewform
Mr. Robg
My previous post
http://stackoverflow.com/questions/1102986/most-powerful-examples-of-unix-commands-or-scripts-every-programmer-should-know
http://www.commandlinefu.com/commands/browse/sort-by-votes

Monday, May 20, 2013

Few must know unix commands

After spending more than 9 years of my life in microsoft os,  I feel great now to use my Mac notebook.  Especially I have fallen in love with the terminal and thrilled to learn the power of unix.  I curse myself for not being an unix user so far. Well, its never late to start. :-)

As a beginner I thought of sharing my learnings to my fellow unix beginners with few basic commands that you "must" know as an unix user.

Few basic "must" know unix commands

1. ls - list files and folders

What is ls?  A command to list all files and folders in a directory.
Usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

Explaining few commonly used parameters of lsls command args

2. pwd - present working directory

What is pwd?  A command to display the present working directory path.
Usage: pwd

3. chmod - file permissions changer

What is chmod?  A command to display the present working directory path.
Usage:  chmod arguments file
Permissions to a file can be of the following three categories,
  1. Read
  2. Write
  3. Execute
Let me try explaining this using numeric permissions,

Read

400 - read permission to owner
040 - read permission to group
004 - read permission to anybody

Write

200 - write permission to owner
020 - write permission to group
002 - write permission to anybody

Execute

100 - execute permission to owner
010 - execute permission to group
001 - execute permission to anybody

All permissions are in 3 digits,
xyz - indicates owner
xyz - indicates group
xyz - indicates anybody

For example:
chmod 777 filenmae means read-write-execute (full) permissions for anybody to the file.
chmod 644 filename means read and write permission to owner and read access to rest of the users to the file specified.
chmod 755 filename means read-write-execute permissions to owner and read and write access to others for the specified file.

The best way to learn any unix command is to do a "man" of that command.  For ex: man ls