almost 3 years ago

To writing Markdown in Sublime Text 3, two plugins are needed to:

  1. Enable Markdown syntax highlighting
  2. Convert Markdown document to HTML and preview it in a web browser

To accomplish these, install these two plugins:

  1. Markdown Editing: This plugin enables syntax highlighting for standard Markdown, Github Flavoured Markdown(GFM) and MultiMarkdown

  2. Markdown Preview: as the name suggests, it converts your MD file to HTML and opens it in your system default browser

  3. LiveReload(optional): This is a standalone plugin (i.e. has nothing to do with Sublime Text 3) that automatically refreshes a webpage if its source (HTML,CSS,JS, etc.) has changed. With this plugin, you don't have to click on refresh or F5 everytime you make changes in the Markdown document.

How to use these plugins:

  • Markdown Editing: once installed, open a Markdown document, set the syntax to either Markdown, GFM, or MultiMarkdown by either click at the bottom right corner and select or use command + shift + p > Set Syntax: Markdown

  • Markdown Preview: once installed, open a Markdown document, press ** Command + Shift + p ** > Preview in browser > select the markdown option (You will see two options presented here. GitHub or Mardown. The github option means to use Github's online API for parsing the MD file, this can take sometime due to your connection speed. It also has a 60 calls/day limitaion. But the upside is you get a GFM with syntax highlighting and EMOJI support for free)

If selecting parser becomes anonying everytime you preview a MD file, you can always set a hotkey in your Keybindings - User file:

{ "keys": ["alt+m"], "command": "markdown_preview", "args": {"target": "browser", "parser":"markdown"} },


A "better" alternative: Github has released a text editor named Atom. It looks and works almost exactly like Sublime Text (most of the hotkeys are the same too). The best part is that it is FREE.

The reason I mentioned it is because Atom offers default Markdown support. It can open a split window with your MD file on the left and a live HTML preview on the right all in the text editor. There is no need to install any plugins or opening the web browser.

 
almost 3 years ago

To following up on my previous post on the essential settings for Sublime Text 3, I would like to layout a few non-essential but useful settings:

Change cursor style

Set the caret_style property to change cursor style:

"caret_style": "phase", // change cursor's blink style from on -> off to fade in -> fade out

Other caret styles can be used:

  • smooth (this is the default)
  • blink (I cannot tell the difference between this and smooth)
  • wide (the cursor width is wider and it does not blink)
  • solid (the cursor does not blink and stays "solid")
  • phase (fade in / fase out effect, very subtle)
Make cursor wider

If you just want the cursor to be wider but still blinks, use this

"wide_caret": true,
"caret_style": "blink",
Show ruler

To display a vertial ruler:

"rulers": [80, 120] // this will display two vertical rulers at 80 and 120 character count

You can word wrap at specific character count:

"wrap_width": 80 // default is 0

Read more here

Highlight the line where cursor is at
"highlight_line": true
Save all files when tab out to another Window

When you alt+tab to another window, saves all the files opened in Sublime:

"save_on_focus_lost": true,
Turn on spell checker
"spell_check": true, 
Visualize all whitespace characters
"draw_white_space": "all"

Available options are:

  • none
  • all
  • selection
Antialiasing Font for Retina Display

Try this option if you are on a retina display:

"font_options": ["gray_antialias"],
Always show code folding icon

The folding icon is only shown on hover by default, you can change it to always appear:

"fade_fold_buttons": false,
A handy website for generating setting file

http://eduardolopes.github.io/sublime-config/

 
almost 3 years ago

Sublime Text is great, but a few settings should be enabled as default to make the experience even better. Open your user setting file by pressing command + , on Mac or ctrl + , on windows. Or go to Preferences -> Settings - User:

TL;DR: just copy and paste the following chunk into your user setting file:

"always_show_minimap_viewport": true,
"indent_guide_options": [ "draw_normal", "draw_active" ],
"line_padding_bottom": 3,
"line_padding_top": 3,
"highlight_modified_tabs": true,
"scroll_past_end": true,
"translate_tabs_to_spaces": true,
"find_selected_text": true,
"word_wrap": true,
"ensure_newline_at_eof_on_save": true,
"bold_folder_labels": true,

and the following to Preferences -> Key Bindings - User:

{ "keys": ["command+v"], "command": "paste_and_indent",},
{ "keys": ["command+shift+v"], "command": "paste",},


Got 5 mintues to read?

Show current location on minimap

By default, the minimap does not highlight your current location. Use this setting to turn it on:

"always_show_minimap_viewport": true,
Convert tabs to spaces

It is import to convert tabs to white spaces when coding in Python, Ruby, YAML etc. To do that:

"translate_tabs_to_spaces": true,

Indentation by 4 spaces or 2 spaces depend on the language, it is better off to set the tab_size property separately for different file types. For instance, if I code in Python, I want the tab size to be 4 spaces. If I code in Ruby, I want it to be 2. To do this,

  1. Open a new document and click the bottom right corner (which says Plain Text) and set the language type to Python for instance
  2. Go to Preferences -> Settings - More -> Syntax Specific - User
  3. A file named ** Python.sublime-settings ** should be opened

Add the following lines:

{
    "tab_size": 4,
}
Enable word wrap
"word_wrap": true,
Find highlighted text

When you have a phrase highlighted and press command + f to look it up in the document, the highlighted word is not automatically copy and pasted to the Find dialog box. This can be fixed by:

"find_selected_text": true,
Scroll past the end of the document

It is sometimes awkward to look at lines at the bottom of the screen. You can actually scroll the windows pass the last line of the document:

"scroll_past_end": true,
Highlight indentation level

When writing in a language that requires multiple levels of indentations (like Python), highlighting the current indentation the cursor is in makes finding yourself within the nested code structure easier:

"indent_guide_options":
 [
     "draw_normal",
     "draw_active"
 ],

You can find a screenshot of this on Wes Bos' blog.

Adjust line height to increase readability

The default line height is too narrow for the eyes. Change it to a number that looks more comfortable:

"line_padding_bottom": 3, // adjust the number as you like
"line_padding_top": 3,
Highlight tabs with unsaved changes
"highlight_modified_tabs": true,
Ensure newline at end of file on save

UNIX/Linux convention is to have a newline at the end of the file. When collaboration with people using different OS, this is important:

"ensure_newline_at_eof_on_save": true,

WARNING:
You may attempt to set this as well:

"trim_trailing_white_space_on_save": true

But this can be hazardous if you write in Markdown. Because two trailing white spaces in Markdown means a newline character. A workaround proposed by this blog is to set the setting to True in the user setting file and set it to false in the Mardown syntax file.

Make folder name bold in the sidebar
"bold_folder_labels": true,


Paste code without having to fix indentation

This is definitely an annoying issue that everybody encounters all the time. When you copy and paste a block of indented code, the indentation on certain lines of the copied code is guaranteed to be messed up.

Sublime Text acutally has an Paste and Indent option which fix this issue but it is bind to command+shift+v instead of command+v. To fix this, Open Preferences -> Key Bindings - User and paste the following lines:

{ "keys": ["command+v"], "command": "paste_and_indent",},
{ "keys": ["command+shift+v"], "command": "paste",},
References:

http://stackoverflow.com/questions/25900954/80-characters-right-margin-line-in-sublime-text-3
https://www.sublimetext.com/forum/viewtopic.php?f=2&t=2478
http://wesbos.com/sublime-text-5-visual-tweaks/
http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/reference/settings.html

 
almost 3 years ago

Run the following command to copy the Packages and Installed Packages folder to your dropbox.

mkdir $HOME/Dropbox/sublime-text-3/
mv "$HOME/Library/Application Support/Sublime Text 3/Packages" "$HOME/Dropbox/sublime-text-3/"
mv "$HOME/Library/Application Support/Sublime Text 3/Installed Packages" "$HOME/Dropbox/sublime-text-3/"

Then, on a different computer, remove its Packages and Installed Packages folder and make a symbolic link to the copies in the Dropbox folder.

DSTPATH="$HOME/Library/Application Support/Sublime Text 3"
DROPBOX_PATH="$HOME/Dropbox/sublime-text-3"
rm -rf "$DSTPATH/Installed Packages"
rm -rf "$DSTPATH/Packages"
mkdir -p "$DSTPATH"
ln -s "$DROPBOX_PATH/Packages" "$DSTPATH/Packages"
ln -s "$DROPBOX_PATH/Installed Packages" "$DSTPATH/Installed Packages"

seen on: http://www.hongkiat.com/blog/sublime-text-tips/

 
almost 3 years ago

After updating pip using pip install -U pip,

$ pip
/usr/bin/pip: No such file or directory

pip can no longer be found:

$ which pip
/usr/local/bin/pip

$ pip
-su: /usr/bin/pip: No such file or directory

$ type pip
pip is hashed (/usr/bin/pip)

So pip is definintely in /usr/local/bin/pip but it is been cached as in /usr/bin/pip, thanks to the Stackoverflow question, the solution is very simple:

$ hash -r

When the cache is clear, pip is working again.

 
almost 3 years ago

Linters are used to highlight syntax and style errors in your source code. It is very useful to have when writing python, js, css, YAML, etc.

To enable linting in Sublime text 3, a few things need to be installed to get it working.

1) Install Sublime Linter first. This is a framework rather than an actual linter that can inspect your code. When we install other linters, they will depend on this framework. Thus, it is important to install this 'mother base' first.

  - open up package control, cmd+shift+p on Mac OS X, ctrl+shift+p on Linux/Windows
  - type 'install' and select 'Package Control: Install Packages' 
  - type 'SublimeLinter'

