Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How would I go about creating a command that is like cd, but you do not need to escape spaces? (Assume there are never more than one space in a row.)

Example usage:

cds some directory

vs.

cd some\ directory

share|improve this question
    
You can enclose the path in quotes to avoid per-character escapes, but I don't think you can alter such a fundamental behavior as space-character-interpretation without extraordinary effort. –  Amazed Mar 14 '12 at 17:52
    
You could also rely on tab completion completely for this. Add the following to your .bashrc or .bash_profile to make tab cycle through options instead of listing them: bind '"\t":menu-complete' –  Daniel Beck Mar 14 '12 at 18:13
add comment

2 Answers

up vote 7 down vote accepted

If you're using bash, put this in your ~/.bashrc:

cds() { cd "$*"; }

Another solution would be to type the first letters of the directory name, then hit the Tab key and let the shell complete the name for you.

share|improve this answer
1  
This is the best answer to the question as asked, but it won't help with directory names that contain any funny characters besides single spaces (e.g. ampersands, semicolons, ...). Really, the OP would be better off learning to quote/escape/etc properly; kluges like this will just enable him to keep bad habits. (+1 for the answer anyway.) –  Gordon Davisson Mar 14 '12 at 19:06
add comment

This is just the way the shell works. It separates arguments by spaces. If you created such a command, you'd be teaching yourself not to escape spaces properly, which might result in problems sooner or later.


If you just want to cd to a directory without ever needing to escape, start the command with a quote:

$ cd 's … now press Tab ome directory'/

As you can see, no escaping used, and your quote will be auto-closed too.

share|improve this answer
    
If you autocomplete, it'll escape the space if necessary anyway. –  Daniel Beck Mar 14 '12 at 18:04
    
Yeah, sure. It depends though on what the OP really wants to achieve (and I personally think quoting is more beautiful) –  slhck Mar 14 '12 at 18:09
    
Generally I agree, but I have a directory in an environment variable that I cd often into. But it has spaces in its path, so I have to double quote it. –  Thomas Eding Mar 14 '12 at 18:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.