How to store text into keywords.. (I think thats how to say it)


#1

Ok so say you have a bit of text you need to use frequently, but its kind of hard to remember and has so many odd characters its a slow one for you to type…

Take this as an example: print(’\33[f\33[2J’)

Since that is a “clear screen” command for python, it would be really nice to be able to just type in an easy key phrase like maybe “cls” or “clear” or something that atom would recognize and automatically replace that key phrase with the actual command. I’m sure I could just create my own python module and store tat command in.a function… but I’d rather not have to import it nearly everytime I wanted to write a script that involves the console.

Is there a way to do this in atom? If not is there a package for it?

Thanks


#2

What you’re looking for is the snippets functionality.


#3

Funny enough, I just found it right before coming back to check.

Unfortunately I dont know how to create a new entry… I guess you have to write it in coffeescript? Do you know where I’d find a decent tutorial on it?


#4

What part of the documentation is leaving you confused?


#5

Well I think I have it almost… I am trying to create a snippet for the above mentioned clear screen command…

Here is what I have so far:

‘.source.python’:
‘Clear Screen’:
‘prefix’: ‘cls’
‘body’: ‘print(’\33[f\33[2J’)$1’

Now I am getting an error on that saying unmatched outdent…

I think the problem though, is the quotation marks in that clear screen command. They seem to be giving messing with the syntax… maybe I need to insert some escape characters in front if them? I just sont know the coffeescript escape characters to try it.


#6

Well I had the indents aligned… the site automatically took them out though. So pretty sire thats not the issue lol.


#7

This is driving me a bit nuts… I’m on mobile… I do not see an edit option on my posts… which is why I am posting another reply rather than editting. Where is the edit button? Lol


#8

You can wrap your code in tags by highlighting it and pressing the </> button above the editor. Then the spacing will be preserved.

Assuming your indentation is appropriate ('.source.python': should be flush with the left side), then what I see is the single quotes in your print() statement. Since you’re using single quotes to define the variable, the single quotes inside are going to break the parsing of the string. Observe:

    'body': 'print('\33[f\33[2J')$1'

The easiest way to handle this is to use double quotes, like so:

    'body': "print('\33[f\33[2J')$1"

Now, if you passed that through, you would see that the backslashes get dropped. That’s because they’re escape characters. You need to escape them.

    'body': "print('\\33[f\\33[2J')$1"

In terms of cosmetics, you don’t require the $1, since the cursor will be at the end of the snippet once you confirm. It’s also valid CSON to have the key be unquoted as long as it’s just one word, so I prefer to do that for the sake of readability. Your finished snippet would be:

'.source.python':
  'Clear Screen':
    prefix: 'cls'
    body: "print('\\33[f\\33[2J')"

#9

Awesome! Thank you for your help! Seems simple enough.


#10

CSON is a very small hump to get over and then it’s great to work with. Same for JSON, but it cares more about punctuation (CSON mostly cares about indentation unless you’re doing something fancy).