code

There are times that I feel lazy to open a new tab in my firefox to search for something or a youtube video, even though I have tags for every important thing that I use commonly. For example if my firefox is open, all I need to do is to press ctrl+t to open a new tab, then ctrl+k to go to search bar, and then type in my search query and press enter to search my term in DuckDuckGo. Though that ctrl+k is not really needed! Or when I have a new tab, all I need to do is to type yt my search query to search it in youtube. Firefox has this great shortcuts for this searches. But still sometimes I feel lazy. So, I open a terminal (F12 opens up my Guake terminal) and then type ytp my search query and it does the same youtube search for me, or other things (see below). The whole idea is very simple. Here is what I do. I have some bash scripts in a folder at ~/opt. I’ve added the address of this folder to my ~/.bashrc file with: PATH=$PATH:"~/opt"

And in my opt file, I put my bash scripts. So to do a search you actually first need to read the search query from terminal. In all this scripts I use the following to read:

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter your search term: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

So, really what it does is if I run my command like ytp my search query it puts the three terms my and search and query together in $searchterm. But if I just run ytp without anything, it asks me to enter the search terms in the next line. If I just press enter one more time, then it passes an empty string, which is sometimes good to open an empty google page 😀 I also do this with my contacts in google!

Then the rest of the scripts are just simple commands to do something with that search term. Here are a few examples:

Google things: (I save this in a file called google)

#!/bin/bash

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter your search term: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

firefox http://www.google.com/search?q=$searchterm

But you better be searching with DuckDuckGo instead of Google!
Search using DuckDuckGo: (I save this in a file called ddg)

#!/bin/bash

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter your search term: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

firefox https://duckduckgo.com/?q=$searchterm

Search Youtube (I save this in a file called yts)

#!/bin/bash

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter your search term: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

firefox https://www.youtube.com/results?search_query=$searchterm

Search Google Contacts (I save this in a file called contacts. Don’t use gc since it’s reserved for some other commands! See for yourself by typing whatis gc in terminal :p )

#!/bin/bash

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter your search term: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

firefox https://contacts.google.com/u/0/preview/search/$searchterm

But here is the most important thing. If you want to play youtube videos from terminal using player use the following script. But you need to get the video ID from its url first. For example if this is the video you’re watching: https://www.youtube.com/watch?v=daQhI1JFXn4 Then the video ID is the part after v= which is daQhI1JFXn4 and feed this to your shell script when calling it in terminal. I need to figure out how to join this with my youtube search. I mean, I want to search for a term and then ask youtube to play the first result in the terminal using mplayer. It shouldn’t be too hard, but I don’t know how to do it yet.

There could be several reasons you might want to play some youtube video in the terminal. Here is the code:

Play youtube in terminal: (I save this in a file called ytp)

#!/bin/bash

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter the video ID: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

