how would you read a file into an array of lines
[ link: 20090610150039 | tags: bash shell zsh | updated: Fri, 15 Jan 2010 22:27:56 ]
I was working on a shell script that needed to look at some lines in a bunch of files and perform some data mining. I started it in bash, so I am writing it in bash even though dave0 notes that I should have started in in perl. Point taken... I suffer for my mistake.
After a quick google I learned that a nice way to do what the topic of this post describes can be done using
IFS='
'
declare -a foo=( $(cat $file) )
Which is great! Right?
shrinking URLs
[ link: shorturl | tags: script bash shell | updated: Sat, 13 Feb 2010 12:35:12 ]
I wrote a short script to shrink URLs:
% shorturl http://www.jukie.net/~bart/shorturl
http://2tu.us/ce8
% shorturl
Type in some urls and I'll try to shrink them for you...
http://www.jukie.net/~bart/shorturl
http://2tu.us/ce8
http://www.jukie.net/~bart/20090320214228
http://2tu.us/ce9
I am doing this as part of my new identi.ca addiction^W
usage and extending GregKH's command line micro blogging tool.
UPDATE: also picked up by @vando for use with mcabber.
cloning xterms in wmii+ruby
[ link: 20070112131252 | tags: ruby desktop bash wmii | updated: Fri, 15 Jan 2010 22:27:55 ]
I have recently added a few things to by wmii+ruby configuration that I wanted to share. These are:
- start a program in a given view from bash prompt (authored by Dave O'Neill)
- start a program in a given view using
Alt-Shift-p(authored by Jean Richard) - start an xterm in a given view using
Alt-Shift-Return - cache directory changes in a view, start an xterm in the view's last directory using
Alt-Apostrophe
shell commands
[ link: shell-commands | tags: bash linux | updated: Fri, 15 Jan 2010 22:27:55 ]
I saw this blog post by Debian's Florian Ragwitz, and ran my own list of most commonly used shell commands. Here they are...
history |awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}' | sort | uniq -c | sort -r | head -15
627 git
266 vim
98 cd
76 grep
69 ls
63 gitk
60 ssh
51 sudo
47 vv
47 apt-cache
40 cat
34 make
33 patch
30 rm
25 man
generating html colourized sourcecode
[ link: generating-colourized-source-html | tags: vim html bash | updated: Fri, 15 Jan 2010 22:27:55 ]
I wanted to have vim colouring of source files in html format. There is Text::VimColor perl module, but it's not in Debian.
Vim has a :TOhtml command (see :h syntax). I wrote a tohtml shell script to solve the problem using :TOhtml. And yes, the html was generated with itself.
bash vi editing mode
[ link: 20040326082602 | tags: bash vim | updated: Fri, 15 Jan 2010 22:27:55 ]
For a few years now I've been using vi editing mode for bash and anything that uses readline. Here is how I've set things up.
In .bashrc I use the following to enable vi editing mode:
set -o viThis allows me to type as usual and use ESC to get into vi command mode. Since ESC is so far away I frequently use control-[... unless I feel I need the exercise.
Ok, so the whole ESC and even control-[ sometimes gets tired. For example if I want to search history I would have to push ESC to get into vi-command mode and then be able to push J or K to go through history. I found a way around that by using the 'meta convert' feature of many terminal programs. In xterm, for example, setting the eightBitInput to false will cause xterm to convert all Meta-something key combinations to ESC-something which means that I can push Meta-k to go up 1 in history. It just so happens that all the other terminals I've used have this working already, but anyway, here is the line in my .Xdefaults:
xterm*eightBitInput: falseNote that once you push Meta-something you are in vi-command mode so to go up by two lines in history you would use Meta-k k, or use J & K to go up and down as you wish. everything else
Many programs use readline, as bash does, to present a command line to the user. In particular I wanted to get lftp, and others, to use vi editing mode as well. For this I set the following in the .inputrc file:
set editing-mode vi
set keymap vi
set convert-meta on
Now when I start up lftp, I can use vi editing just like in bash.
making tab completion more advanced
I noticed that the vi editing mode in bash has much fewer commands defined then the emacs counterpart. The commands are available but just not bound to any shortcuts. Here is a list of extra bindings that I've added to make TAB-completion, which makes bash so great, a bit more useful:
# ^p check for partial match in history
bind -m vi-insert "\C-p":dynamic-complete-history
# ^n cycle through the list of partial matches
bind -m vi-insert "\C-n":menu-complete
The comments give away the secrets of what the commands do. They don't work
exactly like the vi equivalents for completion, but also provide a bit more
functionality.
clearing the screen
Last feature I missed was to be able to clear the screen with control-L, as defined in emacs editing mode. I've added the following to bind the same in vi editing mode:
# ^l clear screen
bind -m vi-insert "\C-l":clear-screen
further reading
Here are a few links that you might find useful:
