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.

No comments:

Post a Comment