Thursday, 29 December 2011

QuickBits-12: Bash Shortcuts for Geeks

 

These are some of the shortcuts that i use ...

Command Editing Shortcuts

  • Ctrl + a – go to the start of the command line
  • Ctrl + e – go to the end of the command line
  • Ctrl + k – delete from cursor to the end of the command line
  • Ctrl + u – delete from cursor to the start of the command line
  • Alt + d – delete to end of word starting at cursor (whole word if cursor is at the beginning of word)
  •  Ctrl + w – delete from cursor to start of word (i.e. delete backwards one word)

 

Command Recall Shortcuts

  • Ctrl + r – search the history backwards
  • Ctrl + g – escape from history searching mode
  • Ctrl + p – previous command in history (i.e. walk back through the command history)
  • Ctrl + n – next command in history (i.e. walk forward through the command history)
  • Alt + . – use the last word of the previous command
  • Ctrl + o -  This command is my favourite. If you hit CTRL+o after picking a command from your history, the command is executed, and the next command from history is prepared on the prompt.
  •  

Command Control Shortcuts [Not a Bash Feature]

  • Ctrl + l – clear the screen
  • Ctrl + s – stops the output to the screen (for long running verbose command)
  • Ctrl + q – allow output to the screen (if previously stopped using command above)
  • Ctrl + c – terminate the command
  • Ctrl + z – suspend/stop the command
  •  

Bash Bang (!) Commands

  • !! - run last command
  • !blah – run the most recent command that starts with ‘blah’ (e.g. !ls)
  • !$ – the last word of the previous command (same as Alt + .)
  • !* – all of the previous command except for the first word (e.g. if you type ‘ls foo foo/bar foo/bar/foo‘, then !* would give you ‘foo foo/bar foo/bar/foo‘)
  • ^^ - If you type a command and run it, you can re-run the same command but substitute a piece of text for another piece of text using ^^ (e.g.: if you run 'ls this/is/cool/stuff' and if you do '^cool^geek' then it will run 'ls this/is/geek/stuff'. It replaces only the first occurance)
  • !!:gs/search/replace/ - It is same as previous except it replaces all occurance

 Tricks with ~/.inputrc

  • Put this in your ~/.inputrc
"\e[A": history-search-backward
"\e[B": history-search-forward
"\C-p": history-search-backward
"\C-n": history-search-forward

Type something, then pressing Ctrl-p(or Ctrl-n) will initiate the search in the history with the already typed text as prefix
  •  

Monday, 21 November 2011

QuickBits-11: Giving User Input to cmd through file

Lots of user developed commands requires user input for "if you want to continue ... [y/n]" . If you want to automate this

For example: If mycommand requires user input saying 'y' then add 'y' to the arguments.txt file.


cat arguments.txt - | mycommand


Now your mycommand will not ask for user input. It takes it from the file arguments.txt

Saturday, 19 November 2011

QuickBits-10: Highlight a word in Emacs buffer

If you have used Source Insight for code browsing, you would have used its word hightlight feature.You can highlight the word under cursor, in your file you are browsing using shift+f8.

You can do the same in Emacs, by adding this to your .emacs file


 
(defun is-word-highlighted ()
  (interactive)
  (let ((face (or (get-char-property (point) 'read-face-name)
                  (get-char-property (point) 'face))))
   (if (facep face) (if (face-equal face "hi-yellow") t nil) nil)))

(defun toggle-highlight-word ()
(interactive)
(setq sym (current-word))
(if (is-word-highlighted) (unhighlight-regexp sym) (highlight-regexp sym)) 
)

(global-set-key [S-f8] 'toggle-highlight-word)

Sunday, 11 September 2011

QuickBits-9: Search String in files

1. To search for a string/regular expression in all files in a folder
grep -H "string" * 
This will print the matching line.
-H is for printing the matching filename.

2. To search for a string/regular expression in all files in a folder + subfolders
grep -H "string" * -r

3. To search for a string in all files of certain type
find . -name "pattern" -exec grep -H "string" {} \;


grep -H "string" $(find . -name "pattern")
Example: To find string "main" in all .c file
find . -name "*.c" -exec grep -H "main" {} \;


grep -H "main" $(find . -name "*.c")






Sunday, 4 September 2011

QuickBits-8: Remove Duplicate Lines in a File

To Remove duplicate lines in a file and print it
 cat filename | sort | uniq  

sort will sort the lines and
uniq will remove adjacent duplicate lines.

To count the number of repetition of each line
 cat filename | sort | uniq -c

Sunday, 28 August 2011

QuickBits-7: Convert .a file to .so file

.a files are just archive of .o object files. You need to just extract the files from the archive and package them as a shared object (.so).

ar -x myarchive.a


gcc -shared -Wl,-soname,mysharedlib.so *.o -o mysharedlib.so

Sunday, 21 August 2011

QuickBits-6: Search and Kill Process

Many a times we need to find a process and kill it. The best way to Find a process is

Search
 pgrep -l [pattern] 

-l will list the name of the process or else only pid will be displayed. pgrep searches for the match in any part of the name including the arguments to the command.

Example:
 pgrep -l work


output
217 kworker/u:3
218 kworker/u:4
219 kworker/u:5
863 NetworkManager
900 ./a.out work

Search & Destroy
 pkill [pattern]

It will find and kill all the process that matches the Pattern.