Thursday, July 23, 2009

Syntax Highlighting Pager: Vim

5 comments
Pagers

If you use a Linux/Unix system using a pager should be quite familiar to you. A pager in the Unix sense is a program which lets you view pages of contents. Examples for pagers are the Unix commands "less" and "more". It's likely that your man page viewer is also using one of these.

The program less is perhaps the most popular of these, with more features than "more". These pagers are nice to use use and come very handy in day to day Linux/Unix operations. When you just need to view a file (instead of opening it for editing) it's quite natural to use less. If it's a file with a few lines we'd usually use cat, but less is more convenient on longer files.

One irritating thing about less is, there is no syntax highlighting support there by default. You can try to use an external program like "highlight" for the highlighting part and then pipe it to less. (Eg: $ highlight --ansi --force my_file | less -R) But that's tedious. If the filetype isn't supported by "highlight" you need to add them manually.


Enter Vim

If you have painstakingly tuned a beautiful Vim environment like I did, you might be wondering if there's a way to use Vim as a pager. In fact you can.

The good folks at the Vim project ships a nice configuration file and a shell script just in case you love Vim so much that you want it to be your pager too. It's usually located under the macros directory in your Vim installation directory. For example in my case it's: /usr/share/vim/vim72/macros/

The file we need is named less.sh. It in turn uses a file named less.vim. Which means you can edit the less.vim file in order to introduce your custom changes, if you need any. In that case it's better to copy the two files to somewhere else and do it. That way you leave the original files untouched.

However, if you simply just need to use Vim as a viewer we can do it easily.

1. Create a bash alias to /usr/share/vim/vim72/macros/less.sh
Eg: in your .bashrc specify,
alias vess='/usr/share/vim/vim72/macros/less.sh'

2.
Next time you log in the alias you created will be ready to use. Or you can just reload the .bashrc again by running
Eg: $ source ~/.bashrc

Now you are good to use vess instead of less. You can use any name you instead of vless as long as it doesn't conflict with existing commands, aliases, etc.

You can always create a link named less so that your custom configuration is used instead of the original less. But I'd rather keep it as it is and use my new "vess" whenever I need a pager.

The final result acts like less. You can traverse with arrow keys, exit with pressing "q" and do on. If you need to edit the file you are viewing you can press "v" to start editing.
Read More...

Wednesday, July 15, 2009

My Vim Configuration

10 comments
My Editors

I have used many text editors. Some for specific tasks such as LaTeX editing (Eg: Texmaker), coding (Eg: gedit with plugins) and the others for more generic text editing.

As a system administrator I have been using Vi/Vim all along for editing configuration files and the likes. It was always convenient as almost every Unix system has vi/vim. For me the first Unix editor I used was Emacs. I felt more at home with Emacs key combinations than a hard to fathom Vim modes. Time passed and after a little while, I got used to basic Vi concepts. Since then all of my editing in command line was done in vi/vim.

As I've said recently, I have been transforming myself from a sysadmin to a sysadmin/developer kind of person. For coding purposes I used gedit. Some people might think of gedit as the notepad.exe of Linux, but that's so not correct. In fact gedit is a powerful text editor with a lot of plugins to extend it's capabilities. For example there there are plugins for LaTeX, Python consoles, bash terminal, snippet completing, snap open, integration with VCSs, smart tab/space, code formatter, class browser, Ruby on Rails modes, encryption, smart indentation, ToDo list, web browser, full screen editing, split screen, PO file editing, markdown preview, etc. among other things. As you can see, it's no toy editor. It's fully fledged editor for serious work.

Let's get back to the topic. I love gedit and I use it whenever I need. But from a couple months ago, my main editor is Vim. Vim is the most popular improved clone of the original Vi editor. It's been around for about 18 years. So you too might be wondering "Why, oh WHY, do those #?@! nutheads use vi?" or why Vim, an 18 year old, hard to learn terminal based text editor?!

I can take a lot of posts to answer that or assume you will try it yourself and get into your own terms with Vim. I'll go with the latter and share with you what my current (as of 15 July 2009) Vim configuration is.




My Vim configuration: Gavim


Note: I'm working on a Fedora Linux system. By default there's a very decent Vim configuration in Fedora. So I took the default global Vim configuration file in a Fedora 10/11 system and did the tweaks to get it to my liking. As you would have expected, a significant part of the vimrc is straight from Fedora.

Some of the plugins I use are taken from vim.org, while some are taken from their sources (mostly from GitHub repositories).

And oh, I only use Vim not gVim (Vim with a GUI).


In the first few lines of my Vim configuration file is an entry which says,
Eg:
set nocompatible

which is to tell that it shouldn't try to be compatible with older Vi, and just use the Vim behaviour. I also liked to have the editor show the line numbers on left. There are other other small tweaks I like to have.

All these things are included in my Vim configuration files repository, and the files are well (hopefully) commented. So anyone interested can refer to my vimrc and find more about those.

One thing to notice is I'm using the key "," as my leader key.
Eg:
let mapleader = ","

Vim 7 also has built in spell checking abilities. I find this very useful as I use Vim for every typing needs these days. I have configured 2 keyboard shortcuts to enable and disable spell checking.
Eg:
map <F8> <Esc>:setlocal spell spelllang=en_us<CR>
map <F9> <Esc>:setlocal nospell<CR>

This means that I can press F8 key to turn spell checking on with language set as English (USA) and turn off by pressing F9.

I also like to have the ability fold/unfold code blocks when coding. For this I used another Vim built in.
Eg:
setlocal foldmethod=syntax
setlocal nofoldenable

which sets the folding based on syntax (i.e. programming language), and doesn't enable it by default.

I also have set a place holder character to be displayed while typing "space" and "tab" characters. These are displayed until you type a new character after the tab/space.
Eg:
set list
set listchars=trail:⋅,nbsp:⋅,tab:▷⋅

And BTW, the theme (colour scheme) I'm using for Vim is called "darkblack". It's a dark theme which provides very good readability. It is included in my repository.

To get the best view for most of the themes and the smoother fonts you need to tell Vim to use 256 colors in the terminal.
Eg:
set t_Co=256

As you already know most Linux systems have excellent font rendering. The default on Ubuntu systems are not good, but other systems like Fedora was having quality font rendering by default for a while. So having a nice colour theme with smooth fonts is a very pleasant experience. See the screenshots for proof. :)

Those are the major Vim features I'm using. You can see everything in the configuration files. Now let's check the major Vim plugins I'm using.



Plugins I use

Different plugins may have different installation steps. Check the "Readme" files of the plugin for more details. Generally it involves copying the .vim file in the plugin package into the plugin directory of the vim configuration directory.
Eg:
$ cp ./NERD_tree.vim ~/.vim/plugin/

1. NERDTree

The NERDTree plugin by Martin Grenfell is an excellent file system tree browser plugin for Vim.

You can set the exact key binding to activate NERDTree by specifying
Eg:
nnoremap <leader>d :NERDTreeToggle<cr>

Once installed you can toggle the tree browser pane by pressing [leader]d in command mode. Eg: ,d

I have also set this:
Eg:
let NERDTreeMapActivateNode='<CR>'

which aloows you to toggle the expanding of directory views by pressing "Return" (Enter) on the directory node.




2. Scratch

Scratch gives a temporary scratch buffer area which will be discarded when you exit vim. This is not saves in a file. It's quite useful to jot down something quick while you are editing.


I'm using s to toggle the scratch area.
Eg:
function! ToggleScratch()
if expand('%') == g:ScratchBufferName
quit
else
Sscratch
endif
endfunction

map <leader>s :call ToggleScratch()<CR>

3. Snipmate.vim