2) Install pep8 and pyflake. These python libraries will be used by the linters installed in the next step. Open up a console window, and install them by:

pip3 install pep8 pyflakes

The pep8 package is for style checking and pyflakes is for syntax error checking.

NOTE: make sure you are not installing these packages while using virtualenv. This is the mistake that I made. These pacakges need to be available system wide.

3) Install the actual linters: Sublime Linter-pyflakes, Sublime Linter-pep8:

  • open up package control, cmd+shift+p on Mac OS X, ctrl+shift+p on Linux/Windows
  • type 'install' and select 'Package Control: Install Packages'
  • type 'pep8' and find 'SublimeLinter-pep8'
  • type 'pyflakes' and find 'SublimeLinter-pyflakes'

4) After everything has been installed, restart sublime text 3

5) Now you should see error highlights in the gutter and around the code. To set when linting should be active, right click anywhere in Sublime Text 3 and select 'SublimeLinter' > 'Lint Mode'

6) It is very likely that pep8 will point out a few styling errors. It is anonying to fix them by hand. We can use another plugin to autoformat python code for us. Install Python PEP8 Autoformat via package control.

After this is installed, press 'ctrl + shift + r' to perform auto format.

7) pep8 linter is likely to complain about using 4 spaces instead of tabs when writing Python code. You can change this in Sublime's setting File. Go to 'Sublime Text > Preferences > Settings - User', add the following lines:

"tab_size": 4,
"translate_tabs_to_spaces": true,

Now, when the tab key is pressed, 4 spaces are inserted instead.

 
almost 3 years ago

“If you don’t design your own life plan, chances are you’ll fall into someone else’s plan. And guess what they have planned for you? Not much.” — Jim Rohn

 
almost 3 years ago

When trying to access either the MEDIA_URL or STATIC_URL in the template, it is important to add the following to the template context processor in settings.py:

TEMPLATE_CONTEXT_PROCESSORS += (
    "django.core.context_processors.media",
    "django.core.context_processors.static",
)

Setting the MEDIA_URL or the STATIC_URL in settings.py is not enough.

 
almost 3 years ago

Sometimes, we need to dump a database from production server and import it into development machine for testing purpose, to do that:

  1. use the following command to dump the database first:
su - postgres
pg_dump database_name > file_name

For instance, if I have a database called note, I would dump it as (this is done in shell not in psql):

pg_dump note > note.db.bak

Before importing note.db.bak into your local postgres, a few things need to be taken care of first:

1) You have to have the same list of users on your local machine as on your server. Use \du inside psql to list users on your server. Then, recreate them on your local machine in psql

create user user_name;

Users created on your local machine do not have to mirror the same privileges they have on the server.

2) Create the database that you want to import the backup into. In psql,

