Posts tagged GNU/Linux
Bash spellchecker
0Spell checking in bash is a cool feature which can be added by adding just one line in your bashrc file.
shopt -s cdspell
That’s all :)
Add that to your ~/.bashrc file.
If you want to enable this for all the users, then append this to your /etc/bash.bashrc (in Ubuntu) or /etc/bashrc (in Fedora). Of course, you need the root privilege to edit the files in /etc.
Now try these
cd /hoem
cd /hoe
Cool right ;)
SSH Client…
2I guess every GNU/Linux user must have sshed from one comp to another sometime or the other. We have different servers for various purposes in our college and ssh is a common thing. Most of the time, we end up typing some thing like
ssh -XCYP hari@spider.nitt.edu
Though not many knows what these options stands for, life will become very simple if you try to understand the command and create a configuration file for ssh and use it when you ssh from one box to another.
The ssh client takes the parameters from three places in the following order.
- Command line options
- User-specific configuration file
- System-wide configuration file
The command line options are the one you specify when connect to other system using the ssh command. For example ssh -X specifies that X11 forwarding should be enabled.
The User specific configuration file is ~/.ssh/config.
The System wide configuration file is /etc/ssh/ssh_config. Note that the configuration file for ssh server (sshd_server) is also found in this folder. The file ssh_config is the configuration file for the ssh client while sshd_config is the configuration file for the server.
Any configuration value is only changed the first time it is set. So if you run the ssh command with X option, X11 will be forwarded no matter what values ~/.ssh/config and /etc/ssh/ssh_config file has. The values are parsed in the order mentioned above.
The User config file gives users the choices to configure ssh client when you ssh often. A config file is explained with an example below.
Host codelabs
hostname codelabs.nitt.edu
user hari
ForwardX11 yes
port 22
In the above example, very few options were added to make the config file simple. I guess that will be enough for everyone.
The config file has Host blocks. The properties which come under a host are set when you connect to the given to any host. In this example i have used the host machine called codelabs.nitt.edu. You can also specify * to apply to any host or 10.1.39.* to apply to all machines which comes under 10.1.39. series. Remember that the configuration values are set only once. So if * appears in the beginning of your config file, then the configurations which come below that may not be used by the ssh client. For example consider the following configuration file.
Host *.edu
ForwardX11 yes
Now in the above case when you connect to any server which ends with .edu (spider.nitt.edu, codelabs.nitt.edu …) then the X is forwarded. But if you want to diasable X11 forwarding for spider.nitt.edu, then the following won’ t work
Host *.edu
ForwardX11 yes
Host spider
hostname spider.nitt.edu
ForwardX11 no
This is because in the first block, ForwardX11 is set to yes for all hosts ending with .edu and then it can’t be changed. The correct way to block X11 forwarding for spider is to have a config file as shown below.
Host spider
hostname spider.nitt.edu
ForwardX11 no
Host *.edu
ForwardX11 yes
You can tweak almost everything you need from the public key file, port, number of passwords prompts, compression, ciphers, compression level, user, tunnel, tunnel device and lots more. :) Happy sshing :-)
Gedit Plugin
2In this article, we’ll see how to write plugins for Gedit. Since plugin development is easy in Python, I’m planning to explain this using Python.
When the first caveman programmer chiseled the first program on the walls of the first cave computer, it was program to paint the string “Hello, world” in Antelope pictures. Roman programming textbooks began with the “Salut, Mundi” program. I don’t know what happens to people who break with this tradition, but I think it’s safer not to find out. So we’ll also deal only with HelloWorld in our first plugin.
This plugin is going to do two things. One is to add the string “HelloWorld ” at the cursor position. The next is to convert all “Hello” in the program to “World”. A neat tutorial to write python plugins is can be found at live.gnome.org. Though you can skip that tutorial for now since I’ve explained most of the things one need to know to write this plugin. But i suggest you read that tutorial soon.
Some of the paths you need to know for writing a plugin.
| Path | Details |
|---|---|
| /usr/lib/gedit-2/plugins/ | System-wide plugins directory |
| ~/.gnome2/gedit/plugins/ | User Plugins directory |
| /usr/share/gedit-2/ | Data needed for system-wide plugins. |
| ~/.gconf/apps/gedit-2/ | Gedit configuration. Can be modified using gconf-editor. |
| /usr/share/gtk-doc/html/gedit | gedit Reference Manual |
Every python plugin needs at least two files. Let us name our plugin as “frisco”. So we should have two files namely, frisco.gedit-plugin and frisco.py
Let’s start with the contents of frisco.gedit-plugin.
[Gedit Plugin]
Loader=python
Module=frisco
IAge=2
Name=HelloWord
Description=A HelloWorld plugin for Gedit.
Authors=Harishankaran K <sp2hari@gmail.com>
Copyright=Copyright © sp2hari
Website=http://www.sp2hari.com
The contents of this file is almost same for all the plugins. The module, name and description will change for different plugins. If your plugin has the python file frisco.py, then the modulde is frisco.
Note :: We don’t specify the extension in the module name.
The .gedit-plugin file is done now. Next let’s move on to frisco.py.
#!/usr/bin/python
import gedit
class HelloWorldPlugin(gedit.Plugin):
def __init__(self):
print "Plugin loaded"
def activate(self, window):
print "Plugin activated"
def deactivate(self, window):
print "Plugin deactivated"
def update_ui(self, window):
pass
This file, frisco.py derives one class from the Gedit.plugin and defines activate, deactivate and update_ui. activate() is called when the plugin is activated. Similarly deactivate() is called when the plugin is deactivated. We will check how our plugin works now.
Run gedit from terminal and check whether your plugin is listed in the plugins list.
You can see that the details you provided in frisco.gedit-plugin can be viewed in the About Plugin and the Credits.
Now enable the plugin and look at shell prompt. You will see the print statements being execute when we activate and deactivate the plugin. :). Wow, our first plugin is ready to add more spice. :-)
Note:: If you get an error saying
WARNING **: Cannot load Python plugin ‘HelloWord’ since file ‘frisco’ cannot be read.
WARNING **: Error activating plugin ‘HelloWord’
Then the file frisco.py is not present in your plugins directory or the python file has an error. Fix the code and try again. You will get the output as shown below.
The output for the above plugin in the shell when the plugin is activated and deactivated for a few times will be like
hari@hari-laptop:~$ gedit
Plugin loaded
Plugin activated
Plugin deactivated
Plugin activated
Plugin deactivated
Plugin activated
Plugin deactivated
Plugin activated
Now we have to add functionality to this plugin. A good practice while coding plugin is the create a separate ‘Helper’ Class which will control the window and do all useful actions. It’ll be called once in the main class.
So we will modify the HelloWorldPlugin class as follows
#!/usr/bin/python
import gedit
class HelloWorld(gedit.Plugin):
def __init__(self):
gedit.Plugin.__init__(self)
self._instances = {}
def activate(self, window):
self._instances[window] = HelloWorldHelper(self, window)
def deactivate(self, window):
self._instances[window].deactivate()
del self._instances[window]
def update_ui(self, window):
self._instances[window].update_ui()
The code for the HelloWorldHelper class will be
class HelloWorldHelper:
def __init__(self, plugin, window):
print "Plugin created for", window
self._window = window
self._plugin = plugin
def deactivate(self):
print "Plugin stopped for", self._window
self._window = None
self._plugin = None
def update_ui(self):
# Called whenever the window has been updated (active tab
# changed, etc.)
print "Plugin update for", self._window
When you run gedit from terminal, now, it will print more details like the Gedit window object. A sample run for the above code is given below
hari@hari-laptop:~$ gedit
Plugin created for <gedit.Window object at 0x8521504 (GeditWindow at 0x816e000)>
Plugin update for <gedit.Window object at 0x8521504 (GeditWindow at 0x816e000)>
Plugin update for <gedit.Window object at 0x8521504 (GeditWindow at 0x816e000)>
Plugin stopped for <gedit.Window object at 0x8521504 (GeditWindow at 0x816e000)>
Plugin created for <gedit.Window object at 0x8521504 (GeditWindow at 0x816e000)>
Plugin stopped for <gedit.Window object at 0x8521504 (GeditWindow at 0x816e000)>
Plugin created for <gedit.Window object at 0x8521504 (GeditWindow at 0x816e000)>
Plugin stopped for <gedit.Window object at 0x8521504 (GeditWindow at 0x816e000)>
Plugin created for <gedit.Window object at 0x8521504 (GeditWindow at 0x816e000)>
Hereafter there is no need to change the HelloWorldPlugin class. So we’ll work only on the HelloWorldHelper class. We will define a ui_str which will enable us to add a menuitem in the Tools menu.
We will define the ui_str as follows
import gtk
ui_str = """<ui>
<menubar name="MenuBar">
<menu name="ToolsMenu" action="Tools">
<placeholder name="ToolsOps_2">
<menuitem name="Insert HelloWorld" action="InsertHelloWorld"/>
<menuitem name="Change Hello to World" action="ChangeHellotoWorld"/>
</placeholder>
</menu>
</menubar>
</ui>
"""
The activate, deactivate and update ui functions for the HelloWorldHelper Class are modified as follows
def __init__(self, plugin, window):
self._window = window
self._plugin = plugin
# Insert menu items
self._insert_menu()
def deactivate(self):
self._window = None
self._plugin = None
self._remove_menu()
def update_ui(self):
# Called whenever the window has been updated (active tab
# changed, etc.)
self._action_group.set_sensitive(self._window.get_active_document() != None)
Now we need to write the method insert_menu which will register the callbacks for the actions like inserting hello world and changing hello to world.
The insert_menu function is defined as followed. If you know gtk or pygtk then it’ll be easy for you to understand that code. I’ll advise you to read the basics of pygtk if you plan to write your own plugin.
def _insert_menu(self):
# Get the GtkUIManager
manager = self._window.get_ui_manager()
# Create a new action group
self._action_group = gtk.ActionGroup("ExamplePyPluginActions")
self._action_group.add_actions([("InsertHelloWorld", None, _("Insert HelloWorld"),
None, _("Insert HelloWorld"),
self._inserthelloworld)])
self._action_group.add_actions([("ChangeHellotoWorld", None, _("Change Hello to World"),
None, _("Change Hello to World"),
self._hellotoworld)])
# Insert the action group
manager.insert_action_group(self._action_group, -1)
# Merge the UI
self._ui_id = manager.add_ui_from_string(ui_str)
def _remove_menu(self):
# Get the GtkUIManager
manager = self._window.get_ui_manager()
# Remove the ui
manager.remove_ui(self._ui_id)
# Remove the action group
manager.remove_action_group(self._action_group)
# Make sure the manager updates
manager.ensure_update()
Now we are all set the two final functions which will do the required actions. These are defined in the helper class as follows
def _hellotoworld(self, action):
doc = self._window.get_active_document();
if not doc:
return
doc.replace_all("Hello", "World", 0);
def _inserthelloworld(self, action):
doc = self._window.get_active_document();
if not doc:
print "Noe";
return;
doc.insert_at_cursor("HelloWorld")
Well, that’s all. Your first plugin is up and running now :-) :-)
The python files used in this example can be downloaded here.
Gedit
1Every GNU/Linux user must be knowing about Gedit. This tiny text editor which comes with gnome is much more powerful than we all think. A few customizations and adding a few plugins will convert this simple text editor into a complete IDE for any programming language. In this article, i have mentioned about the configurations i have made in my box and the plugins which you might find useful.
Any text editor has to be customized according to the need of the end user. The customizations for a C or C++ coder and a PHP or Python coder are totally different. So let me write here according to various needs. Check out in which category you fit in and use those customizations.
The Preferences window in Gedit is the core which converts this simple text editor into a complete IDE. It has features to customize almost everything.
General Configurations
Open Preferences Window (Edit -> Preferences)
In the View Tab
- Check Enable text wrapping.
I prefer to see the long lines in the same window, which reduces the pain of scrolling across the window using the mouse.
- Check Display line numbers.
- Check Highlight current line
In my Ubuntu Studio theme, it wasn’t easy to read when the current line is highlighted. So i disabled this in that theme. So check this whether this suits your gnome theme when you enable this.
- Highlight matching bracket
An useful option if you are used to getting confused with brackets.
In the Editor Tab
- Set the Tab width as 4
- Check Enable automatic indentation
- Uncheck Create a backup copy of files before saving
Again this depends upon your need. Backup files are created by adding a ‘~’ to the current file name. If you are a web developer and coding in any web development language, better don’t enable this option. Many times these ‘~’ files created might create a security hole in your server. For example, mysql_connect.php file will have a backup called mysql_connect.php~ and your web server will parse the mysql_connect.php but it won’t do the same for the backup file. It’ll just display the source code to the client and thereby your your mysql password is gone :P
- Check Autosave files every 5 minutes and make that 2 mins
If you don’t have the habit of saving the file often, then this might save you when something terrible happens :P
In the Font & Colors Tab
- Uncheck the Use the system fixed width font
- Set the size of the editor font as 10 or 12
- Choose any one font from the following. Note that Mono space 10 is the system fixed with font.
Standars Symbols L
Serif
Vemana
Sans
Monospace
Mallige Normal
Loma Book
- I prefer the Classic color scheme. Just three more clicks, you can see all the color scheme and choose anything you like.
In the Plugins Tab
This sections makes the gedit a really powerful text editor. By default gedit comes with few standard plugins but you can install more plugins by installing the gedit-plugins package. If you don’t have root access, download the plugins from gedit.org and copy it to your ~/.gnome2/gedit/plugins folder. You can continue if you are lazy to download the plugins now. You can do that when a need arises. ;)
After this jump to section which you might be interested in.
Programming
- Bracket Completion ( You need to install this plugin)
A plugin which automatically adds closing brackets, single quotes and double quotes. Sometimes really useful and sometimes irritating. When you are typing the code, this is pretty useful but when you are editing a code already written, then this irritates you by adding closing brackets or quotes where you never intended to add.
- Code Comment ( You need to install this plugin)
A useful plugin for any programmer. You can comment or uncomment a selected block of code.
- Indent Lines
Very useful when you are specific about the indentation rules. Indents or un-indents a selected block of code. Very useful for python.
- Snippets
Want to insert a piece of code which just a few keystrokes, then this is for you. Yes lesser keystrokes than Ctrl C Ctrl V.
- External Tools
This gives amazing power to manipulate anything within gedit. I guess i can’t write about this here. Check out this post about External Tools. And don’t skip that post. It’s the BEST plugin for gedit.
HTML
Download the HTML tidy plugin from here and extract the folder to your ~/.gnome2/gedit/plugins folder. Enable the HTML Tidy plugin from the plugins tab in Preferences Window. HTML Tidy is an utility to clean up and pretty print HTML/XHTML/XML. Enable the Bottom Pane from the View Menu and you can clean up or check any HTML/XHTML/XML document.
The default configurations are enough to use Tidy. If you want to tweak it according to your needs, then Configure Plugin ( There are so many options :P )
Snippets
This is an useful plugin once you know what gives what. If you code for online programming contests like spoj, topcoder, acm or usaco and if your programming language is C++, then download this cpp.xml file and put it in your ~/.gnome2/gedit/snippets folder. Then press template[tab] or topcoder[tab] in a new cpp file.
Apart from that, if you are a web developer, then check out the Snippets for HTML and PHP. Once you remember the shortcuts which you need, they’ll make your life really easy.
Meet GNU/Linux 07
0After 2 successful editions of Meet GNU/Linux, which is conducted by
GLUG-T every year for beginners, the third edition of MGL is starting
on 27th July, at NITT.
A few updates about MGL’07 :
1. Guys are planning to release a beginner handbook, which would be
given out to them. The content is being edited in a wiki :
http://glugt-mgl.pbwiki.com/ Go ahead and edit when you are free.
2. This is GNU/Linux for dummies. So, if you have any of your
friend/relative interested in learning about it(in trichy), you can
ask them to contact suren who is co-ordinating
the event.
3. The co-ords are planning to release podcasts of the events, which
will be helpful.
Ideas, suggestion about how to organise the classes are welcome!
Seg Fault
1Wikipedia says,
A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).”
I have seen hundreds and hundreds of seg faults ;-) while coding for record module of LDTP and Spider SMS. But the one i saw yesterday was new, strange and fascinating. I am not sure whether i will be able to reproduce it again. The screenshot says why it is strange and fascinating :-) .
-bash-3.1$ man su
says
AUTHOR
Written by David MacKenzie.
REPORTING BUGS
Report bugs to <bug-coreutils@gnu.org>.
Maybe i should consider reporting this :P .
















