Tag Archives: Vim

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.

Flattr this!

Quote of the Day: Not All Change is Progress

There’s nothing wrong with helpful new features, but too often those helpful new features come with a price. They require relearning how to do things that you did reliably the day before. And even the most helpful features cease to be helpful if they require me to completely change my existing workflow.

Scott Gilbertson on the Joy of Vim.

With apologies to the Linux Luddites.

Flattr this!

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 "Path\To\PortableApps\gVimPortable\App\vim\vim80\vim.exe"

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

Flattr this!

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.

Flattr this!

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.

Flattr this!

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.

Flattr this!

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.

Flattr this!

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.

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

With thanks to @jk

Flattr this!