mplayer $(youtube-dl -g https://youtube.com/v/$searchterm)

You need youtube-dl and mplayer to be installed, of course. And in case you don’t remember all the handy keys to control mplayer, here is list of some of them:mplayer shortkeysIf you’re watching an online course from youtube, I think { and } will come handy for the speed. Now, if you want to do play something on an infinite loop, do this:

Play youtube in terminal on an infinite loop: (I save this in a file called ytx)

#!/bin/bash

if [[ $(echo $*) ]]; then
    searchterm="$*"
else
    read -p "Enter the video ID: " searchterm
fi

searchterm=$(echo $searchterm | sed -e 's/\ /+/g')

video=$(youtube-dl -g https://youtube.com/v/$searchterm)

[[ -f ~/Videos/$searchterm.mp4 ]] || wget -O - $video | tee ~/Videos/$searchterm.mp4 2>&1 > /dev/null

while true; 
do 
    sleep 2;
    mplayer ~/Videos/$searchterm.mp4
done

When you’re tired of playing that video you need to press ctrl+c twice, to first exit the mplayer, then exit the script. Or, you could get a number and make a for loop and play the file that many times! What is slightly different in this is I first download the file and put it in a loop to play for ever. This saves some bandwidth! (I got the solution here: http://unix.stackexchange.com/questions/233928/downloading-a-file-once-and-playing-it-multiple-times) I’m actually saving the files with their ID, and I could play them later.

The line before the while loop, actually searches if that video ID is already downloaded, and it won’t download it again, if it exists. || means exclusive OR. I don’t wanna use this for the previous version, because it downloads the whole video fist and then plays it, and the file remains there. It would be good if I’m playing something, I’ll check to see if it’s downloaded, and if not to do download it though, but it seems the code will get a little complicated. I’ll look into it later.

What I would like to do for now is to see if I can tag my videos or add titles from the youtube title so that they’re also human readable. Because why not?

Search and play things from terminal

Aside
code
  • I use my tablet to write with its pen quite a lot, whether for taking notes, annotating a pdf file, or teaching. Here is a nice little script that I’ve made to get the screen to rotate and disable the touchpad, trackpad and touchscreen so that I can easily write with pen:
    #!/bin/bash
    status=$(xrandr --verbose | grep LVDS1 | awk '{print $5}')
    if test inverted = $status
    then
    xrandr -o normal && xsetwacom list | grep stylus | awk '{print $7}' | xargs -Idevice xsetwacom set device Rotate none && xsetwacom list | grep eraser | awk '{print $7}' | xargs -Idevice xsetwacom set device Rotate none && xinput list | grep TouchPad | awk '{print $6}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 1 && xinput list | grep TrackPoint | awk '{print $6}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 1 && xinput list | grep Finger | awk '{print $7}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 1 
    fi
    
    if test normal = $status
    then
    xrandr -o inverted && xsetwacom list | grep stylus | awk '{print $7}' | xargs -Idevice xsetwacom set device Rotate half && xsetwacom list | grep eraser | awk '{print $7}' | xargs -Idevice xsetwacom set device Rotate half && xinput list | grep TouchPad | awk '{print $6}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 0 && xinput list | grep TrackPoint | awk '{print $6}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 0 && xinput list | grep Finger | awk '{print $7}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 0 
    fi
    
    ##xrandr --output LVDS1 --rotate inverted
    ##xsetwacom list | grep stylus | awk '{print $7}' | xargs -Idevice xsetwacom set device Rotate half
    ##there is no touch in xsetwacom anymore:
    ##xsetwacom list | grep touch | awk '{print $7}' | xargs -Idevice xsetwacom set device Rotate half && 
    ##xsetwacom list | grep touch | awk '{print $7}' | xargs -Idevice xsetwacom set device Rotate none &&

    I’m keeping some of the old commands there so that I can refer back to them later. Add this to an sh file and run it. I’ll have to add more details to it later. You could of course add two more cases of rotated left and right if you use them.

  • If you just want to disable the touchpad, trackpad and touch screen without rotating the screen try the following:
    #!/bin/bash
    status=$(xinput list-props "SynPS/2 Synaptics TouchPad" | grep 'Device Enabled' | awk '{print $4}')
    if test 0 = $status
    then
    xinput list | grep TouchPad | awk '{print $6}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 1 && xinput list | grep TrackPoint | awk '{print $6}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 1 && xinput list | grep Finger | awk '{print $7}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 1 
    fi
    
    if test 1 = $status
    then
    xinput list | grep TouchPad | awk '{print $6}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 0 && xinput list | grep TrackPoint | awk '{print $6}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 0 && xinput list | grep Finger | awk '{print $7}' | sed 's/^id=//' | xargs -Idevice xinput set-prop device "Device Enabled" 0 
    fi
  • And of course I’ve added them to my .bashrc and set launchers for them to access them easily with mouse:
    launchers
  • You can get the icon files from here:
    rotate touch

Rotating X and disabling/enabling touch

Aside
Uncategorized

And while I’m at it, let me get this post started. A lot of times it happens that I have to work on a windows machine for a period of time. While I’m still trying to figure out how to get rid of the annoying obligatory update which breaks the computer each time, I use some apps that do the basic things for me. Here are a few (I’ll add more as I go):

  1. There is a directory of all free softwares available here: FSF
  2. You can install tons of KDE software on windows: https://windows.kde.org, for example you can install okular for reading pdf files and annotating them.
  3. Light Screen is a nice screen capturing app. It provides a simple taskbar icon and it has the ability to copy the captured area of the screen directly to the clipboard, so I can paste it in whatever app I use (usualy Journal). On a Linux machine I prefer to use shutter. It is light and it provides much more.
  4. MS Journal in my opinion is the only app by microsoft that’s not only worth working with, also it satisfies me. I take all my notes with it when I’m on a windows machine, and its linux clown xournal is comparable to it, and in many cases much better.
  5. Firefox plays the the HTML5 and flash content automatically. To stop that I did several things that I’m not sure which one helped. But here is a list of all of them:
    1. Install adobe falsh plugin from adobe’s site (this is ironic but I need it to access the shockwave options)
    2. In about:config set the followings:
      1. plugins.click_to_play to true
      2. media.windows-media-foundation.enabled to false
      3. media.autoplay.enabled to false
    3. In about:addons set the Shockwave flash to ask to activate
      This will result in firefox asking you to allow flash content to be played each time. So if you allowed it and then it started to keep autoplaying those content again click on the icon on the left side of the URL and go to permissions tab and fix the permissions.
  6. For compiling tex files I use MikTex. Don’t forget to install updates after installation.
  7. For editing tex files I prefer TexMaker, since I also use it on my Linux. It also comes with a portable version. It’s dark theme is also very comfortable.
  8. Notepad++ for the win.

Things to do on a windows machine

Aside
code

There are several things that I need to do on a monthly or yearly basis on my laptop in order to fix some problems. So far, I googled the problem and tried the solutions one by one, until I reached the one that worked. Imagining it could be a year till I faced that problem again, I wouldn’t remember which one finally worked. So, I started making a log of these problems and the solutions that worked for me. The problem is, if I keep them offline, I’ll eventually loose them, or I won’t be able to find them among all the files. If I keep them online, I won’t remember what online note-taking tool I’ve used, and under what user. So I decided to make a draft file in my email and log each of them there. The gives me the benefit of searching it easily, and having them all online in one place. But then I need to log into my email each time to do so. So, I decided to make things easier for myself, and also for some other people that might end up in my blog searching for the same thing, to post them here. Here are a few for now:

  1. Installing sagemath has just got easier in ubuntu and fedora. Directly install it from the repositories. To open the notebook, in the terminal enter sage -notebook. To open it in a specific folder, for example in the dropbox, do this:
    sage -notebook -- directory=~/Dropbox/Math/Scripts/sage

    The weird thing is that there is a blank space between -- and directory! What this does is to create a folder called sage.sagenb in the folder Scripts, and then creates the home folder in there. If you have other folders that you want the worksheets from it, just copy the admin folder from the other home to the admin folder to this home. I think it has a problem with the access of reading and writing that it doesn’t let old folder to be used directly.
    I have to figure out how to open it in terminal in a specific folder. I used to do it before, but I can’t get the syntax right, now. Here are a couple of icons that I use for the launchers. The black one for the terminal, and the blue one for the notebook. I should change them to png files and clean out the corners!
    sageBsage

  2. There is this annoying bridge network that some app starts in my fedora, and I can’t figure out how to completely get rid of it. The bridge is called virbr0. To delete it first take it down then delete it:
    sudo ifconfig virbr0 down && brctl delbr virbr0
  3. Firefox has this problem with the outdated flash plugin. To solve it:
    1) exit Firefox
    2) delete this file: ~/.mozilla/firefox/*.default/pluginreg.dat
    3) restart Firefox and go to the Tools -> Add-ons -> PluginsUse this to locate all versions and delete all the mozilla ones and replace them by the original one: rpm -q flash-plugin
  4. To convert images to a multi-page pdf:
    convert -adjoin -page A4 *.jpeg multipage.pdf
  5. To cut a part of a video:
    ffmpeg -vcodec copy -acodec copy -i orginalfile -ss 00:01:30 -t 0:0:20 newfile

    Where 00:01:30 is the start time and 0:0:20 is the length of the video you want to cut out.

  6. Create a nifty overview of the hardware in your computer:
    lshw -html > hardware.html
  7. To find the 20 largest folders in hard drive:
    sudo du -xk | sort -n | tail -20
  8. And an easy to work with PDF annotator is okular. Unlike xournal it doesn’t rasterize the file.
  9. In order to write an iso file to a usb flash drive you can do it directly by running
    sudo dd if=ADDRESS_TO_THE_FILE.iso of=/dev/sdX bs=8M

    where sdX is the address of the mount point of the flash drive. You can get a list of all the drives by

    sudo fddisk -l

    There are several apps that do it for you, but I generally find this the easiest.

  10. And of course gnome shell is as annoying as osx and windows. So, here is how to add something to startup:
    create a file ~/.config/autostart/MYAPP.desktop and add the followings to it:

    [Desktop Entry]
    Version=1.0
    Name=NAME
    GenericName=NAME
    Comment=COMMENT
    Exec=THE_COMMAND_TO_RUN
    Terminal=true/false
    Type=Application
    

    That Version=1.0 is apparently very important! Alternatively, you could install gnome-tweak-tool and then use the options there.

  11. Fedy tool is a handy one.
  12. I can’t handle gnome shell, and gnome classic isn’t really classic. So to go back to good ol’ gnome I install mate desktop:
    sudo dnf install @mate-desktop
  13. And here is to get more familiar with dnf: http://www.tecmint.com/dnf-commands-for-fedora-rpm-package-management
  14. Once in mate, you can’t resist but to install the lovely friendly docky. Install it from the repositories. If you have tons of resources and like eye-candies, give cairo-dock a try:
    sudo rpm -ivh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm
    sudo yum install cairo-dock*

    And yes, still yum since I don’t know how dnf installs a downloaded rpm package!

  15. An app for painting that sometimes comes handy is called mypaint.
  16. A nice dictionary app with on the fly word look up and offline database is artha.
  17. To take notes using a pen or to annotate pdf files using a pen I use xournal.
  18. To see the number of a key pressed use
    sudo showkey


I’ll add references as I find them later.

Things to do on a linux machine

Aside