Feeds:
Posts
Comments

Archive for the ‘linux’ Category

bash has a great feature called dynamic-complete-history that will allow you to complete text at point with contents from your bash history list.

examine the following workflow :

$ touch file1.txt file-with-a-long-name.txt file3.txt
$ some commands
$ some more commands
$ change directory
$ touch file…

at this point you want to type touch file-with-a-long-name.txt to create another file with the same name in the new directory. would it not be great if i could hit a few characters and then magically complete the name just like hitting TAB in bash completes filenames. yes you can using the bash feature dynamic-complete-history.

just write

$ touch file

and then hit C-M-i (Ctrl+Alt+i) and bash will try to complete the the file name by scanning through the history list. if there are multiple matches then it will display all matching items. enter some more characters until u get a unique match and then hit C-M-i.

the gist of the matter is that if you have already typed some thing and it is in the history list then you can insert it at point with ease. this saves typing which is a good thing.

at first i wrongly assumed that this feature was provided by the gnu readline library but on examining the man page of readline i could not find the corresponding documentation. it turns out that this is a feature provided by bash. on reading the man page of bash i found that it is bound to the key M-TAB but on gnome3 M-TAB is the task switcher i.e. it cycles between open applications. fortunately the magic key chord “\M-\C-i” (Alt+Ctrl+i) is also bound to dynamic-complete-history. this i found out by examining the output of bind -p. i have no idea where this binding is defined

to learn more about bind try

$ help bind

yes that is a help not man

bash version i tested this on is GNU bash, version 4.2.24(2)-release (i686-pc-linux-gnu)

the M in the key chord is the Meta key which is the Alt key on my keyboard.

point basically means where your cursor is at.

bash history list is not the same as ~/.bash_history

Read Full Post »

guys when i run a code i get a lot of text dump on the console so i m in the habbit of issuing

“tmux clearhist && ./a.out”

so that when i scroll back i only see dump pertaining to only the current execution of a.out . turns out that this interacts weirdly with the standard input buffer. for a simple code such as the follows:

#include <iostream>
#include <string>
int main()
{
  std::string s;
  while(std::getline(std::cin,s))
    {
      std::cout << s << std::endl;
    }
return 0;
}

does not work at all with “tmux clearhist && ./a.out”.

by “does not work” i mean the program does not wait for any input and simply proceeds to completion. at first i thought that probably the standard input buffer has whitespaces or newlines sitting which i needed to clear before doing an input, but that did not work either.

so currently i have resorted to first clearing up history and then running my executable instead of chaining them together as one code. or one can use

tmux clearhist >/dev/null 2>&1 </dev/null && ./a.out

Read Full Post »

there have been some situations where i needed a custom grid on a 2-dimensional plot in gnuplot instead of a regularly placed grid. in order for me to set up a customized grid i have to first set up a customised xtics and/or ytics. gnuplot does provide a way of providing user preferred tics by manually typing them in at the gnuplot command prompt:

gnuplot> set xtics (1,2,3,4)

however that might get too tiresome for your digits. naturally one would want to employ the powerful text processors and stream editors that GNU/Linux provides. this is how i went about it:

assume that the data file(data.out, say) is as follows:

# some comments and blank lines

#      #x1	#x2 	  #x3

1      1.1	3.9	  4.5
2      2.7	6.1	  5.6
3      3.9	2.3	  3.4
       
4      3.5	4.3	  34.0
5      12.1	3.4	  15.9

i want to set the xtics to the values in column 2 and ytics to the values in column 3 say. first i conjure up the following shell script (ttf.sh, say):

#!/usr/bin/env bash

# set tic levels from file at the gnuplot prompt
# examples:
# gnuplot> `./tff.sh data.dat 1 xtics`
# gnuplot> `./tff.sh data.dat 2 ytics`

# dataFile from which tic levels will be read
dataFile=$1

# which column to use
column=$2

# xtics OR ytics 
whichTics=$3

sed -e '/^#/d' $dataFile | \
    sed -e '/^$/d' | \
    cut -f $column | \
    tr '\n' ',' | \
    sed -e "s/^/set $whichTics (/" | \
    sed -e 's/,$/)\n/'

and then in gnuplot carry out the following sequence of commands:

gnuplot> `./tff.sh data.out 2 xtics`
gnuplot> `./tff.sh data.out 3 ytics`
gnuplot> plot 'data.out' u 1:2 w p pt 13 ps 2

to produce the following plot:

it should be noted that some of the grid lines are hidden due to alignment with borders.

the shell script needs 3 inputs

  1. datafile name
  2. which column to pull from the datafile
  3. which tics to set the values to

then the whole command is sandwiched between couple of backtics which bring in the command substitution magic. one could very well do the following in bash:

$ ./tff.sh data.out 2 xtics > tmp

and then in gnuplot

gnuplot> load 'tmp'

but i prefer the backtics.

now the explanation of the wonderful sed magic. first we do a little bit of file cleaning/processing like removing comments and blank lines. these operations may not necessarily apply to your case or might need additional operations which is not that difficult a job. anyways on to the explanations now…

sed -e '/^#/d' $dataFile

