Blog Archives

Get Current Directory in Objective-C

Since Mac OS X is a unix based system, you can always use “getcwd” from the standard C library, but in general if you want to stick within the context of Objective-C/Cocoa, see the examples below.

Here’s one snippet you will probably Google quickly, but ultimately not the solution I chose.

(original code from stackoverflow see reference below)

NSFileManager *filemgr;
NSString *currentpath;

filemgr = [[NSFileManager alloc] init];

currentpath = [filemgr currentDirectoryPath];


Using the “bundle path” rather than “executable path” turned out to work much better in my instance:

NSString *currentpath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingPathExtension] stringByDeletingLastPathcomponent];
NSString *fileName = [NSString stringWithFormat:@"%@/%@/",currentpath,@"filename.pdf"];

References,, “NSFileManager”,, “Working with Directories in Objective c”,
Macrumors Forums,
stackoverflow, “Find Parent Directory of a path”,


Concatenate string in Objective-C

Unfortunately, this is not as straight forward as it seems it should be. This really comes down to roughly two approaches in my opinion:

stringByAppendingString approach:

NSString *robot= @"Ronnie";
NSString *robotname = [robot stringByAppendingString:@" is the name of a robot."];

strigWithFormat approach:

NSString *robot= @"Ronnie";
NSString *robotname= @" is the name of the a robot.";
NSString *robotknowledge = @" knows eight languages.";
[NSString stringWithFormat:@"%@/%@/%@", robot, robotname, robot, robotknowledge];

StackOverflow,, “Learn Objective-C”,

Mac Sudo Password

Note: “Sudo” is short for “Substitute User Do” or “Superuser Do”

This may come as a shock if you’re new to Mac and ever used a *nix system before.. but the sudo password, is your password! If you’re administrator anyway, if not, ask your *rents, instructor or local admin to help you out. 😉

/etc/sudoers contains the permissions for who can use it, and once you have it, it is very powerful indeed.

If for any reason you need to have access to the full root account, this is actually not enabled on a Mac by default, for good reason.

To Enable Root User (see Apple KB below for reference):
1) From the Apple menu choose System Preferences….
2) From the View menu choose Accounts.
3) Click on the lock and authenticate with an administrator account.
4) Click Login Options….
5) Click the “Edit…” or “Join…” button at the bottom right.
6) Click the “Open Directory Utility…” button.
7) Click the lock in the Directory Utility window.
8) Enter an administrator account name and password, then click OK.
9) Choose Enable Root User from the Edit menu.
10) Enter the root password you wish to use in both the Password and Verify fields, then click OK.

Apple KBase,
Wikipedia (Sudo),
Wikipedia (SU),

vi keyboard shortcuts Quick Reference

Never know when those quick reference bookmarks might point to dead links, usually when you need it most!

For this reason, I’ve consolidated a quick list from the link below and removed ads and unwanted formatting for a little cleaner html table (no offense keyxl) and easier copying by you. For your future reference, and mine. 😉

Shortcut Command
i Inserts text to the left of the cursor.
I Inserts text at the beginning of the line, no matter where the cursor is positioned on the current line.

a Begins inserting after the character (append) on which the cursor is positioned.
A Begins inserting at the end of the current line, no matter where the cursor is positioned on that line.

o Begins inserting text on a new, empty line that is opened for you, below the current line. This is the only command that will allow you to insert text BELOW the LAST line of the file.
O Begins inserting text on a new, empty line that is opened for you, above the current line. This is the only command that will allow you to insert text ABOVE the FIRST line of the file.

Deleting,copying and changing
d Delete text. (see explanation above)
y Copy text (that is, yank it into a holding area for later use). (see explanation above)
c Change text from one thing to another, which you will type. (see explanation above)
! Filter text through a program.
< Shift a region of text to the left.
> Shift a region of text to the right.

