WeeChat – grep.py for log files

grep.py is probably one of the most useful script I have ever used. It allows you to search for a pattern in buffers and log files. To grep in a buffer, I prefer to use CTRL+R, but for logs, nothing is comparable to grep.py. In one command, you can see all URLs a certain nickname mentioned before, any time you where highlighted from anyone or someone in particular and so on.

Here I will show you some useful examples and then I will create an alias to a useful grep command. Finally, I will bind a key to use in the chat area and the nicklist.

I do not log via WeeChat but via ZNC. The format of the log and where they are stored is different. You will need to adjust it for you.

My logs goes there:

~/.znc/users/<account name>/moddata/log/<years in format aaaa>/<IRC network>/

There is a log file for each days:


correspond to June 22. If you want the same directory structure than me:

/msg *status loadmod --type=user log %Y/$NETWORK/$WINDOW.%m-%d.log

When you install grep.py, it will automatically find all these logs. Let’s get started:

/script install grep.py

this will obviously install grep.py. Here is some useful examples:

/grep log *.znc/users/r3m/moddata/log/*/freenode/#weechat.* -a ^\[\d{2}:\d{2}:\d{2}\] <%{escape [dude]}>
/grep log *.znc/users/r3m/moddata/log/*/freenode/#weechat.* -a ^\[\d{2}:\d{2}:\d{2}\] <%{escape [dude]}> .*%{url}
/grep log *.znc/users/r3m/moddata/log/*/freenode/#weechat.* -a ^\[\d{2}:\d{2}:\d{2}\] <%{escape [dude]}> .*%{url youtube.com}
/grep log *.znc/users/r3m/moddata/log/*/freenode/#weechat.* -a ^\[\d{2}:\d{2}:\d{2}\] <%{escape [dude]}> r3m

The first will match any line from nickname [dude]. Here I used %{escape } to escape meta character (append a \ before each of them). Otherwise you will probably get unpredictable results. The rest will match respectively any line containing an URL, a youtube link or an highlight (r3m is my nickname).

Next, I created an alias that will erase anything in the input bar and insert:

/grep log */#activechannel.* -a ^\[\d{2}:\d{2}:\d{2}\] <%{escape anickname}>

Then, you press enter to search for any lines from anickname or you append something to it and then press enter.

Here is the alias:

/alias add customgrep /input delete_line;/input insert /grep log *.znc/users/r3m/moddata/log/*/$server/$channel.* -a ^\[\d{2}:\d{2}:\d{2}\] <%{escape $1}>\x20

So, if I go to #weechat buffer and type:

/customgrep FlashCode

and press enter, the input line will become:

/grep log *.znc/users/r3m/moddata/log/*/freenode/#weechat.* -a ^\[\d{2}:\d{2}:\d{2}\] <%{escape FlashCode}>

then press enter again to see every lines from FlashCode.

Now, bind a key, here the ‘g’ key in cursor mode:

/key bindctxt cursor @chat(*):g /window ${_window_number};/customgrep ${_chat_line_nick};/cursor stop
/key bindctxt cursor @item(buffer_nicklist):g /window ${_window_number};/customgrep ${_chat_line_nick};/cursor stop

Now, middle-click on a nickname and press ‘g’. So useful!

Please note that if the nickname contains a \ you will need to escape it two times. For example:

/grep log *.znc/users/r3m/moddata/log/*/freenode/#weechat.* -a ^\[\d{2}:\d{2}:\d{2}\] <%{escape [\\dude]}>

Here we use %{escape } to escape ‘[‘ and ‘]’ but it also escape \ so we must add a \ in front of \.

WeeChat and conky

capture_019_01062016_221203Here is my new way to show conky in WeeChat.

First, you need to install conky-cli package, in Debian:

sudo apt-get install conky-cli

Then, create a .conkyrc file in your home directory with the following content:

background no
cpu_avg_samples 2
net_avg_samples 2
no_buffers yes
out_to_stderr no
update_interval 1.0
uppercase no
use_spacer none

Uptime: $uptime
CPU: $cpu%
RAM: $memperc%
Swap: $swapperc%
Up: ${upspeed wlan0}
Down: ${downspeed wlan0}

Configure WeeChat to run conky at start:

/set weechat.startup.command_after_plugins "/exec -norc -noln -buffer conky conky"

In WeeChat, install the following scripts:

/script install perlexec.pl highmon.pl buffer_autoset.py

Now split the window in 2 part, the new window need 8 lines: 6 for the infos provided by conky, 1 for the titlebar and 1 for the separator. (If you have a input and statusbar, add 2 more).

/perlexec my $total = weechat::hdata_integer(weechat::hdata_get("window"), weechat::current_window(), "win_height"); weechat::command($buffer, "/window splith " . int(7 / $total * 100));


/buffer perl.highmon

Now split this new window in two parts, one for highlight and one for conky:

/buffer splitv 85
/buffer exec.conky
/window 1

Now save your layout:

/layout store highlight-conky

Now create the following trigger to color conky

/trigger add conky_tag_color modifier weechat_print
/trigger set conky_tag_color conditions ${tg_buffer} == exec.exec.conky
/trigger set conky_tag_color regex /\t(Uptime|CPU|RAM|Swap|Networking|Up|Down (.*)/${color:31}${re:1}\t${color:reset}${re:2}/

Now, if you want to color CPU pct when it is over, 60, for example: (I set it to 10% in the screenshot for demonstration)

/trigger add conky_color_high modifier weechat_print 
/trigger set conky_color_high conditions ${tg_buffer} == exec.exec.conky
/trigger set conky_color_high regex /([6-9][0-9]|100)%/${color:red}${re:0}/

Of course you can add another trigger for other percentage.

If you want to have another title for the buffer exec.conky than “Executed commands” do this:

/buffer exec.conky
/autosetbuffer add exec.exec.conky title Conky - the light-weight system monitor


WeeChat – Custom Ignore

The following alias will automatically add the host of the user to the ignore add command. If you precise the bitch argument, you will ignore the user then tell the channel where you issued the command. Of course you don’t use the latter on serious network like Freenode or OFTC. AFAIK this is only used on general network like UnderNet. The default message sent to the channel is the same as the /ignore replies in mIRC. Let’s start!

First, you need the perlexec.pl script :

/script install perlexec.pl

then create the following alias :

/alias add ignore2 /eval /who ${channel};/wait 2s /eval /perlexec my $infolist = weechat::infolist_get("irc_nick", "", q(${server},${channel},$1))\;weechat::infolist_next($infolist)\;my $host = (split("@", weechat::infolist_string($infolist, "host")))[1]\;if (!defined($host)) { weechat::command("", q(/print -core I dont have the host of $1 please try again))\; } else { if (lc(q($2)) eq lc("bitch")) { weechat::command($buffer, q(* Added *!*@) . $host . q( to ignore list))\; } weechat::command("", q(/ignore add *!*@) . $host . q( ${server}))\; } weechat::infolist_free($infolist)\;

Now, if you ignore someone without the bitch argument, you should receive something similar to this in the weechat core buffer:

/ignore2 stupiduser
irc: ignore added:
[8] mask: .*!.*@ip\.ip\.ip\.ip / serverL UnderNet / channel: *

you can unignore the host with

/ignore del 8

if you ignore someone with the bitch argument, you should see this on the channel:

/ignore2 stupiduser bitch
r3m: * Added *!*@ip.ip.ip.ip to ignore list

Now, if you want to be able to ignore people via the nicklist or the chat area, here is how to do it for chat area:

Middle-click on the nickname in the chat area and then press ‘i’ for a ignore or ‘I’ for a bitch ignore.

/key bindctxt cursor @chat(*):i /window ${_window_number};/ignore2 ${_chat_line_nick};/cursor stop
/key bindctxt cursor @chat(*):I /window ${_window_number};/ignore2 ${_chat_line_nick} bitch;/cursor stop

Now for the nicklist:

Middle-click the nickname in the nicklist and then press ‘i’ for a ignore or ‘I’ for a bitch ignore.

/key bindctxt cursor @item(buffer_nicklist):i /window ${_window_number};/ignore2 ${nick};/cursor stop
/key bindctxt cursor @item(buffer_nicklist):I /window ${_window_number};/ignore2 ${nick} bitch;/cursor stop


btw: if you are wondering why I put /who ${channel} instead of the nick, read this. Also replace /wait 2s to something higher if you don’t receive the /who reply before /ignore is called. If WeeChat don’t have the host, nothing will happen, except you will receive a message in core buffer telling you to try again.