simply deletes all lines starting with the # symbol. of course if the comment character is different one should change the code appropriately. ^ stands for the beginning of all lines and ^# means all lines starting with a # symbol. the `d’ command instructs sed to delete those lines. we then pipe (|) the output of this command to the next command

 
sed -e '/^$/d' 

which simply deletes all blank lines. since ^ represents the beginning of all lines and $ represents the end of all lines, ^$ then represents those lines with nothing in between ^ and $ i.e. blank lines. as always the `d’ command instructs sed to delete this line.

cut -f $column

next we cut out the required column from the datafile using the cut command. we assume here that the field separater is a TAB character. if some other field separater is being used that can be specified with the -d switch. do a `man cut` to find out more. now comes the all important part of the entire exercise, what we want is to transform the vertical stack of values:

1
2
3

to a horizontal comma separated list of values:

1,2,3,

notice the comma after 3.
we achieve this by using the tr command which we instruct to transform all the newline characters (`\n’) to the comma character (`,’). we do pick up an unwanted comma at the very end of the list which we get rid of and transformed to a right bracket ) like so:

sed -e 's/,$/)\n/'

this time we are using the subsitute command `s’ and asking sed to replace the last comma by a ). as you already know $ represents the end of the line so (,$) represents the last comma we substitute those with a right bracket ) and a newline character `\n’.
of course we had to prepend the “set xtics (” to the beginning of the list and that bit is done by the following:

sed -e "s/^/set $whichTics (/"

notice the double quotes instead of the single quotes. we use double quotes so that $whichTics is automatically expanded to the user supplied tics name.

Read Full Post »

better bash history

let me first describe the problem that way annoying me for a long long long time.

i use tmux and do my compiling and coding in one window and then many a times i need to open a new window to do some other stuff. the problem i face is that the bash commands that i write in one window are not present in the new window’s bash history.

for example note a typical annoyance

cd code/genetic/convergence/zdt1/population100/

do some work and then create a new window in tmux (C-b c) in order to do some work in the same directory.

and now most probably you will be placed in your home folder if that is where you started tmux initially.  so now in the new window i have to do this long change directory command.  the long cd command will not be written to bash history unless the shell exits.

this was a major annoyance to me for a long time. i wanted all the bash sessions to share the history concurrently.  i found a hack which lets me do what i want.

put the following in your .bashrc

export PROMPT_COMMAND="$PROMPT_COMMAND; history -a"
shopt -s histappend

If the histappend shell option is enabled the lines are appended to the history file, otherwise the history file is overwritten. to find out more about histappend just do a man bash and search for histappend.

the history command we refer to here is the bast built-in history command to know more about it do a

help history

yes that is help history **not** man history

if i have understood it correctly i guess the cleverness of the hack is in automatically doing a “history -a” after each command and what better way than to modify the PROMPT_COMMAND which defines how the prompt should be constructed each time. sweet :)

Read Full Post »

make sure that your ~/.Xresources file contains the following line

XTerm*metaSendsEscape: true

Read Full Post »

urxvt-tabbed

am having a lot of fun experimenting with archlinux and learning the proper way to approach linux … the arch philosophy.  so far i have had lots of fun learning various things about the way linux operates … the archwiki is a wonderful place to learn about so many things … the #archlinux irc channel on freenode is a great great place with wonderful ppl always ready to
help you and suggest the best practices to follow … while there is no need for me to write down the trivialities i m about to blog about (as they are already mentioned on the archwiki) but i will do it nonetheless as i have nothing
better to do at the moment … :P

i m currently using openbox as a standalone window manager and am using the rxvt-unicode (urxvt) terminal program. in fact i m using the urxvt daemon (urxvtd) and simply launching the urxvt client (urxvtc) which attaches itself to the daemon. this greatly reduces memory requirement. i have a low end laptop IBM Thinkpad R51 (238MB ram/ 1.4GHz Pentium M processor) and i love to use firefox which means i must conserve as much memory as possible.

moving on to the central matter of this post now …

i had great difficulties in using urxvtc as i was used to gnome-terminal and its tabs. i was missing it greatly … so i set about searching for a tabbed terminal that was light on system resources. turns out that that urxvtc already was tab capable. there are two ways of using tabs in urxvt:

  1. urxvt -pe tabbed or urxvtperl
  2. urxvt-tabbed

approach 1:
to use urxvtperl simply add the following to your ~/.Xdefaults

URxvt.perl-ext-common: default, tabbed

that is it. the only problem i m facing right now is i do not know how to bring about the changes without having to restart my session. so like a fool all i do is log out and log in again. please let me know if you have a sensible solution to
bring in to effect changes made to .Xdefaults. anyways … now when you log in and launch urxvt/urxvtc you will see the following:

to open a new tab simply hit Shift+DownArrow and to move between tabs use Shift+LeftArrow and Shift+RightArrow. this works great but looks ugly in my opinion.

approach 2:
instead of changing the .Xdefaults you may also launch the tabbed version like this:

urxvtc -pe tabbed

now let us have a dekko at the urxvt-tabbed version which is gtk2 based.

urxvt-tabbed is installed automatically when you install urxvt … i installed urxvt as follows

sudo pacman -S rxvt-unicode-256color

that will also install the urxvt-tabbed version.

however when i launched urxvt-tabbed i got the following error:

Can’t locate Gtk2.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.10.1 /usr/share/perl5/site_perl/5.10.1 /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5/core_perl /usr/share/perl5/core_perl /usr/lib/perl5/current /usr/lib/perl5/site_perl/current .) at /usr/bin/urxvt-tabbed line 14.
BEGIN failed–compilation aborted at /usr/bin/urxvt-tabbed line 14.

turns out that i was missing gtk2-perl which is in the extra repo of arch so i did a

sudo pacman -S gtk2-perl

and that solved my problem with urxvt-tabbed which looks like this:

unfortunately i could not discover any keyboard shortcuts to create new tabs and move between them … :(

so urxvtperl looks ugly but has keyboard shortcuts, urxvt-tabbed looks professional but is keyboard unfriendly … none of this really solves my problem …

so i presented my dilemma to the local gentoo guru, vivek who is taken aback and immediately retorts with:
“why do you need tabs? why don’t you use screen or tmux?”

hmmmm… this was interesting … why not indeed?


sudo pacman -S tmux

so the current set up i m using is as follows:

urxvtd -q -f in ~/.config/openbox/autostart.sh

urxvtc and then start a tmux session there

you r good to go!

the good thing about tmux over screen is that the info bar at the bottom is a great help … it also displays date and time which i was missing as i have no panels installed in openbox nor am i using conky.

p.s. i will try and arrange so that tmux automatically gets started when i launch urxvtc … but that is for later :)

remember that urxvt respects .Xdefaults NOT .Xresources.

  1. .Xdefaults
  2. urxvt
  3. tmux
  4. archlinux
  5. openbox

Read Full Post »

gnuplot annoyance

i hate myself for writing that post title… the joy of free/libre software should muffle any feeling of annoyance or hatred towards a software, so i apologize in advance. anyways…

i wonder why the following does not work in gnuplot


plot "a.dat" u 1:2 w p, [0:1] 1-sqrt(x)

while the following does


plot [0:1] 1-sqrt(x), "a.dat" u 1:2 w p

why should the order be important?

i m sure it has a perfectly logical explanation… which i have yet to discover!

Read Full Post »

bash has emacs key binding if you need to navigate around the current command and edit it … most novices ( that includes me as well) use the arrow keys to navigate and fix the errors or change the commands however i watched a nice screen cast on bash command line editing which explained how one could use the Meta key to navigate one word at a time instead of one character at a time using the arrow keys. on the pc keyboard the Alt key is mapped to the Meta key unfortunately when i tried to use the Meta key on the gnome-terminal on ubuntu hardy heron it interered with the predefined menu shortcut keys (Alt+F etc. ) that allow easy menu access from the key board. note the underlined letters in the menu bar of gnome-terminal in the following picture

now that Alt was being used for something else that i rarely used i was in a fix. i first tried using the Esc key as a Meta key but it is too far on the keyboard and keeping it pressed continuously caused some weird beeps and flickers so that was the end of it.

turns out that one can switch off the menu shortcuts in gnome-terminal as follows. go to the Edit menu and choose Keyboard Shortcuts and check the following options disabling the use of Alt key in menu shortcuts.

that is it you are are done :)

more power to the keyboard and more power to emacs  :D

Read Full Post »

emacs cpu hog

i m on ubuntu 8.04 ( hardy heron ) and every once in a while emacs freezes … i use htop to find out that emacs and X are hogging up cpu cycles … when i kill emacs it all returns to normal.

some times i see the same thing happening when i have firefox open but this is quite rare … this leads me to believe that probably emacs alone may not be the culprit. but how can i find out exactly what is causing the problem ?

Read Full Post »

this pertains to ubuntu 8.04 hardy heron though it might work for others too.

the problem::

the dhcp client renews the resolv.conf file to the following

domain cc.iitk.ac.in junta.iitk.ac.in .iitk.ac.in
nameserver 172.31.1.1
nameserver 172.31.1.130

though i want it to read

domain cc.iitk.ac.in junta.iitk.ac.in .iitk.ac.in
nameserver 172.31.1.130
nameserver 172.31.1.1

notice the order of the nameservers have changed. so each time at boot up or network restart i had to manually edit the resolv.conf file but a simple change in dhclient.conf makes my life just a little bit easier.

i opened dhclient.conf (/etc/dhcp3/dhclient.conf) and found the section

request subnet-mask, broadcast-address, time-offset, routers,
domain-name, domain-name-servers, host-name,
netbios-name-servers, netbios-scope;

which basically meant that by requesting domain-name-servers new entries would be added to the resolv.conf file. if domain-name-servers is removed from that list then my problems are solved. as i have to specify resolv.conf only once and expect the domain name servers won’t be changed.

so the new request section of dhclient.conf looks like

request subnet-mask, broadcast-address, time-offset, routers,
domain-name, host-name, netbios-name-servers, netbios-scope;

and that solves my problem :)

Read Full Post »

Older Posts »

Follow

Get every new post delivered to your Inbox.