Showing posts with label Vim. Show all posts
Showing posts with label Vim. Show all posts

Sunday, August 23, 2015

Vim: Convert Markdown to HTML

This is pretty obvious, but I just started blogging, and I decided to dust off my old blogger account and use it for my posts, because, why not?

However, what has been bothering me is blogspots WYSIWYG editor, and while I can edit my posts in HTML, I am a bit too lazy to fight the editor. My process was to save rough drafts in Apache OpenOffice's native ODT format, then copy and paste the draft into blogspot's editor and 'polish' off the blog, switching between the Compose and HTML screens.

Then a thought hit me, why not blog in Markdown and convert to HTML, and post the HTML instead. Great idea, but how?

For almost a month now, I had made the switch from Emacs to Vim as my default editor and "IDE" of choice, so I did a search for available plugins and came across this awesome article, Vim Essential Plugin: Markdown to HTML

So I downloaded Markdown 1.0.1, unzipped it and placed it in my /usr/local/binand tested it out...

/usr/local/bin/Markdown.pl: not found

Whuh? Not found? Oh yes, I needed to ensure the file could find my perl, so I edited the first line in the file to reflect it's location.

$ which perl
/usr/local/bin/perl

I edited the first line of Markdown.pl so it looks like this #!/usr/local/bin/perl and I tested it from my terminal this time.

Markdown.pl --html4tags README.md

It's output was splendid, clean HTML. So I assigned my vim shortcut key, like so:

" Markdown to HTML
nnoremap <leader>m2h :%!/usr/local/bin/Markdown.pl --html4tags <CR>

I also downloaded the html2text.py script from Aaron Swartz html2text page.

That way I can now convert between the two formats within vim. Just follow the same steps as we did for Markdown.

As a sign off note, this is my first blog written entirely in Markdown, converted to HTML and published to Blogger!

Friday, July 24, 2015

Vim: Rebind Caps Lock And Escape

In this article I discuss how I managed to bind Escape to my Caps Lock key and Caps Lock to Escape, not only for my OpenBox environment, but also for my Virtual Terminal/Console.

I recently decided to finally give Vi/Vim a try. Previously, I thought it was too arcane and tried Emacs instead. I was an avid Emacs user and it became my editor and IDE of choice. So why did I decide to switch?
My decision came down to three (3) things:
1. Why not?
2. Striving to be more productive (i.e. lazier...i.e. less keystrokes)
3. Vi is installed by default on Unix, so an advanced editor is always ready. So are ee, ed and a whole range of others, but I like the challenge and wanted to see what the rage was all about. Yes Emacs came with Mac OS X by default albeit an older version, so I still had to install the latest. On FreeBSD? pkg install emacs is your friend.
While using Emacs on OS X, I could easily remap the keybindings for Caps Lock to Control, in fact, it was a builtin feature of the OS. It's a bit more work to get it done on FreeBSD, so here goes.

OpenBox setup:

This was taken from the vim wikia:

  1. vi ~/.vimkeyswapper – you can name the file anything you want, I just wanted the easy reminder everytime I looked at it. This was easier to understand at a glance over '.xmodmaprc'.
  2. Edit the file to include the following contents:
    ! Swap CAPS LOCK and ESC
    remove Lock = Caps_Lock
    keysym Escape = Caps_Lock
    keysym Caps_Lock = Escape
    add Lock = Caps_Lock
  3. Edit either your ~/.config/openbox/autostart if you are using a login manager or your ~/.xinitrc if, like me you only need to start X when you really need to
    xmodmap ~/.vimkeyswapper
  4. Restart X, or reboot just to make sure it takes effect.

Console setup:

Alright, be careful here. We're going into the file system, which requires root privileges. Ironically, I found the original solution here.
  1. We need to decide which keyboard mapping we are using. So you'll need to install misc/kbdscan if you don't already have it installed.
    $ cd /usr/ports/misc/kbdscan && sudo make install clean
  2. Please note, you have to be in the console to be using this, so you can't be logged into X and use your terminals. So ALT+F1 or ALT+F(n) to whichever console you like and login.
    $ kbdscan
    Press and release Caps Lock and make a note of which number appears, for me it was 58
    Press and release Esc, the number I got was 1.
  3. Okay, you may have to do some digging around to find which files match the numbers you got.
    $ cd /usr/share/syscons/keymaps && ls
    Based on your locale, language or keyboard setup, it may be different for you. The file that matched my bindings were us.iso.kbd. [1]
    $ sudo cp us.iso.kbd us.vim.kbd
  4. Edit the file, so it should look something like this:
    # $FreeBSD: releng/10.1/share/syscons/keymaps/us.iso.kbd 74119 2001-03-11 23:41:19Z ache $
    # alt
    # scan cntrl alt alt cntrl lock
    # code base shift cntrl shift alt shift cntrl shift state
    # ------------------------------------------------------------------
    000 nop nop nop nop nop nop nop nop O
    001 clock clock clock clock clock clock debug clock O
    ...
    058 esc esc esc esc esc esc esc esc O
    [2]


    I don't know if it made a difference or not, but I realigned the text before saving the file.
  5. Add this to your /etc/rc.conf file: keymap="us.vim.kbd"
  6. Reboot: # reboot OR $ shutdown -r now
  7. And there you have it, if done correctly, your Caps Lock will now be your new Esc key. Happy coding![3]
[1] – On a side note, they already have us.emacs.kbd, no fair to us vim users :-)
[2] – Ellipses denotes more content, I just did this to highlight the relevant changes. Please don't remove anything from this file.
[3] – I am not sure if this affects console only or the X environment as well, making the xmodmap command unneccessary.