Editing RPGLE source members with Vim

I am very fond of the Vim text editor and will use it, out of preference, whenever possible. This includes writing RPG (and, obviously, RPGLE) programs on the IBM i. The only downside here is that, while Vim supports syntax highlighting for a multitude of languages, RPG isn’t one of them.

For a long time, I have relied on the syntax files written by Martin Rowe back in 2014. These, however, are getting more than a little long in the tooth. So, since I have a little time on my hands, I have started putting together a new set of syntax files which, hopefully, will be able to keep up with any new developments.

It’s all very early days so far, but you can find the Vim RPG Syntax highlighter here.

Feel free to download it and use it. And if you have any improvements to make or suggest, I would love to hear from you.

Portable Vim in Powershell

Because I have finally been able to replace my work laptop.

Because I’m allowed to install software on a server, but not on the laptop in front of me.

Because corporate security policies are insane.

Because I need a decent text editor.

Because PortableApps is a lifesaver.

I have talked about getting Vim to work in Powershell in the past, but this time around I need to get the portable version of Vim working in Powershell.

The first step, of course, is to download the PortableApps platform and install gVim. Handily, this brings the Vim binary along with it so you just need to point your Vim alias to this.

This is done by editing your $profile so that the following line is included:

set-alias vim "PathToPortableAppsgVimPortableAppvimvim80vim.exe"

Everything else appears to be working as expected, so I can actually get some work done now.

Prettify XML with Vim

File under quick and dirty, but works for me.

The issue is that I have an utterly unreadable XML file in front of me. Not only is there no indentation, I don’t even have any line breaks.

To format it for readability, first insert line breaks

:%s/>r</g

Then load and apply the XML indent file

:set filetype=xml
:filetype indent on

And apply it

gg=G

There are probably better ways of achieving the same end, but as a quick fix, this works for me.

Spellchecking in Vim

Here’s something I didn’t know.

I have used Vim to write text files in the past and, when I do so, I use aspell check to catch my multitude of typing errors. It turns out that Vim is also able to support spell checking, so I no longer need to keep dropping back to the command line.

Joe ‘Zonker’ Brockmeier goes into detail, but I am reposting the highlights here so that I can easily find them again.

:setlocal spell spelllang=en_gb turns on the spell checker.
:set nospell turns it off again.

]s and [s moves to the next and previous spelling errors respectively.
z= brings up a list of alternatives for the typo under the cursor.
zg adds the word to the dictionary.
zw allows you to mark a word as incorrect.

This post was entirely written in Vim.

Vim in Powershell

Because, why not.

And because I am stuck in Windows Land while at work.

And because I found myself playing around with Windows Power Shell. It’s not as good as Bash by a long stretch, but it is a significant improvement on the old Windows cmd almost-terminal. And, because I like to be able to move from task to task, without having to constantly switch windows, I thought I would give it a try.

The first thing to do, of course, is install a decent text editor. This is easy, a Windows binary exists for Vim so I downloaded and installed it. Then comes the fun bit.

Getting it to work in Powershell isn’t exactly difficult, but there are a few hoops that need to be jumped through and no clear overview of what and where these hoops happen to be. And that’s why I am posting this.

First, you need to allow scripts to run. This is not enabled by default because, obviously, if you have a scripting environment, the last thing you are going to want to do is execute scripts. Fortunately, this is easily checked and altered by means of the execution policy.

To see the current state of your execution policy, use:

Get-ExecutionPolicy

This will probably tell you it’s restricted, in which case you need to tell it to run scripts by typing (or pasting):

Set-ExecutionPolicy RemoteSigned

(You can also set the execution policy to AllSigned or Unrestricted, but RemoteSigned is sufficient for now.)

Next, you need a Powershell profile (why is it that Windows terms all feel like they should be wrapped in a blink tag?), which can be created by typing (or pasting):

new-item -path $profile -itemtype file -force

And now you need to edit your profile to add a Vim alias. You’ll need to do this in Notepad:

notepad $profile

And then paste the following:

set-alias vim "C:/Program Files (x86)/Vim/Vim74/./vim.exe"

# To edit the Powershell Profile
# (Not that I'll remember this)
Function Edit-Profile
{
    vim $profile
}

# To edit Vim settings
Function Edit-Vimrc
{
    vim $HOME_vimrc
}

(Obviously, you will need to amend the Vim path name to match the reality on your system.)

And you’re done.

I’m not sure how much use I will actually get out of Vim at work as my workflow often involves copying and pasting text from one application to another – and for that Notepad++ is probably still a better fit. However, having an additional tool certainly does no harm and I can certainly see cases where having the power of Vim available will be useful. For now, I will use both and see how each fits into my workflow.

And finally, this post was brought you you with the help of Microsoft, Marlon Abastar and Julian Kay.

Vim syntax highlighting for the IBM i

As you may have noticed, some of the personal projects I have mentioned on here have been written for the IBM i. I generally develop these locally in Vim and then copy the source files to a server so that I can compile and test them as and when I have a chance.

I like Vim. When I want to just sit down and get something down, Vim provides a distraction-free environment with a powerful collection of features. One of these features is syntax highlighting, and Vim comes with syntax files for a host of languages as standard. Unfortunately, RPG is not one of the languages supported out of the box.

Inevitably, however, it didn’t take much time with Google to discover that someone else had the same thought as me some time ago. So I must say thank you to Martin Rowe of DBG/400 for this rather gorgeous collection of syntax files.

Vim :TOhtml

File this under New To Me. The Vim text editor includes a handy command to convert code to HTML.

The command, unsurprisingly, is: :TOhtml

This generates a formatted HTML file which looks like this:

And if I paste this code straight into this post (with minimal editing to make the colours fit a little better with the blog theme), I am reminded the Python style guide recommends a maximum line length of 79 characters.

 



 1 #! /usr/bin/python
 2 """ XKComplete: A date reversing completeness counter inspired by XKCD
 3 http://xkcd.com/1017/
 4 
 5 Copyright (C) 2012 Paul Pritchard
 6 
 7 This program is free software; you can redistribute it and/or modify
 8 it under the terms of the GNU General Public License as published by
 9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>."""
19 
20 from __future__ import division
21 import datetime
22 import math
23 
24 def xkcdate(percentage_complete):
25     """ Returns the percetange completed, either as a date or a year"""
26     today = datetime.date.today()
27     completion = percentage_complete / 100
28     days = float(365.25 * (math.exp(3+20.3444*(completion)**3) - math.exp(3)))
29     try:
30         date = (today - datetime.timedelta(days)).strftime("%A, %d %B %Y")
31     except:
32         dateval = datetime.datetime.today().year - int(days / 365.25)
33         if dateval < 0:
34             dateval = 0 - dateval
35             adbc = " BC"
36         else:
37             adbc = " AD"
38         date = str(dateval) + adbc
39 
40     return date
41 
42 if __name__ == "__main__":
43     percentage_complete = 0
44     while percentage_complete < 100:
45         percentage_complete = input("Enter percentage complete: ")
46         print xkcdate(percentage_complete)

With thanks to @jk