Snippet completion is a very useful thing to have when you are coding. This is a feature which can be found on almost all Integrated Development Environments (IDE). Snipmate is a cool plugin with brings some of snippets from the popular test editor TextMate to Vim.

Martin Grenfell have a useful collection of snippets to use with Snipmate.vim.



4. Bufexplorer

This is a simple Buffer Explorer plugin which with list the open buffers and let you go to any of them.


5. VCSCommand

This plugin takes care of working with different version controlling systems such as Git, Bazaar, Subversion, CVS, etc.



How to use this for you

That's all I have to tell about my Vim configuration for now. Some of the general configuration and some plugins I use are not mentioned in this post. But you are welcome to check it out.

Just in case if you are interested in using these for your Vim setup, be my guest. Be informed though. This works fine with Fedora 10/11 systems. I haven't checked on other systems, which might ot might not have differently compiled Vim setups. In any case you are welcome to grab the files and tweak it to your liking.

Here's the link to my Vim files repository: gavim

You can find more instructions in the "Readme" file there.
Read More...

Monday, July 06, 2009

Still Blogging. Yes, I am

3 comments
I know I have been away from this blog for quite a long time. It's been just over 6 months since I've blogged. There are reasons and other stories. But apart from all of those, I'm glad I am writing here again. I don't know how often I'll be writing again, certainly not once a day (like I ever did that). However I'm hoping now I'll have freedom to write whenever I feel like. But the best way to keep in touch with me is to follow me on Twitter.

Here's a brief account of how my life's been during the past few months which saw me transform significantly.


Microblogging

First thing which slowed down my blogging is microbloggin. Yes, I got in to Twitter early last year and since then never look back. I'm an avid Tweeter and it has become a part of my daily routine. One cool thing I like about Twitter is it's no nonsense and does just one thing, and one thing properly. It lets me keep in touch with like minded people and share information as a side benefit. It's the one social network service which I found non-obtrusive. Another thing Twitter did was killing my dilemma about write or not about every interesting thing I came across. It's a good thing. Now I can write here only the things I feel like.


Employment

My professional life has not been so good. I got to learn a few important lessons about how *not* to do things. I'm glad I got to learn those things but I'm really disappointed about a certain few people, whom I thought had the guts to walk the talk. I'm not without error in this interest. So I am not blaming anyone for taking my own enthusiasm about FOSS (and computing in general) and hanging myself with it. I did that to myself. Those people just gave me a place to do just that. :)

To be concise, I left one place and joined a new place hoping that there's much to do and gain. Unfortunately it was not so to be. If it hadn't been my parents, that patch was enough to see me to financial oblivion. I'm all grateful to my family including my love and my good friends for helping me to hold up. Being said all that, today I'm doing as well as any time else. Life's good. There's a lot of challenges to be faced, but I'm up to it.


Education

I've been an undergraduate from the beginning of the recorded history I guess. When some of my friends have achieved their masters I was still in my bachelors. To my justification I've been working and doing my studies part time.

This year, finally, I finished my undergraduate studies by completing Business Computing program of the University of Wolverhampton. The lecturers were wonderful and they flew all the way from UK to deliver the lectures. Academic staff at IDM did a very good job coordinating the process.

Unfortunately I missed this years graduation ceremony. No worries, I can wait for the ceremony.


Maturity

I assure you that I've seen a a lot of maturity in the last few months. A lot of people close to me might beg to differ. ;) But hey, I know how I think better.

I used to be this guy who went with the flow and thought everything would be more or less OK (in life). Maybe not exactly in the way it sounds like, but there were certain things I thought I took serious. But really I had no idea how serious they were. And I always felt a little shy/awkward to discuss about my girlfriend with my parents, even when they've known her for a long time and considered her to be one of their own. :)

From that I went on to being the guy who sat down and had 5.5 hour discussions with family about marriage, wedding, career, and etc, etc. It might not sound convincing, but I know who I am now.


New Computer


