Yasnippet example for ssh hosts configuration

I manage or help managing a lot of unix servers around the net, they all have one thing in common: they use ssh, but several different hostnames, IP address and usernames. Sometimes I have two or three usernames for a single box, for example: personal username, deploy and root.

To avoid having to remember all those different configurations I do a heavy use of the ~/.ssh/config file, to define host aliases. Here are some examples:

# Defined in ~/.ssh/config
Host example
    Hostname   example.com
    User       root

Host example-deploy
    Hostname      example.com
    User          deploy
    FordwardAgent yes

Host example-admin-console
    Hostname      192.168.100.230
    User          admin
    Port          2330

Then I can type in console:

$ ssh example
$ ssh example-deploy
$ ssh example-admin-console

Here is a simple yasnippet to add new host aliases in ~/.ssh/config file when using emacs.

  # -*- mode: snippet -*-
  # contributor: Jorge Calás Lozano <calas@railsdog.com>
  # name: ssh-host
  # key: host
  # group: ssh
  # expand-env: ((yas/indent-line 'fixed) (yas/wrap-around-region 'nil))
  # --
  Host ${1:short-name}
       Hostname           ${2:hostname}
       User               ${3:username}
       ForwardAgent       ${4:$$(yas/choose-value '("yes" "no"))}
       ${5:Port               ${6:22}}
  ${7:host}$0

This is very small set of the options you can set. If you want to see what else you can configure in you ssh config file go ahead and type man ssh-config in your console.

Using yasnippets in markdown mode.

I’ve found that when using markdown-mode yasnippets’s TAB completion doesn’t work. It’s just because TAB key is bind to markdown-cycle function. To change this behaviour I use the following snippet in my .emacs file:

;; This goes into my .emacs file
(defun markdown-unset-tab ()
  "markdown-mode-hook"
  (define-key markdown-mode-map (kbd "<tab>") nil))
(add-hook 'markdown-mode-hook '(lambda() (markdown-unset-tab)))

Now I can expand yasnippets using TAB inside markdown-mode.

My first post on github pages

This is my very first post using github_pages and jekyll. Just a simple test.

Just added disqus comments too.