Saturday, 28 November 2015

emacs motion

emacs motion

C-f: forward one char
C-b: backward one char
C-p: previous line
C-n: next line

C-a: beginning of line
C-e: end of line
M-f: forward one word
M-b: backward one word

C-v: forward one screen
M-v: backward one screen
M-a: beginning of sentence
M-e: end of sentence
C-l: center the screen
M-r: cycle point through top, middle and bottom of the screen
M-<: jump to the top of the buffer
M->: jump to the end of the buffer
M-g c: jump to the nth character
M-g l: jump to the nth line

C-M-f: forward over balanced expression (eg: braces in C++ code)
C-M-b: backward over balanced expression
C-M-k: delete balanced expression
C-M-<SPC> or C-M-@: mark balanced expression

emacs ibuffer

ibuffer

In ~/.emacs.d/init.el:

;; replace `list-buffers` with `ibuffer`
(global-set-key (kbd "C-x C-b") 'ibuffer)

Open ibuffer by C-x C-b

Create filters:

/m: filter by a major mode
/n: filter by buffer name.
/c: filter by buffer content.
/f: filter by filename
/>: filter by buffer size
/<: filter by buffer size
//: remove all filters in effect

Filtered groups:

/g: create a filter group from filters
TAB: move to next filter group
M-p: move to previous filter group
/\: remove all active filter groups
/S: save the current groups with a name
/R: restore previously saved groups
/X: delete previously saved groups

Every time you open a file which matches a filter it will appear under the group

o on a file will open that file and switch focus to it
C-o on a file will open that file and leave focus in ibuffer
C-x o switches focus to the other window

Saturday, 7 November 2015

Create a bootable USB from an ISO using dd on the Linux command line

Install syslinux, a suite of utilities which ensures the iso image is in SYSLINUX format rather than ISOLINUX

    $ sudo apt-get install syslinux

Convert the iso image to SYSLINUX format

    $ isohybrid /path/image.iso 

Locate the USB device 

    $ lsblk
    sdb         8:16   1   3.8G  0 disk 
    └─sdb1      8:17   1   3.8G  0 part /media/user/usb_disk

Unmount the USB device

    $ sudo umount /dev/sdb1

Ensure it is indeed unmounted

    $ lsblk
    sdb         8:16   1   3.8G  0 disk 
    └─sdb1      8:17   1   3.8G  0 part 

Copy the ISO image onto the USB deisk

    $ sudo dd if=/path/image.iso of=/dev/sdb1

Wednesday, 28 October 2015

Qt: Updating styles based on dynamic properties

#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QStyle>
#include <QDebug>

class App : public QObject
{
    Q_OBJECT
public:
    enum Alert
    {
        None,
        Warning,
        Critical
    };
    Q_ENUM(Alert);

    App(int argc, char** argv)
        : app(argc, argv)
    {}

    int exec()
    {
        window.setCentralWidget(&main);

        label.setText("hello world");
        label.setProperty("alert", QVariant::fromValue(None));

        button.setText("alert");

        QObject::connect(&button, &QPushButton::clicked, [this]()
            {
                label.setProperty("alert", QVariant::fromValue(Critical));
                label.setProperty("foo",   false);

                label.style()->unpolish(&label);
                label.style()->polish(&label);
                label.update();
            });

        layout.addWidget(&label);
        layout.addWidget(&button);
        main.setLayout(&layout);

        app.setStyleSheet(R"(
            QLabel[alert="None"]                { color: green; } 
            QLabel[alert="Warning"]             { color: blue;  }
            QLabel[alert="Critical"][foo=true]  { color: red;     }
            QLabel[alert="Critical"][foo=false] { color: magenta; }
        )");

        window.show();
        return app.exec();
    }

    QApplication app;
    QMainWindow  window;
    QWidget      main;
    QLabel       label;
    QPushButton  button;
    QVBoxLayout  layout;
};

int main(int argc, char** argv)
{
    return App(argc, argv).exec();

}


Tuesday, 6 October 2015

Gnome - change default application for text files

xdg-mime query default text/plain
xdg-mime default sublime_text.desktop text/plain

xdg-mime query filetype application/x-shellscript
xdg-mime query default application/x-shellscript

Wednesday, 19 August 2015

Send email from script

Install ssmtp:

    $ sudo apt-get install ssmtp

Edit the ssmtp config file:

    $ sudo vim /etc/ssmtp/ssmtp.conf

Enter this in the file:

root=username@gmail.com
mailhub=smtp.gmail.com:465
rewriteDomain=gmail.com
AuthUser=username
AuthPass=password (create a app-specific password in google accounts)
FromLineOverride=YES
UseTLS=YES

Enter the email address of the person who will receive your email:

    $ ssmtp recepient_name@gmail.com

Now enter this:

To: recipient_name@gmail.com
From: username@gmail.com
Subject: Sent from a terminal!

Your content goes here. Lorem ipsum dolor sit amet, consectetur adipisicing.
(Notice the blank space between the subject and the body.)

To send the email: Ctrl + D

You can also save the text mentioned in Point 5 into a text file and send it using:

    $ ssmtp recipient_name@gmail.com < filename.txt

http://askubuntu.com/questions/12917/how-to-send-mail-from-the-command-line

Friday, 7 August 2015

Supervisord & Rundeck

supervisord

install

$ sudo easy_install supervisor

generate SHA-1 hash of password to be used for TCP access

$ echo -n password | sha1sum | awk '{print $1}'

enable TCP access

sudo vim /etc/supervisor/supervisord.conf

[inet_http_server]
port = 127.0.0.1:9001
username = user
password = {SHA}1235678

configure supervisorctl command line access (note password cannot be SHA hash, has to be plaintext)

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
serverurl=http://localhost:9001
username = user
password = abcdef

add a program

sudo vim /etc/supervisor/conf.d/app_example.conf

[program:app_example]
command=app_example
directory=/path/to/app
autostart:false
autorestart:false
startsecs:5
startretries:3
user:prod
redirect_stderr:true
stdout_logfile:/path/to/app/logfile.log
stdout_logfile_maxbytes:1000GB

supervisorctl - enter interactive mode

supervisorctl -s http://localhost:9001 -u user -p password

start/stop

    supervisorctl -s http://localhost:9001 -u user -p password start group:app

reread/restart/update

supervisorctl -s http://localhost:9001 -u user -p password update (or reread, restart)

reread just reads config changes, but doesn’t restart any affected processes
restart restarts the names process without loading any config changes
update rereads and restarts apps with changed configuration

rundeck

download and install from http://rundeck.org/downloads.html

add user to rundeck group

$ sudo usermod -a -G rundeck steve

add write permissions to rundeck group

$ sudo chmod g+w -R /var/rundeck