Entries Tagged 'Entry Level Programming' ↓

Using vim In OS X — A Text Editor Tutorial For Beginners

Here is a quick tutorial for people unfamiliar with text editors in UNIX.  If you are just getting started with the Terminal in OS X, you probably need to create your .bash_profile and such, so that you can keep your settings upon logging out of the shell.  I will show you how to create this file in a text editor called vim, which stands for vi IMproved.

Vim is  a very powerful text editor, and if you have any experience in UNIX at all, you probably were shown pico, which is easier to use at first due to some of the commands being shown at the bottom of the screen as you work in pico’s buffer.  In pico, there is no separation from command or input modes, also making it a bit less confusing.  The buffer simply means what is shown on your screen, not yet written to disk.  While a GUI text editor such as Microsoft Word or Apple’s Pages do not tell you that you are working in a buffer input mode, technically it is the same thing.  If they crash, you lose what you changed if it was not saved prior to the crash.

Vim is a little obscure, yet extremely functional.  You start vim by typing vi or vim at the bash prompt in Terminal.  Terminal is located in your ~/Applications/Utilities folder by default on OS X.  When it opens, you are by default in command mode.  Vim shows you this startup screen, which has a bunch of tildes (~) on the left-hand side, and some version information in the center:

vim Startup Screen OS X Snow Leopard

vim Startup Screen OS X Snow Leopard

If you type something in, the startup screen goes away and the first tilde also disappears.  The tilde characters simply clarify lines in the buffer.  They will not print, they are just there showing you where the next lines are.  Once started, you are by default in Vim’s command mode.  If you type vim testfile.txt at the bash prompt, vim will open the file testfile.txt in whichever directory you are currently in.  If testfile.txt doesn’t exist, vim will create the file and open into the edit buffer for you, skipping the welcome screen:

Vim Buffer

Vim Buffer

As shown in the screenshot (click to enlarge it, as with all screenshots on this blog), the buffer is in INSERT mode.  By default, no matter what file you open or create, vim starts in command mode.  It doesn’t ever show —COMMAND— at the bottom of the screen.  Vim lets you know you aren’t in command mode by telling you that you are in INSERT mode.

To get back into command mode, which is where you will end up saving files to disk, changing the contents of vim’s 26 named buffers (consider them like the clipboard in a GUI text editor), moving around the screen, deleting lines, etc.  Anything you want to do with the file besides type in text will generally be done in command mode. Let’s save this file now, so you can see how it works to get in and out of command mode.

First, hit the ESC key.  On almost every keyboard ever, this will be the key  at the very top-left corner of the keyboard.  You should no longer see –INSERT– at the bottom of the Terminal window.  Now type the following command, without the quotes:  ”:w testfile.txt“.  See screenshot below:

Write Command

Write Command

File Saved

File Saved

You can see at the bottom of the Terminal window that the write command was successful.  You also see that three lines were written containing a total of 138 characters.  You can verify the file was written by typing (again, without quotes) “:q“, and hit enter.  This quits vim.  At the bash prompt, type “ls” and hit enter.  You should see your new file in the list of the directory.  To remove (delete) the file, type “rm testfile.txt” and hit enter.

Now to create your .bash_profile, so you can save certain settings.  When Terminal starts, it will read this file to load alias information, screen settings and such, if they are explained in this file.  As you become more familiar with the Terminal and start to have preferences for certain things, i.e., showing hidden files when you get a list of a directory, you may want to create an alias for the ls command so it shows them by “default” because of your .bash_profile.

First, navigate to your home directory if you are not there now.  You do this by typing the command “cd ~” at the bash prompt and hitting enter.  Terminal will show your computer name, followed by your present working directory, and yourusername$, which is the bash prompt.  You should see something like this:

Bash Prompt

Bash Prompt

Create your empty .bash_profile by typing “vim .bash_profile”  and hitting enter.  We will create a simple alias and save the profile.  Then we will quit Terminal, restart it and verify the alias still works.

Alias in Profile

Alias in Profile

By default, the alias wouldn’t work again after you quit Terminal.  If it is in your profile, it will work when you open a new Terminal, such is the point of having a profile.  To type in what I show above, press the letter “i on your keyboard to put you in insert mode.  Then type the following exactly:

dirA=”ls -lia”

Hit the ESC key, and type: “:w” to save the file.  Now type “:q” to exit vim and return to the bash prompt.  You can verify the file was written and it’s contents by typing “cat .bash_profile” and hitting enter:

Verify File Was Written

Verify File Was Written

Now quit and re-open Terminal.  You should now be able to get a detailed list of your directory, showing hidden files, by typing dirA and hitting enter:

Working dirA Alias

Working dirA Alias

Next time I will show you how to navigate through text, delete lines and add or retrieve lines to and from the named buffers.  If there are certain things you would like to learn about Terminal or vim, please leave comments below.  Please also let me know if any of this could be better clarified, as I check my comments often and will respond promptly.  Thanks for reading!

Simple Introduction To Apple’s Unix Using Terminal on OS X

This post is for someone interested in learning UNIX on Mac OS X Snow Leopard, but lacks experience using a command line, or has only C:\ prompt knowledge. I will show you, simply, how to map a DOS command to a UNIX command, and to save your profile so the alias name you create is persistent.  If you are a shell scripting genius, this post is something you are encouraged to comment on and expand upon, as learning scripts and commands for OS X can be input many different ways.  It never hurts to have different suggestions.  I will try to make this at the minimum a weekly edition to my blog, and expand each article further than the next.

Let’s get started.  First, open Terminal from your /username/Applications/Utilities folder. This is the command line utility for UNIX, by default it is Bash (Bourne Again Shell.)

