Category Archives: Command Line

Vim colorscheme tuneup

I hadn’t thought about it for awhile but today I updated by Vim colorscheme for the first time in about two years. In the process I found a few that were notable:

rdark-terminal

vcs_1_rdark-terminal

link

gruvbox

vimcs_2_gruvbox

link

synic

vcs_4_synic

I think this one is based on neon. link

kolor

vcs_5_kolor

link

jellybeans blueberry

vcs_7_jellybean-blueberry

This is what I decided on. It’s jellybeans with a twist. I put this in my ~/.vimrc for the adjustments:

colorscheme jellybeans
" Jellybeans colorscheme edits (not working)
"let g:jellybeans_overrides = {
"\   'Normal': { 'ctermbg': '242' }, 
"\   'CursorLine': { 'ctermbg': '238' },
"\}
highlight Normal     ctermbg=323232
highlight Normal     ctermbg=303030
highlight CursorLine ctermbg=238
highlight Visual     ctermbg=240

Others may have different results though as the theme alters dynamically as per background (mine is #323232 btw).

Download

I got these from a collection today and have put them in the AUR

Get down with getopt

What is getopt?

getopt is a command used in scripts to parse their options and add a basic error checking ability. getopt is not getopts the bash built-in that has similiar functionality. Urban Vagabond explains:

getopt and getopts are different beasts, and people seem to have a bit of misunderstanding of what they do. getopts is a built-in command to bash [that] processes command-line options in a loop and assigns each found option and value in turn to built-in variables [(so that they can be further processed)]. getopt, however, is an external utility program, and it doesn’t actually process your options for you the way that [(e.g.)] bash getopts, the Perl Getopt module, or the Python optparse/argparse modules do. All that getopt does is canonicalize the options that are passed in — (i.e. convert them to a more standard form) so that it’s easier for a shell script to process them.

For example, a use of getopt converts the following:

tmpscript -abd -ooutfile.txt

into this:

tmpscript -a -b -d -o outfile.txt

getopt can also process the long format option of --output=/tmp.... Also basic error check abilities, it has:

tmpscript -c
tmpscript: invalid option -- 'c'

or

tmpscript -o
tmpscript: option requires and argument -- 'o'

getopt gets defined by telling it the available options in short and long form (if you like). An option with a colon (:) following denotes that an argument is required for that option. If followed by two colons the argument is optional. Here is an example:

Options are generally set as variables so that they can defined how to be used after the while loop (usually, so that mainly they are processed in the right order). getopt parses in the same order as specified on the input, so a -a -d -b input would not work real well if the -d option required the -b option to run correctly.

One caveat of getopt is that each option allows either one or no arguments to follow. So in input like tmpscript -v --files file1.txt file2.txt file3.txt just isn’t possible in any type of predictable-fashion. One can however use quotes on the input:

tmpscript -v --files "file1.txt file2.txt"

or use mutiple options (-f file1.txt -f file2.txt) and append them to an array, as seen, here.

Numbered file backups – Easy as can beasy

file-twoWhen I’m about to edit a configuration file or a bash script, sometimes I don’t know if the edit will work. In these cases I create a backup of it in numbered order (to keep track of them if I need to revert the changes). Because this is something I regularly do, I decided to build a script to make this easy. The usage is basic: define the file and destination-directory:

# bckfile definedfile.bash .
‘/home/johndoe/.local/bin/definedfile.bash’ -> ‘/home/johndoe/.local/bin/definedfile_00.bash’

Optionally a tag can be added too:

bckfile youdle .vault/ new
‘/home/johndoe/.local/bin/youdle’ -> ‘/home/johndoe/.local/bin/youdle_02-new’

A limitation of the script is that filename cannot have ‘periods’ in their filename. A heuristics of discovering multiple extensions and with . in the filename itself would be tough.

gt – script to create a temp file in gedit

posticon

I am really lazy with my editors. I have aliases in my shell configuration for gedit and vim that are very basic:

alias v="vim -p" # open in tab
alias sv="sudo vim -p"
alias g="bgcmd gedit"
alias sg="bgcmd gksudo gedit"

This is very nice for me because I use my editors quite a bit. One thing I needed though was a command that would create and open a temporary file in gedit. The main reason for this is that at the time I don’t know how to name or place the file properly. Another reason is that at other times I like to have a scratchpad but would lose information if it was just a New File and a crash… happened.

Running just gt will create and open a file named of the current time (MMDDhhmm) and will be saved in the trash folder. If gt is followed by a name (e.g. gt cssbox or cssbox.css) the name will be appended to the current time.

Screenshot from 2013-08-26 08:17:12

The name is helpful if wanting to dig the file out of the trash folder at a later time.

WTFM (Write the Fine Manual) with Pod2man text-converter

Note: This is a guide from a lightly-proficient manual writer that’s targeted for beginning users: it’s formatting method is easy and conversion method is basic. This method may also be helpful for those that use github because it has rendering support for this method. For a more traditional approach, nixCraft has an article for that.

Because it was the authors first effort to write a man page, talk was that it was good to use Pod2Man. Pod2man is an application that converts text using Plain Old Document (POD) formatting to traditional manual (man) pages. POD markup is easy to do and pod2man is conveniently pre-installed on most Linux distributions (as it is part of the Perl package).

An example

To get a good idea of what to do, here is a basic example demonstrating the formatting (of a fictitious program dr-smile):


=encoding UTF8

=head1 NAME

dr-smile - output platitudes to help to improve one's mood

=head1 SYNOPSIS

dr-smile [B<--happy>] [B<--joke>=F<type>]

=head1 DESCRIPTION

B<dr-smile> outputs jokes and happy thoughts designed to make one fell F<--better>.  It has the ability to output pleasant thoughts, jokes, good memories, optimistic fortunes...  B<dr-smile> I<isn't> a replacement for a professional doctor, please use responsibly and enjoy.  Additional options are available in the configuration file F</etc/dr-smile.conf>.

=over 4

=item B<-h>B<--happy>

A general happy thought will be output (e.g. C<dr-smile --happy>).

=item B<-j> F<type>B<--joke>=F<type>

A good-sense joke will be told.  The types are: F<knock> and F<oddball>.  Read more in B<jokes(1)>.

=back

Formatting

There is both text formatting and section formatting that is required for a man page.

Text formatting

All the POD text formatting tags needed to know are as follows:

Bold B<text>
Italic I<text>
Link L<http://address.org >
Filename F<file>
email E<gt>name@address.comE<lt>

Man pages use two types of formatting (that I’ve ever seen). That are implemented by POD’s bold formatting and PODS filename-or-italic formatting:

Formatting type Used for
POD Bold formatting
  • Program names
  • Options
  • Manuals
POD Filename formatting
POD Italic formatting
  • Files
  • Keywords
  • Emphasis
  • Arguments
  • Occasionally manuals

Section formatting

There are three section tags that are used in pod2man (that I know of). The pod2man manual refers to these as Command Paragraphs and they are: header/category sections, item sub-sections, and “over” sub-sub-sections for additional descriptions of item sections. References to all of these are in the above example.

  • Header/Category sections are the block descriptors and their contents (like NAME, and DESCRIPTION…).
  • Item sections refer to options generally and often override =over sections for their list item.
  • Over sections refer to option descriptions.

General layout structure

Man pages follow a basic (though liberal) structure can be well represented by this:

NAME
The name of the command, followed by a one-line description of what it does.
SYNOPSIS
A representative invocation of how to run the command and options it has.
DESCRIPTION
A detailed description of the functioning of the command.
OPTIONS
A list of it's options (these are sometimes put in DESCRIPTION).
EXAMPLES
Some examples of common usage.
BUGS
List known bugs.
SEE ALSO
A list of referenced or related commands.
AUTHOR
   Specifying the contact information.
COPYRIGHT
   Specifying the copyright information.

The order of categories varies a bit from man page to man page. The first four categories are almost always the same, however the remaining vary a bit. The above layout is an mnemonic aggregate of category order of the man pages that the author has seen.

Convert pod format to manual page

Pod2man usage is basic:

pod2man dr-smile.pod > dr-smile.1

However, these additional options will probably be wanted to be added, plus compression:

pod2man --section=1 --center="dr-smile manual" --name="dr-smile" --release="1.0" dr-smile.pod | gzip > dr-smile.1.gz

Result

screenshot-manpage

Resources

xuserrun – Run a command on the currently-active user’s X.org server display

xuserrun is a bash script to run a command on the active X.org server display.  This is primarily necessary if from within another environment  (different user, console, cron, boot script…).  xuserrun gathers DISPLAY and user environmental variables via systemd and passes them on to be able to dothis. xuserrun is designed for use with only a single user X.org server display. Running it is basic:

xuserrun xclock -digital

Tto put out a notification:

xuserrun notify-send "Hello, Dave."

It’s available for Arch users in the AUR and is also downloadable on github.

Encode an Audio file to Video file

I wanted to convert an mp3 file to an mp4 or avi file because I wanted to be able to use it on my PS3 ( the PS3 has a basic audio player that doesn’t save position [ particularly troubling for large audiobooks ] ). This although is good for audio posting on youtube… I found a script by Jeremy Tharp that needed a bit of a tuneup (quoting parameters for file names with spaces, fixed exit status, better detection for the duration) and it works pretty good:

External monitor as Discrete

I use my laptop primarly at home with an external monitor as discrete, meaning that I have the laptop monitor turned off and I only use it. At times this is also called a dedicated monitor. GNOME can be set to disable the laptop monitor and enable the external but it wasn’t able to hotplug the monitor after I returned the laptop, and at times wouldn’t do so after resuming from sleep. Also in the proccess I discovered that the X.org server DPI setting wasn’t being done correctly and that GNOME’s text scaling needed to be adjusted. So I decided to do it in a script and it turned out to be pretty easy.

I wrote the basic script that toggles monitors depending if the external monitor is present, then it detects correct physical size dimension of the screen so the the correct DPI can be set. After this, I added a startup script (.desktop file), a pm-utils script to runafter resuming, and a udev script to detect andset the monitor when plugged in. The udev rule is generic but appears to be working for a lot of people, it relys on Kernel Mode setting (KMS) so doesn’t work for me wiht the catalyst driver, but every thing else works great. I put it on github for any who like to look at it.

The bash script cannot be used right away instead a couple bit will need to be directed:

 The package cannot be installed directly and be expected to work, some edits
 will need to be made.  First, in the resume script '80_discretemon' a username 
 will need to be defined; next, the monitor names will need to be defined as
 created by the driver in 'discretemon'.

Also, the monitors can be defined in xorg.conf but the fix for after resume from sleep, remains.

Section "Monitor"
  Identifier  "0-LVDS"
  Option      "VendorName" "ATI Proprietary Driver"
  Option      "ModelName"  "Acer Aspire Laptop Screen"
  Option      "DPMS" "true"
  Option      "TargetRefresh" "60"
  Option      "Position" "0 0"
  Option      "Rotate" "normal"
  Option      "Disable" "true"
  DisplaySize  344 194 # only works with xrandr disabled.

EndSection

Section "Monitor"
  Identifier  "0-DFP1"
  Option      "VendorName" "ATI Proprietary Driver"
  Option      "ModelName"  "Samsung SyncMaster SA350"
  Option      "DPMS" "true"
  Option      "PreferredMode" "1920x1080"
  Option      "TargetRefresh" "60"
  Option      "Position" "0 0"
  Option      "Rotate" "normal"
  Option      "Disable" "false"
  DisplaySize  476 268 # only works with xrandr disabled.
  Option      "DPI" "102 x 102"
EndSection

The Beauty of rsync and Backup Script

rsync is a command-line tool used to copy/clone files (“fast incremental file transfer“). It is a great, simple backup tool. The basic rsync command is this:

rsync -a src dest_dir

Where src is the original directory or file and dest_dir is the destination directory. Because rsync does incremental backups it only adds the file to the dest if it has been updated from the original backup.

rsync -axS src dest_dir

This is the command I use. This command can be used to backup just about anything! The options:

  • -a means archive mode which basically means to preserve the file “as is” (same permissions…)
  • -x means not to cross file systems boundaries
  • -S means to handle sparse files efficiently
  • -v option (verbose) can be used to print what rsync is doing

An important note about rsync: when src is a directory a trailing slash (/) tells rsync to copy the “contents” of the directory:

rsync -axS src_dir/ dest_dir
ls -1 dest_dir/
 file1
 file2

Without a trailing slash:

rsync -axS src_dir dest_dir
ls -1 dest_dir
 src_dir

rsync can also use file-lists containing paths of directories and files, to both include and exclude them for backup:

sudo rsync -axS --files-from="incl_file.txt" --exclude-from="excl_file.txt" src_dir dst_dir

src_dir will have to be specified and will have to be relative to paths in the file list:

cat incl_file.txt
Desktop/
rsync -axS --files-from="incl_file.txt" --exclude-from="excl_file.txt" /home/user/ dst_dir

rsync can also remove files from the dest_dir with the --delete option, so files that get added to the exclude file or taken out of the include file will removed from dest_dir.

rsync -axS --delete-excluded --files-from="incl_file.txt" --exclude-from="excl_file.txt" /home/user/ dst_dir

Backup Script

I use rsync to backup my system configurations and /home/ to make reinstalling easy. I created the script to remember the command to use, but to also easily add to the include and exclude files:

bcksysc i /etc/hostname 
 Added "/etc/hostname" to bcksysc-inc.txt include file.

Syntax:

bcksysc 
 bcksysc  - backup configurations
 i - add to the include list a file or folder
 e - add to the exclude list a file or folder
 c - create backup

Here’s the script all that needs to be done is to change the Parent Destination Directory (for backing up /home/ I copied the script to bckhome, changed the type to home and added /home/ to the include file):

So my destination directory looks like this:

ls -1 /run/media/todd/Backup/rsync/
 ...
 aspire_2012-08-31_sysc
 aspire_2012-08-31_home

Screencasting Done Easy (Desktop Recording)

I got to test out a good number of screencasting applications and I found a good one, and as usual the easiest was the best. I started with recordMyDesktop.

recordMyDesktop

recordMyDesktop is a basic program that works good. The GTK version has a simple UI that sets a border around the area to be recorded. I has sound recording too.

A minor thing but of note is that the window detection area is off when selecting a windows, but the reason I didn’t use recordMyDesktop was because I found the quality wasn’t that good. It could be because it uses .ogv format, or perhaps it had something to do with my system.

This is and example I did with recordMyDesktop and though it’s enlarged (OpenShot doesn’t have the ability to use the original size) the quality I wanted to be better.

Others

I tried Istanbul and a couple others all with about the same recording results. Istanbul hasn’t been developed in several years and though I got excited about xvidcap it hasn’t been developed in years either. xvidcap grabs screenshots and then concatenates them into a video. I got excited because xvidcap’s preview uses Imagemagick’s animate tool to preview the video and it was real nice. Unfortunately very little works in xvidcap anymore but taking the screenshots. To put them together I used:

fmpeg -i out%04d.xwd -r 15 -vcodec huffyuv test.avi

unfortunately the quality was no better than that of the others.

FFmpeg

The great command line tool to encode and decode video ffmpeg can also do screencasts and I read a lot of how people liked it (and I do too). To use it it’s real basic:

ffmpeg -f x11grab -s wxga -i :0.0 -sameq screencast.mpg

The quality isn’t quite what I want it to be, but I’ve seen other people have nice looking screencasts so I think it must be either my video card or my video driver.

This line can be amended some for better quality, performance, and add sound recording. Using the raw, lossless codecs for video and audio improves processor usage for better FPS recording:

ffmpeg -f x11grab -s wxga -i :0.0 -vcodec huffyuv -sameq -acodec pcm_s16le -f alsa -i pulse -ac 2 screencast.avi
  • -s and -i are for size and input. -s will give the dimensions and -i will define the co-ordinates. wxga is a definition of a video resolution standard (available ones are listed in man ffmpeg)
  • -r can be added to define the frame rate. Default is 25 and is good. Only reason really to change it is if frames are dropped during recording (marked with red).
  • -follow_mouse 100 can be added to follow mouse movements. 100 is the border in pixels that must be reached before the area is moved.

ffcast and FFmpeg

ffcast is a program that grabs and passes X.org server dimensions and co-rodinates to other programs. It has built-in support to pass these parameters for some programs including ffmpeg. So the command will now look like this:

ffcast -s ffmpeg -- -vcodec huffyuv -sameq -acodec pcm_s16le -f alsa -i pulse -ac 2 screencast.avi

ffcast’s -s option will prompt for the screen area and then pass the dimensions and co-orodinates to ffmpeg using --.

Now to make this easy, I put this in a bash script, it runs as such:

 screencast <a|f|m|w> - create screencasts (a)rea (f)ull-screen (m)ouse (w)indow

Here’s the bash script:

An example:

cVLC as Default Video Player



I love MPlayer. I’ve been using it for years. Whenever I needed to watch a video from my camera or downloaded something from YouTube it always did great. However, I revisited recently trying to play a DVD with MPlayer after having gone through a lengthy setup process a ways back and discovered MPlayer still cannot play DVD’s reliably. From the examples I tried it seemed as error-prone as before.

MPlayer always ran dependably and with almost no resources, videos would pop rightup. Learning to use the keyboard to navigate Mplayer was likely having one big remote control. However, I came to the decision that I cannot deal with the quirks of MPlayer anymore (there is good work on the mplayer2 project that is trying to fix a lot of the internal plumbing problems of MPlayer) but I needed something more-reliable. So when I decided just to use VLC, I accidentally learned about clvc.

I don’t normally use VLC because I use GNOME. Having MPlayer open up immediately was a big plus, but with clvc (which is part of the VLC package) videos open just like they did with MPlayer. And the playback quality is good. To play a DVD:

cvlc dvd://

The big thing is I’m going to have to learn all the key mappings again for cvlc, so a made a reference sheet:

Key Mappings

Desktop Recognition

To have clvc be recognized by the desktop a .desktop needs to be created:

and put in ~/.local/share/applications.

sudo update-desktop-database -q

Warning: I had to put it in /usr/share/applications/ for GNOME 3.6 to be able to recognize it in Default Applications and Removable media. This is likely a bug.

To have all known video types that VLC knows and define them to cVLC as the default application do:

xdg-mime default cvlc.desktop $(grep -oP 'video.*?;' /usr/share/applications/vlc.desktop | tr ';\n' ' ')

Load on DVD Insertion

I have yet to find out how to do this. This probably isn’t the correct way to do it, but it should work (note: my install is busted a bit right now so unable to test). Put in /usr/share/applications/clvc-dvd.desktop:)

