To writing Markdown in Sublime Text 3, two plugins are needed to:
- Enable Markdown syntax highlighting
- Convert Markdown document to HTML and preview it in a web browser
To accomplish these, install these two plugins:
Markdown Editing: This plugin enables syntax highlighting for standard Markdown, Github Flavoured Markdown(GFM) and MultiMarkdown
Markdown Preview: as the name suggests, it converts your MD file to HTML and opens it in your system default browser
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:
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.
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:
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
To display a vertial ruler:
You can word wrap at specific character count:
Highlight the line where cursor is at
Save all files when tab out to another Window
When you alt+tab to another window, saves all the files opened in Sublime:
Turn on spell checker
Visualize all whitespace characters
Available options are:
Antialiasing Font for Retina Display
Try this option if you are on a retina display:
Always show code folding icon
The folding icon is only shown on hover by default, you can change it to always appear:
A handy website for generating setting file
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:
and the following to Preferences -> Key Bindings - User:
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:
Convert tabs to spaces
It is import to convert tabs to white spaces when coding in Python, Ruby, YAML etc. To do that:
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,
- Open a new document and click the bottom right corner (which says Plain Text) and set the language type to Python for instance
- Go to Preferences -> Settings - More -> Syntax Specific - User
- A file named ** Python.sublime-settings ** should be opened
Add the following lines:
Enable word wrap
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:
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:
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:
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:
Highlight tabs with unsaved changes
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:
You may attempt to set this as well:
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
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:
Run the following command to copy the Packages and Installed Packages folder to your dropbox.
Then, on a different computer, remove its Packages and Installed Packages folder and make a symbolic link to the copies in the Dropbox folder.
After updating pip using pip install -U pip,
pip can no longer be found:
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:
When the cache is clear, pip is working again.
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.
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:
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.
- 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:
Now, when the tab key is pressed, 4 spaces are inserted instead.
“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
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:
Setting the MEDIA_URL or the STATIC_URL in settings.py is not enough.
Sometimes, we need to dump a database from production server and import it into development machine for testing purpose, to do that:
- use the following command to dump the database first:
For instance, if I have a database called note, I would dump it as (this is done in shell not in psql):
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
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,
Now, you can import the data with the following command in shell:
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.
I was running a Django server on Ubuntu 12.04 and saw a lot of errors logged in gunicorn's error log file:
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:
- send email via Gmail's smtp
- setup your mail server
Option 1 seems like a quick and dirty way to get things done, but it has a few drawbacks:
- 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.
- 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.
- 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:
Since postfix is newer and easier to config, I decided to use it.
- Install postfix and Find main.cf
Note: main.cf is the config for postfix mail server
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:
After installation, I saw a message:
I see. So, I followed the instruction and copied the main.cf file to /etc/postfix/:
Add the following lines to main.cf:
Then, reload this config file:
Now, let's test to see if we can send an email to our mailbox via telnet:
Once connected, enter the following line by line:
If everything works out, you sould see a comfirmation message resemables this:
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:
Then, I opened up Django shell to test it:
Again, check your spam inbox. If you received this mail, then it means Django can send email via postfix, DONE!