You will see your entire present working directory prior to the $ prompt.  The tilde (~) represents /Users/your home folder.  The standard format is computer name: pwd. You can type the simple command pwd and the shell will respond with the folder you are currently accessing.

Default Shell

Default Shell

I like a cleaner shell screen, so I type bash, and hit enter.  This opens a new bash shell, on top of the existing shell.  Next, the command clear deletes the current Terminal text and starts with a fresh bash prompt.  You end up with a Terminal screen that looks like this:

Clean Bash Prompt

Clean Bash Prompt

Here, we can start by showing the man (short for manual;  built in UNIX help pages) page for the command ls (list), which is basically the dir command in DOS.  Simply type man ls at the command line and hit enter.  You can move down the pages line by line using the down arrow key on your keyboard.  To exit the manual, simply type q.

The man page shows you the various options you can use along with the ls command, for example, ls -lia. This command is particularly useful when searching a directory for hidden files (they start with a period), as Finder refuses to display them by default:

Directory Listing With Hidden Files

Directory Listing With Hidden Files

Now, if you are used to DOS, here is a helpful way to stick with the commands you know, yet tell Terminal to run UNIX commands.  You create an alias, basically a command name you make up mapped to a system command. If you want to use options such as -lia, you need to enclose the command in double quotes.  The syntax of the alias command in bash is alias newCmdName=systemCmdName. The command in DOS for a directory listing like the one above is dir /a.  So we can map dirA to ls -lia like so:  alias dirA=”ls -lia”

Note that UNIX is also case-sensitive, for files as well as commands:

Alias DOS dir to UNIX ls

Alias DOS dir to UNIX ls

You can simplify entering commands by using the up arrow to cycle through previously typed in commands.  The history command, saves the commands you enter by line number, which you can view by typing history.  To execute the line number, type an exclamation point followed by the line number:

History Command

History Command

Now, to save this new alias dirA you created into your bash profile.  This way it doesn’t get erased when we kill the shell and quit Terminal.  You may not have a profile set up for bash in your home directory, so what you need to do is create one.  Bash will look for .bash_profile first, so it is best to name any profile you want to use on a regular basis with this name.  It is a hidden file, so you need to have the period in the file name.  For simplicity, you can create a simple text file in your favorite text editor, name it bash_profile (without an extension)and save it in your home directory.  Write the exact command on one line like so: alias dirA=”ls-lia”

Save the file, and then change the name in Terminal.  Use the command mv to change the name so it begins with a period.  Simply type mv bash_profile .bash_profile and press enter:

MV Command

MV Command

MV Complete

MV Complete

That did two things:  one, you moved the file;  two, you learned that mv is the same command as the DOS command move.  You can follow the procedure above to make an alias for the mv command, and place it on the next line in your .bash_profile, so it remains permanent for your login shell.  If you like running an “non-login” shell as I do above, you can use the command cp to copy your .bash_profile to .bashrc like so:   cp .bash_profile .bashrc

Here is a short list of a few DOS commands, along with their respective UNIX bash counterparts (pdf of list here):

DOS COMMAND

UNIX COMMAND

DIR

LS

MOVE

MV

COPY

CP

FIND

GREP, FGREP, EGREP

MEM

TOP (virtual memory use VM_STAT)

*use ctrl-c to end TOP command*

CD

CD (PWD to display current directory)

DEL

RM

That’s all for this quick lesson.  Stop by next week for more DOS examples in UNIX, and (hopefully) working with vi, UNIX’s text editor.

myBlogEdit

So I decided to go all out and make it easier for me to blog, even if I’m stuck with no intraweb, somewhere where the dredded “E” is in the upper left hand corner of my iPhone. I have been looking into things like this here OS X app called….you guessed it, myBlogEdit.

Set up is pretty simple so far.

  1. Download and install.
  2. Command-Comma for Preferences. Highlighted in blue, you will see the XML-RPC thing. This is important to also set up in settings on your WordPress Dashboard.
  3. Fill in your site info, and you’re pretty much done.

I’m diggin’ that this app has a bunch of HTML tags for your clicking approval, as well as an empty set that does a nice drop-down list. It has taken me a while to get this first post out with the program, but it’s because I’m taking screen shots and telling you all about it as I go. Suffice it to say, it’s pretty slick so far.

Click Thumbnail For Viewable Image

Click Thumbnail For Viewable Image

Unfortunately, I cannot seem to change the CSS for the preview, and I’m going to write the developer and ask him why that is. I cannot cut and paste my CSS into the preference, nor can I drop a CSS file on it. The only option you have is blank, or “default” (program author supplied). I do note that you can type in the box, however I do not consider typing all of your CSS out all over again a viable option. You can choose cut copy and paste from the file menu, but paste seems to be the only one lacking function. Bummer. That will put the brakes on me using this app full time for sure.

Upload is pretty straight forward, and it does look like the preview version. I have to say that you should note the width of an image you want to upload prior to you accepting the upload. It seems that thumbnails are automagically generated for you after you make images in excess of 150px. Pretty neat.

Suffice it to say, though, that you really do need to love editing your blogs in HTML form in order to like this application.  If you are used to the WSYWIYG web version that WordPress gives you to use, than you probably aren’t going to be a huge fan of changing strictly to HTML.  For those of us out there that click the HTML tab on that web interface, this little program is pretty good.

So one thing I learned the hard way, do not delete the post from WordPress, or it won’t let you publish it again. The error I got was invalid post ID, found in the xml data in the log. myBlogEdit just gives a cryptic popup advising you to have XML-RPC enabled, and check the error log as well. Do yourself a favor and check the error log if it worked before and suddenly doesn’t. Well, that’s it for now, until I get more used to writing with this application. Give me about a month and I’ll come back and tell you how it’s been going.