[Desktop Entry]
Type=Application
Name=cVLC
GenericName=Media Player
GenericName[ca]=Reproductor multimèdia
GenericName[de]=Medienwiedergabe
GenericName[fr]=Lecteur multimédia
GenericName[it]=Lettore multimediale
GenericName[ja]=メディアプレーヤー
X-GNOME-FullName=Command Line VLC
Comment=Play movies and songs
Icon=vlc
TryExec=cvlc dvd://
Exec=cvlc dvd:// %U
Terminal=false
Type=Application
Categories=AudioVideo;Player;Recorder;
MimeType=video/dv;video/mpeg;video/x-mpeg;video/msvideo;video/quicktime;video/x-anim;video/x-avi;video/x-ms-asf;video/x-ms-wmv;video/x-msvideo;video/x-nsv;video/x-flc;video/x-fli;video/x-flv;video/vnd.rn-realvideo;video/mp4;video/mp4v-es;video/mp2t;application/ogg;application/x-ogg;video/x-ogm+ogg;audio/x-vorbis+ogg;application/x-matroska;audio/x-matroska;video/x-matroska;video/webm;audio/webm;audio/x-mp3;audio/x-mpeg;audio/mpeg;audio/x-wav;audio/x-mpegurl;audio/x-scpls;audio/x-m4a;audio/x-ms-asf;audio/x-ms-asx;audio/x-ms-wax;application/vnd.rn-realmedia;audio/x-real-audio;audio/x-pn-realaudio;application/x-flac;audio/x-flac;application/x-shockwave-flash;misc/ultravox;audio/vnd.rn-realaudio;audio/x-pn-aiff;audio/x-pn-au;audio/x-pn-wav;audio/x-pn-windows-acm;image/vnd.rn-realpix;audio/x-pn-realaudio-plugin;application/x-extension-mp4;audio/mp4;audio/amr;audio/amr-wb;x-content/video-vcd;x-content/video-svcd;x-content/video-dvd;x-content/audio-cdda;x-content/audio-player;application/xspf+xml;x-scheme-handler/mms;x-scheme-handler/rtmp;x-scheme-handler/rtsp;
X-KDE-Protocols=ftp,http,https,mms,rtmp,rtsp,sftp,smb
Keywords=Player;Capture;DVD;Audio;Video;Server;Broadcast;
NoDisplay=True

and then point to it in Removable Media > DVD.

YouTube videos

VLC has it’s own parser to be able to extract URL’s from YouTube so running is all that is needed to get the job done:

cvlc "http://www.youtube.com/..."
Follow

Get every new post delivered to your Inbox.

Join 52 other followers