create database note_server_bak;

Now, you can import the data with the following command in shell:

psql -1 note_server_bak < note.db.bak

Note that the option after psql is 'dash one' not 'dash L'. This option means while importing, everything will be wrapped in a transaction so that if the import fails, nothing will be stored in note_server_bak, in other words, note_server_bak stays clean.

If you don't use -1 and an error occurs during importing, all of the data before the error will be imported into the database, which leaves your database with incomplete data. When you import the next time, you are going to see even more errors because of this.

 
almost 3 years ago

I was running a Django server on Ubuntu 12.04 and saw a lot of errors logged in gunicorn's error log file:

error: [Errno 111] Connection refused

One line of the error messages caught my eye, send_mail. Gunicorn is trying to send my the error message via the send_mail() function but failed. I realized that I didn't setup the email settings in settings.py.

I searched online for a solution and found two:

  1. send email via Gmail's smtp
  2. setup your mail server

Option 1 seems like a quick and dirty way to get things done, but it has a few drawbacks:

  1. Sending email via Gmail's smtp means the FROM field will be your Gmail address rather than your company or whatever email address you want it to be.
  2. To access Gmail, you need to provide your username and password. This means you have to store your gmail password either in the settings.py file or to be more discreet in an environment variable.
  3. You have to allow less secured app to access your gmail. This is a setting in your gmail's account.

I decided to setup my own mail server because I don't want to use my personal email for contacting clients. To do that, I googled and found that there are two mail servers that I can use:

  • postfix
  • sendmail

Since postfix is newer and easier to config, I decided to use it.

  1. Install postfix and Find main.cf

Note: main.cf is the config for postfix mail server

sudo apt-get install postfix
postfix is already the newest version.
postfix set to manually installed.

Great, it is already installed. Then, I went to /etc/postfix/ to find main.cf and it is not there! Weird, so I tried to reinstall postfix:

sudo apt-get install --reinstall postfix

After installation, I saw a message:

Postfix was not set up.  Start with
  cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf
.  If you need to make changes, edit
/etc/postfix/main.cf (and others) as needed.  To view Postfix configuration
values, see postconf(1).

After modifying main.cf, be sure to run '/etc/init.d/postfix reload'.

I see. So, I followed the instruction and copied the main.cf file to /etc/postfix/:

cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf

Add the following lines to main.cf:

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mydestination = localhost

Then, reload this config file:

/etc/init.d/postfix reload

Now, let's test to see if we can send an email to our mailbox via telnet:

telnet localhost 25

Once connected, enter the following line by line:

mail from: whatever@whatever.com
rcpt to: your_real_email_addr@blah.com
data (press enter)
type whatever content you feel like to type
. (put an extra period on the last line and then press enter again)

If everything works out, you sould see a comfirmation message resemables this:

250 2.0.0 Ok: queued as CC732427AE

It is guaranteed that this email will end up in the spam box if you use Gmail. So take a look at your spam inbox to see if you received the test mail (it may take a minute to show up).

If you recevied the test email, then it means postfix is working properly. Now, let's config Django to send email via postfix.

First, I added the following line to my settings.py file:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = 'Server <server@whatever.com>'

Then, I opened up Django shell to test it:

./manage.py shell

>>> from django.core.mail import send_mail
>>> send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['to@example.com'], fail_silently=False)

Again, check your spam inbox. If you received this mail, then it means Django can send email via postfix, DONE!

references:
http://stackoverflow.com/questions/5802189/django-errno-111-connection-refused
http://stackoverflow.com/questions/4798772/postfix-its-installed-but-how-do-i-test
http://stackoverflow.com/questions/26333009/django-postfix