selfjungle Just another WordPress weblog

6Dec/110

bash case-insensitive filename tab-competion

Edit /etc/inputrc:

add this line:

set completion-ignore-case on
Tagged as: No Comments
5Dec/110

shortcuts to find and grep

Writing these 2 line way too much a day.

Edit ~.bashrc:

function gr {
grep -Hrni $1 *
}

function f {
find . -iname *$1* | grep -i $1
}
Tagged as: No Comments
19Jul/110

find and remove files

With rm and find:

rm $(find . -name *.gcda)

Or with the -exec paramter of find

find . -name *.gcda -exec rm -rf {} \;
Tagged as: No Comments
18Jul/110

choose default answers at make oldconfig

Linux command yes outputs parameter string.

yes "" | make oldconfig
Tagged as: No Comments
4Feb/110

gcc, colorgcc, lcov, valgrind

At home I usually don't create makefiles when my program is so small that it fits into one file.

However compilation errors are more readable with colorgcc, and having as much warnings/errors at compilation time as possible is even better.

.bashrc:

GCC_ARGS="-Wall -Werror -pedantic -Weffc++ -Wshadow -ggdb --coverage"
alias g++="/usr/lib/colorgcc/bin/g++ $GCC_ARGS"
alias gcc="/usr/lib/colorgcc/bin/gcc $GCC_ARGS"

The -ggdb puts debug symbols to the binary and --coverage will make the binary create .gcda and .gcno files at runtime.

To create a nice coverage-html lcov needs more steps (3) than I'm willing to type everytime so the following line in the .bashrc do the work. Note that the lcov_all is function, because argument passing is not possible with aliases.

alias lcov_reset="lcov --directory . -z ; rm -f ./lcov.info"
alias lcov_capture="lcov --directory . --capture -o lcov.info"
alias lcov_html="rm -rf ./cov ; mkdir cov ; genhtml -o ./cov lcov.info"
function lcov_all() { lcov_reset ; $1 ; lcov_capture ; lcov_html ; }

The best way to alter valgrind's behavior modifying the .valgrindrc:

--leak-check=full 
--show-reachable=yes 
--malloc-fill=0xaa 
--free-fill=0xbb

So when I program follow the following steps:

  1. Edit the source.
  2. g++ <sourcefile>
  3. run <binary>
  4. Check leaks: valgrind <binary>
  5. If coverage is needed, lcov_all <binary>
Tagged as: , No Comments
3Feb/110

command line GTD – task

A todo app is always handy, when you want to keep your shit together.

If you are not familiar with Getting Things Done from David Allen, do some googling & torrents reading, it worths the effort.

After trying ikog I settled with task, which is much richer in features (import/export vcalendars, etc)

I added the following lines to my .taskrc, which set some params and define a new view called l1.

defaultwidth=100
editor=vim

report.l1.columns=id,project,priority,due,tags,description
report.l1.filter=status:pending
report.l1.labels=ID,Project,Pri,Due,Tags,Description
report.l1.sort=due+,priority-,project+

So when I append the line to my .bashrc:

task l1

Every time I open a new terminal, I got reminded to my tasks.

Tagged as: , No Comments
29Jan/110

put/get files to/from ftp server non-interactively

I decided to sync my work/home computers' calendar/contact files.

Since I wanted to avoid gmail and other closed solutions, I chose ftp as a transfer method and after some search I found ncftp which does the job.

Now I can issue the get script after login and the put before logout at each machine.

#!/bin/bash
# get files from remote server

ncftpget -u USER -p PASS FTPSERVER /LOCALPATH/ std.ics
ncftpget -u USER -p PASS FTPSERVER /LOCALPATH/ std.vcf
#!/bin/bash
# upload files to remote server

ncftpput -u USER -p PASS FTPSERVER . /LOCALPATH/std.ics
ncftpput -u USER -p PASS FTPSERVER . /LOCALPATH/std.vcf
Tagged as: , No Comments
3Jun/090

bash shortcuts

A good set of shortcuts speeds up usage of every program, this applies to bash too.

My favourites
Home Move to the start of the line.
End Move to the end of the line.
Left Move back one character.
Alt + Right Move back one word.
Right Move forward one character.
Alt + Right Move forward one word.
Ctrl + u Delete from the cursor to the beginning of the line.
Ctrl + k Delete from the cursor to the end of the line.
Ctrl + w Delete from the cursor to the start of the word.
Esc + d Delete from the cursor position to the end of the word.
Ctrl + l Clear the screen leaving the current line at the top of the screen.
Alt + r Undo all changes to the line.
Ctrl + r Incremental reverse search of history.

Some more (incomplete lists)

CTRL Key Bound
Ctrl + a Jump to the start of the line.
Ctrl + b Move back a char.
Ctrl + c Terminate the command.
Ctrl + d Delete from under the cursor.
Ctrl + e Jump to the end of the line.
Ctrl + f Move forward a char.
Ctrl + k Delete to EOL.
Ctrl + l Clear the screen.
Ctrl + r Search the history backwards.
Ctrl + R Search the history backwards with multi occurrence.
Ctrl + u Delete backward from cursor.
Ctrl + xx Move between EOL and current cursor position.
Ctrl + x @ Show possible hostname completions.
Ctrl + z Suspend/ Stop the command.
ALT Key Bound
Alt + < Move to the first line in the history.
Alt + > Move to the last line in the history.
Alt + ? Show current completion list.
Alt + * Insert all possible completions.
Alt + / Attempt to complete filename.
Alt + . Yank last argument to previous command.
Alt + b Move backward.
Alt + c Capitalize the word.
Alt + d Delete word.
Alt + f Move forward.
Alt + l Make word lowercase.
Alt + n Search the history forwards non-incremental.
Alt + p Search the history backwards non-incremental.
Alt + r Recall command.
Alt + t Move words around.
Alt + u Make word uppercase.
Alt + backspace Delete backward from cursor.
Other keybindings
2T All available commands(common).
(string)2T All available commands starting with (string).
/2T Entire directory structure including Hidden one.
2T Only Sub Dirs inside including Hidden one.
*2T Only Sub Dirs inside without Hidden one.
~2T All Present Users on system from "/etc/passwd".
$2T All Sys variables.
@2T Entries from "/etc/hosts".
=2T Output like ls or dir.

Here "2T" means Press TAB twice

Escape Keys combinations
esc+d delete from the cursor position to the end of the word.
esc+f move forward a word.
esc+b move backward a word.
esc+t transpose two adjacent words.
Tagged as: No Comments
25May/090

Bash history

I took a look around recently how to make life with bash easier and the help of the history is definitely a key issue.

.bashrc:

# enable bash history
set -o history
# bash history file length
export HISTFILESIZE=10000
# multi-line commands are stored in the history
shopt -s cmdhist
# no duplicates and empty lines
export HISTCONTROL=ignoreboth
# do not store lines:
export HISTIGNORE="&:ls:[bf]g:exit"
# append the history to the histfile instead of overwriting it.
shopt -s histappend
# update & re-read histfile after every cmd so terminals will share
export PROMPT_COMMAND="history -n; history -a"

Useful keys

ctrl + r : search in history backward
Page up/down : complete command due to history.

For page up/down part, you need to have the following lines in /etc/inputrc:

"\e[5~": history-search-backward
"\e[6~": history-search-forward

Links

Bash-History-Facilities
Searching for Commands in the History
Keeping bash history in sync on disk and between multiple terminals

Tagged as: No Comments