1

I've been searching for ways to make my code more concise, so I want to find a way to automatically replace an identifier with an abbrevation of that identifier (whenever I type that identifier). It would be similar to the "autocorrect" feature that is found in some word processors.

Example:

var con = console.log;
//from this point on, whenever I type "console.log" as a variable name, I want the text to be automatically replaced with "con"
3
  • I'd need some way to avoid replacing matches of con inside string literals, like this: "con". I'd most likely need to use a Javascript parser (to distinguish between identifiers and string literals). Commented Dec 18, 2012 at 6:30
  • Some IDEs have automatic code replacement features, don't they? Commented Dec 18, 2012 at 6:46
  • As a workaround, I could simply prevent a function from being called in a certain section of code, and then allow it to be called again after a certain point - it's a clumsy workaround, but it might work in a very limited number of scenarios. Commented Dec 18, 2012 at 7:12

2 Answers 2

2

use

var con = function(str){console.log(str);};
con('hello world'); //=> hello world

It looks like you want to be able to use a kind of intellisense or autocomplete. Your editor should provide that. You can try Komodo Edit or Visual Studio (2012). In Komodo Edit the above code example leads to autocompletion (it shows con if you typed 'co').

Sign up to request clarification or add additional context in comments.

4 Comments

I'm trying to automatically replace one identifier with another identifier (as described in the original post).
I hope the wording of my question is clearer now - sorry for the confusion.
You may be interested in some obfuscator then, like googles closure compiler: developers.google.com/closure/compiler
What I'm really trying to do is automatically replace variable names with abbreviations as I type (sort of like an AutoCorrect feature). I have clarified the question's wording again - hopefully it is less vague now.
0

Write your code long hand, and run it through an obfuscator, such as one of the packages previously described

3 Comments

What I'm trying to do specifically is automatically replace variable names with abbreviations as I type - I don't think an obfuscator would do that. Also, an obfuscator would make code difficult to read, and I want it to remain human-readable.
To what end, @AndersonGreen?
I'm basically trying to find an "autocorrect" feature that encourages the use of abbreviations instead of full variable names - it's easier to learn abbreviations when they're being automatically suggested to you as you type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.