Monday, February 20, 2012

Vim Key Map For Fortran 77 Continue Line Character

I started learning Fortran because the code I would be working with in a research project was written in Fortran 77. As a result, the Fortran I learned was a fixed column format Fortran based on punch cards. The punch cards had 80 columns with the following format:

1-5: For statement labels, essentially line referencing.
6: For a continuation character to continue a previous  line (this is what we're interested in)
7-72: For statements, basically the actual code
73-80: Ignored by the compiler.

Since this was how I  learned Fortran, it is how I continue to use it. Eventually I got tired of manually adding the ampersand in column 6 of a line that is a continuation, so I set out to make the task much quicker.

I identified 2 different cases in which I would need to add the continuation character. The first was if I was typing a line that reached column 72. In this case I would be in insert mode and would like to press the key(s) I have the shortcut mapped to, and have the ampersand in the correct place on the next line with my cursor following it in insert mode to allow me to continue typing the line. To do this I used a key mapping as it would be quicker that the abbreviation I described here.

Key mappings in Vim are very easy to implement. The syntax is as follows:
:map key_with_mapping sequence_of_keystrokes_to_be_mapped
A simple example would be:
:map <C-g> o<esc> 
This will, when Ctrl-g is pressed, press 'o' and then 'Esc' which would open the next line in insert mode and the exit insert mode.

For my case, this is what I used:
 imap <C-a> <esc>o<home><space><space><space><space><space>&
Since I put it in my .gvimrc, I didn't need the leading colon. The imap just means that it only works in insert mode, whereas a vmap would work in visual mode. So, when I press Ctrl-a in insert mode it escapes to visual mode, opens the next line in insert mode, moves to the first column of the line, enters 5 spaces, then the ampersand. I'm left with the ampersand in the correct place with the cursor in column 7 in insert mode ready to continue my line of code.

More information on Vim key mappings can be found if you type:
:help key-mapping
while in visual mode in Vim.

I'll add my second case for adding the continue line quickly in the near future.

Tuesday, February 7, 2012

Commenting Multiple Lines in Vim

I do a bit of programming in Fortran and periodically I find myself wanting to comment out a block of code for whatever reason. In Vim this is relatively easy. Just highlight the lines you would like to comment and type:
:s/^/\!
The s is for subtitute, the ^ character designates the front of each line, and the exclamation (which has to be escaped with the \ character) is the comment character for Fortran. This can be replaced by any comment character the language you may use requires, but not all characters will need to be escaped like the exclamation does. Similarly to uncomment selected lines:
:s/^/\!/
 Now, I find this tedious and frustrating to type in and/or remember. To streamline the commenting and uncommenting process, I make use of abbreviations. Essentially, you define an abbreviation and what should be substituted for that abbreviation. When you type the abbreviation, Vim will automatically substitute it for you. The syntax is as follows:
:ab <abbreviation> <unabbreviated substitution>
where ':ab' is the command  (you type this in the Vim window), <abbreviation> is what you type, and <unabbreviated substitution> in what replaces the abbreviation. Also note that spaces in the unabbreviated substitution are acceptable. You also do not inclue the '<>' characters.

Here is how it would work with commenting multiple lines:
:ab com s/^/\!
:ab ucom s/^/\!/
Now, all you need to type in once the lines are selected is,
:com
:ucom
to comment or uncomment those lines.

This method will only work for the particular instance of Vim that you are working in, meaning you would need to type this at the start of each session. An easier method would be to place the commands in your .vimrc or .gvimrc file, without the leading colon, and they will be defined every time you start up Vim.