Single Key Movements
h Move cursor to the left one character.
l Move cursor to the right one character.
j Move cursor down one line.
k Move cursor up one line.
^ Move cursor to the beginning of the line.
$ Move cursor to the end of the current line.
1G Move cursor to the first line of your document. Other numbers will move to the line specified by number (ex. 50G goes to the 50th line).
G Move cursor to the last line of your file.
CTRL U Move cursor up in file 12 lines. Hold down the key marked CTRL (stands for control) and type U. CTRL is like another shift key.
CTRL D Move cursor down in file 15 lines.
w Move cursor forward to the next word, stopping at punctuation.
W Move cursor forward to the next word, ignoring punctuation.
e Move cursor forward to the end of the word, stopping at punctuation.
E Move cursor forward to the end of the word, ignores punctuation.
b Move cursor backwards to the previous word, stopping at punctuation.
B Move cursor backwards to the previous word, ignores punctuation.
H Move cursor to the top line of the screen, (as opposed to the top of the document which may not be the same place).
M Move cursor to the middle of the screen.
L Move cursor to the last line on the screen.
% Move cursor to the matching parenthesis, bracket or brace. Great for debugging programs.
( Move cursor to the beginning of the previous sentence (where a punctuation mark and two spaces define a sentence).
) Move cursor to the beginning of the next sentence.
{ Move cursor to the beginning of the current paragraph.
} Move cursor to the beginning of the next paragraph.
; Repeat the last f or F command (see below).

Almost Single Key Movements
Move cursor to a previously marked location in the file. (ex. ma marks the location with the letter a, so a (apostrophe a) moves back to that location).
f Find the character corresponding to the next keystroke typed. Move the cursor to the next occurrence of that character (on the current line only).
F Same as f but movement is backwards.

x Delete character(s) to the right of the cursor, starting with the one beneath it.
r Replace the character under the cursor with the next character you type. This can be a very useful command. If you wanted to split up a line between two words, you might put the cursor on the blank space before the word you would like to go on the next line and type r . This would replace the space between the words with a carriage return and put the rest of the line onto a new line.
J Join lines; the opposite of the line splitting operation above. This will join the current line with the next line in your file. Also very useful.
R Replace lines; puts you in INSERT mode but types over the characters that are already on the current line.
p Paste line(s) you deleted (or yanked) back into the file. This is an excellent command if you want to move a few lines somewhere else in your file. Just type 3dd to delete three lines, for example, and then move to where you want those lines to be and type p to paste the lines back into your file below the cursor.
. The period . command repeats the last text modification command, whatever it may have been (insert, deletion, etc).
:r filename RETURN Read a file into the current file being edited. The file be added gets placed below the current cursor position. Please note the colon : before the r in this command.
CTRL L Redraw the screen. If somebody writes to you while you are in the middle of vi and junk appears all over your screen, dont panic, it did not hurt your file, but you will have to hold down the CTRL key and type L to clean it up (CTRL L).
d$ Delete (including the current character), to the end of the line.
d^ Delete (excluding the current character), to the beginning of the line.
dW Delete a word(s), ignoring punctuation.
de Delete to the end of next word.
dd Delete a line(s).
dG Delete from the current line to the end of the document. CAREFUL: Slightly dangerous.
dH Delete from the current line to the line shown at the top of the screen.

Search and Replace
/the Finds the next occurence of the. This will also find their, them, another, etc.
?the Finds the previous occurence of the.
n Repeats the last search command. Finds the Next occurence.
d/the Deletes until the next occurence of the. This is to demonstrate how the delete prefix can be used with any cursor movement command.
:g/oldword/s//newword/gc This will find all occurences of oldword and replace them with newword. The optional c at the end of the command tells vi that you would like to confirm each change. Vi will want you to type in y to make the change or n to skip that replacement. Great for spelling fixes.

ESC :wq RETURN Save and exit VI
ESC :q! RETURN Exit WITHOUT saving changes