Thursday, February 22, 2007

A more targeted kill

A few posts ago, we learnt how to kill by name (killall "name"). We also know how "xargs" works. (See previous posts if you don't know to use either)

While this works for a number of cases, we will often have the case where the actual command might be the same, but what matters is the path it is invoked from or some such stuff.

For example, I have the command "test" in two directories:

/home/me/scripts/test
/home/me/tools/test

I want to kill only those "test" commands which are from the "tools" directory. A little bit of awking is required. My command in this case will be:

ps aux | grep 'tools/test' | gawk '{print $2}' | xargs kill -9

However, if you want to run this remotely (in another machine), you may have to invoke
bash -c " ps aux | grep 'ssh' | gawk \"{print \"\\\$2\"}\" " within your ssh command (it will lead to a case of painful backslashitis if I included the full command here, so am taking the easy way out).

Friday, February 16, 2007

The pipe smokes

While this is not a tutorial on unix pipes , aka "|", one who uses this will notice that it does not work when dealing with output streamed to stderr...

i.e.

$cmd | cmd2
will work only on o/p of cmd that is streamed to stdout. This is a fundamental property of pipes - it will work on only one channel

A workaround to this is:
$cmd 2>&1 | cmd2

i.e. redirecting stderr to stdout, so that the pipe can read this.

Split it exactly into two "equal" halves???

Alright, you know to use the vim :sp command, and noticed that halves are supposed to be equal.

But, did you know that you can resize the splits within vim very easily?

As a start, if you are editing one file and want to open another, but not give it more than 10 lines, you would type

:10 sp file2

What if, now you are in the file2 split and want to increase its size a tad? Type
Ctrl-w +
If you now want to split the windows equally, you can typle Ctrl-w =
If you want to increase the size by 15 lines, you would type Ctrl-w 15+

Substitute the "+" for "-" if you want to decrease the size of the split you are viewing.

Wednesday, February 14, 2007

Indentation fix using vim

I know, I know, you are supposed to indent every line religiously, and use "cindent" or "autoindent" within vim to make your job easier.

But, (there's always a but), sometimes you are hacking something together, and you don't care...
Sometimes you are hacking OPC (other people's code.....similar to OPM other people's money), and its ugly....

You don't want to indent every line. What do you do?

In command mode within vim type "gg=G", and see how magic is made.....(It is "gg=G", not ":gg=G")...

Oh, I like those highlights!

When you are searching for a pattern in vim, it helps to enforce "search as you type " with highlighting. For this include the following in your .vimrc:
set incsearch
set hlsearch

It also helps while using s//, if you are trying to replace (especiallyto check for mistakes in the pattern matching), to have the search pattern highlighted. Assuming the above two lines are added in your .vimrc,

First, search for the pattern you want to replace , e.g. XXX

/XXX

This will have highlighting and incsearch on for you to see whether there are any matches, etc.

Now, type in the pattern you want to replace XXX with (e.g. YYY)

:%s//YYY

This will remember the "XXX" search, and replace instances of XXX with "YYY"