Browsing Tag

syntax highlight

Latex

Bringing Colors to TeXShop

January 4, 2013

In an effort to continue posts with colors in common I want to bring up TeXShop. TeXShop is a latex editor of Mac that comes with MacTex distributions. In my mind it is the only Latex editor you will need, but what do I now. For those of you who do not know about Latex, it is a markup typeset base on Donald Knuth’s TeX typesettings. Latex has the ability to handle just about anything: math symbols, algorithms, flow charts, you name it. I have learned how to use Latex about 3 years ago, and I cannot recall a document I wrote that has not been typed in Latex. That is a whle other story for another time.

Here is a Simple Latex document I found online. I had made just available here just in case you could not find it. I also had to add comments to show you how annoying the color scheme can get. Here is a screen shot of what the file would look like with the default TexShop color scheme:

wpid-texshop_default_1-2013-01-4-07-42.png

I think this is just awful. So, lets change it. In order to change the colors you will have to run some terminal command. I know, one would think that by now you could change it through preferences. Nope. There are 7 items you could change the colors to. For each you would have to run 3 commands, one for each RGB value. The RGB values have to be in a float RGB scale. You can consult this float RGB color chart if you need. The 7 items you could color are:

background
commands
comments
foreground
index
insert_point
marker

For example, say we want to change the comments from that horrible red to something more easy on the eye. Lets go with some light purple, like <0.8, 0.0, 1.0>. The commands will be:


defaults write TeXShop commentred 0.8
defaults write TeXShop commentgreen 0.0
defaults write TeXShop commentblue 1.0

You will have to restart TeXShop and now comments will look like this:

wpid-texshop_default_2-2013-01-4-07-42.png

In a similar way you can change the rest of the colors to match you style. Here is a bash script to change all the colors on TexShop. The colors are set for the default colors. You can change any of the colors to match you style. If you want to go back to the original simply download the file and run it. I would suggest you keep a local copy of your settings just in case. In order to run the code you will have to change the permissions to 700 by running this command:


sudo chmod 700 texshop_colors.sh

Now you can change any of the colors at by running the file instead of running multiple commands.


./teschop_colors.sh

Terminal

Syntax Highlighting in the Mac OS X Terminal

January 2, 2013

In your programming career, sooner or later you will come across the command line text editor. In the beginning it will look and feel wired, but with time you will get used to it. After a while you will get used to not having the IDE and fancy auto complete. You will even get used to the fact that the syntax is not highlighted, but you do not have to. As it turns out, you can 'activate' your terminals syntax highlighting features. This will help you a lot down the road and can be applied for Mac (yes, even servers, no I do not know about Windows.

So I created a simple program in python to see what my terminal will display now if I were to open the file via vi. Here is the file content:


import time

def main():
	print 'hello'
	x = 42

main()

If I were to run the following command (assuming the file is named syntax.py):


vi syntax.py

The output:

syntax highlight off

We can do better than that. First of all navigate to your home folder. We are going to add some color highlighting to the ls function. This will make it easy to recognize file types in the command line. That is the folder that opens up when you open up the terminal. Now run the following command to create (or modify if exsiting) your .profile file.


vi .profile

If you have File Vault turned on, you might have to add sudo at the beginning of the command and type your password at the prompt. Once the file is open type 'i' to switch to vi insert mode. Then type the following into the file:


alias ls='ls -G'

After you are done typing hit the 'esc' key followed by 'wq' and enter. This will switch you out of insert mode, save (write) & quit. See An Extremely Quick and Simple Introduction to the Vi Text Editor for more information about vi. This command will add some syntax highlighting to your terminal ls function. So now your directory list will be colorful. To add the syntax highlight to vi we will need to do a little more work.

First, change the directory like so:


cd /usr/share/vim

Then open a file called vimrc using sudo. Like this:


sudo vi vimrc

This will take you back into the vi enviorment. Now you are going to go into insert mode again by pressing 'i' key. Add the following code after the line:


backspace = 2

The code to add:


set ai                  " auto indenting
set history=100         " keep 100 lines of history
set ruler               " show the cursor position
syntax on               " syntax highlighting
set hlsearch            " highlight the last searched term
filetype plugin on      " use the file type plugins

" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if ! exists("g:leave_my_cursor_position_alone") |
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif |
\ endif

Save and exit again, by pressing 'esc' and 'wq'. Restart your terminal session and everything should be all colorful. If you were to go back to view your vimrc file you should see something similar to this:

vim color configure

And out python file from before should look like this when you open it:

vim python color highlight

Much better.

Note that at any point you can disable this feature by removing the added line of code. You can also disable terminal syntax highlighting by typing in the following command from within vi:

 :syntax off