Showing posts with label aliases. Show all posts
Showing posts with label aliases. Show all posts

December 7, 2014

Opening files in Notepad++ from Cygwin

Notepad++ is an improved text editor instead of standard Vim in the command line, and especially improves upon using classic Notepad. It is superior, even if only for the syntax highlighting making things more readable. This is an alias function that seeks to implement a GUI-based text editor into Cygwin shell, and to run it as a daemon in the background, instead of seizing control over the terminal window for as long as the editor is open.

np ()
{
/cygdrive/c/Program\ Files/Notepad++/notepad++.exe `cygpath.exe -w "$*"` &
}

Note that it needs to be a function, and not a vanilla alias, because aliases don't allow interpolated arguments. What this function does is open the executable Notepad++ file, then takes a path argument that has been converted from a Cygwin path to a Windows path. $* stores the aformentioned arguments that were entered on the command line, and the & lets the process run in the background. This lets you use Notepad++ from Cygwin without having the shell wait for an exit code. See here for bash scripting info.

Reload the shell:
. ~/.bashrc

Now we can open any text file by passing the filename as an argument along with our new alias function. Instead of using vim or another editor we can simply use Notepad++ like so:
np ~/mycooltextfile.txt

You can even create text files this way - by specifying a filename that doesn't exist yet, it will be created for you!

December 5, 2014

Cygwin shell setup and bash-style tweaks

We have already seen how Cygwin can bring some great quality-of-life improvements as an alternative to using the native Windows terminal. Cygwin closely emulates a Unix shell, but there are still some differences.

The Home directory for Cygwin shell is probably not where you might think it should be. It is actually located within the Cygwin install directory (i.e. C:\Program Files\cygwin\home\User):

cd ~
pwd
/home/User

Since this isn't technically your Windows user's home directory, it will be empty, aside from config files that are used to emulate the Unix environment. As an example, you would navigate to C:\Users\User\My Documents like below (note how spaces need to be escaped by using backslashes):
cd /cygdrive/c/Users/User/My\ Documents/

Packages and programs are also not installed via command line. Instead, as explained in an earlier post, you would need to re-run the Cygwin setup.exe, which doesn't actually reinstall Cygwin from scratch, but just rebuilds it from existing packages, as well as any new ones you add during this subsequent install.

Aliases are a neat way to improve your workflow, and setting some up in Cygwin is really no different than Unix. Let's add some aliases (all config files are in the Cygwin home directory):
vi ~/.bashrc

I usually uncomment the ll, ls, la aliases, and add a few, like this function to use Notepad++ as a GUI text editor instead of using Vim in the terminal. Vim isn't great in Cygwin (feels more buggy than its Unix counterparts), so a solid GUI alternative before resorting to vanilla Notepad, is the nicer Notepad++. Anyways, you can also set up simple navigational aliases if symbolic links aren't your thing:
alias pro='cd /cygdrive/d/Projects'

You can also turn off the annoying bell beep sound the shell makes when you enter something incorrect, or try to autocomplete something that doesn't exist. In your ~/.inputrc file, uncomment or add:
set bell-style none

Reload the shell, no need to close and reopen to pull in the changes:
// These two commands are identical:
source ~/.bashrc
. ~/.bashrc

And now you can enjoy your newly added shortcuts!

September 2, 2014

Getting started with a Raspberry Pi: Some basics

These are some basic steps and resources for getting a Raspberry Pi up and running in very little time, while ironing out some annoyances.

You will need a SD/SDHC card (2-8 Gb is plenty of space) as well as a computer with a card reader, and a Raspberry Pi (duh).
  1. Prepare SD card: If running Windows or Mac, download the SD Association's SD Formatter tool. This tool is recommended by almost any RPi guide worth their salt, and is very lightweight and fast. Excerpts from the official RPi guide:
    • Windows
      1. Install and run the [SD Formatter] on your machine
      2. Set "FORMAT SIZE ADJUSTMENT" option to "On" in the "Options" menu
      3. Check that the SD card you inserted matches the one selected by the Tool
      4. Click the "Format" button
    • Mac
      1. Install and run the [SD Formatter] on your machine
      2. Select "Overwrite Format"
      3. Check that the SD card you inserted matches the one selected by the Tool
      4. Click the "Format" button
    • Linux
      1. We recommend using gparted (or the command line version parted)
      2. Format the entire disk as FAT
  2. Download the latest NOOBS image, or pick your preferred distro.
  3. Unzip the Raspberry Pi image, and copy those files to the formatted SD card.
  4. Insert the SD card into the Pi and connect networking, USB keyboard, HDMI TV/monitor, power etc.
  5. NOOBS will boot up, so select your desired operating system, and hit I to install.
  6. Go grab a drink, because this takes like 10 mins.
  7. Change font size: When it is finally done, you'll need a magnifying glass to see anything on the screen, which sucks. Firstly, you'll be greeted by the console-setup tool. Slog your way to the update command with your magnifying glass; when this is done, we will have the option to change font size.
  8. You may need to log in to your Raspberry Pi at this point, the default username/password is pi/raspberry.
  9. Reconfigure the console-setup tool with:
    sudo dpkg-reconfigure console-setup

  10. Follow the steps, and select a larger font (I personally like 16x32 on my 42" TV), but let's face it, we're gonna be SSH'ing into this puppy most of the time, so this is really just a backup solution, and that we won't get annoyed when we DO need to connect it to a TV.
  11. sudo reboot
  12. Change the default user's password: As in Unix systems, done via the passwd command, or create a new user altogether for better security.



In a following article, I will detail setting up SSH access, and use a Raspberry Pi as an example of how to do this. A few final notes, once you can access your Pi via SSH:

Set up some useful aliases: I usually just uncomment the ll, la and l aliases in the ~/.bashrc file. Reload your terminal profile to pull those aliases in:
su - pi //where pi is my username; substitute if you changed yours.

Proper shutdown: Don't be a dumbass and systematically wreck your Pi by just pulling the plug to turn it off. Power it down properly, either while SSH'ed in, or directly via your plugged-in keyboard:
sudo shutdown -h now

April 28, 2014

Bash shell: Incremental history searching

From using Git, Linux/Ubuntu apt-repositories, or general Bash folder navigation, you may have noticed that pushing the tab button halfway through typing an item/folder name will autocomplete it if it exists. Similarly, we can build an autocomplete feature for user commands that have been entered in the past! They are obviously stored in history somewhere, because you can already chronologically "scroll" through every single command you've issued, or just see it all by entering history. It would be nice to have a quick way to access something specific, complex or lengthy, especially if you already know how it starts.

All you need to do to find a previous command, is to start entering the first few letters of it, then you can navigate all commands you previously issued that started with your entered phrase, by using the up/down arrows! Here's how you set it up:

In terminal, open or create:
gedit  ~/.inputrc

Copy/paste the following there, and save (you may also have to close and reopen a terminal, or reload this way):
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char

And done! Now, for example, if I am now looking for the command...
sudo apt-get update

...all I need to do is enter the first few letters, something like...
sud

...and scrolling up/down through my now-selective bash history will allow me to easily locate the command in its entirety for use again!

Adapted from: Ubuntu User Community