Just last Friday, I bought a new computer. Which reminds me that I have to get my old laptop into repair. I didn't feel like buying a new laptop because I hope I can get the old one working and because I can get a lot of power for a low cost if I bought a desktop. Core i7 is still a little pricey, so my bargain was a Core 2 Duo.

I got a machine with Intel Core 2 Duo 2.8 GHz processor, 4 GB memory, 320 GB SATA disk, DVD burner, Intel motherboard, 17" widescreen LCD display, Foxconn casing, etc. all under 60k rupees.


My Main OS

If you are reading this anyway the chances are, you already know that I use Linux 100%. Sometimes I've dual booted with FreeBSD and OpenSolaris, but I'm mainly a Linux user. I have long given up using Windows. I've been running Red Hat (until version 9), then Fedora, then completely Debian, then Ubuntu for a while and finally Mint until recently.

I still run Mint at home where my father, brother, cousins, etc. all who are not Linux folks are happily using Linux Mint. Kudos to the Mint team for making a very new / convert user friendly distribution of Linux. Whenever a new Linux user asks for a first distro I hand out Mint these days. It's very good. Their latest version (7), named Gloria gives a superb 1st impression.

However my personal favourite distro now is Fedora. I've run Fedora 9 alone with Mint and it was solid. So I switched to Fedora as my main OS when Fedora 10 was released. It didn't disappoint. To me Fedora has the perfect balance between use friendliness and power user convenience. It doesn't stay on my way and in the same time it makes a lot of things I regularly do (coding, documenting) really convenient. Fedora is a solid distribution of Linux and probably the one who's riding the innovation / cutting edge tide at the foremost. So I installed Fedora 11 on my shiny new desktop PC, and I'm loving it.


Contribution

I've always been contributing to FOSS projects whenever I could via bug reports, mailing lists, forums, helping out new users, public events, etc. Now I'm comfortable enough reading code (remember, I'm a SysAdmin) so I might take more active roles in near future.

I've also started lurking more in IRC (Internet Relay Chat) channels at Freenode.net. My nick on Freenode is "gaveen".

I also started contributing to the monthly, free, online IT magazine, diGIT. I'm writing a Linux basics article there. It's good to see a free magazine with good content. Gihan Fernando is leading and coordinating this effort. It should be much commended how much he takes with his crew to get diGIT out by the 1st day of every month.


Professional Transformation

From the regular System Administrator, I've gone into become more coding loving (thanks to Ruby) and more cloud aware, agile aware. I'm so glad that I looked into Ruby programming a few years ago. For the record Ruby has a great assortment of system administration / infrastructure related projects in its ecosystem.

As they say, I came for Rails and stayed for Ruby. The attraction which caught me was Ruby on Rails web application framework. But what kept me all interested was the language behind it.

I'm much interested in following things these days: Puppet (configuration management), Hudson (continuous integration), RabbitMQ (AMQP/message queuing) among many others as usual. As you can see, I've taken quite an interest in the next generation of system infrastructure. ;)

About version controlling systems, there's been no hiding how much I like Git. There's no need to repeat it. My stance haven't changed. Again for the record I started using Git before the Ruby crowd changed from Subversion (seemingly over one night) into Git users. :) I've used Mercurial, and also closed a repo using Bazaar ;) but Git is still my favourite.

My main text editor was gedit (with a few plugins) for a while. I always used vi/vim on a terminal, but on a desktop, gedit did well for me. Then a few months ago I had the opportunity to use Vim for things other than just basic editing. I used, I witnessed and now I am a believer. :)

In fact, I'm loving Vim so much that my next blog post will be about the Vim configuration I'm using. To be fair gedit is a very good editor and I also have good expectation from the new Redcar editor. However I don't see myself switching away from Vim in the foreseeable future.


That's all for this post. I'm not going to say I'm back (because I've done that before). But I'm saying I'm blogging again. I'll be back soon with a post about my Vim configuration. If you like to keep more in touch follow me on Twitter.
Read More...