Example of the status line, notice the "OBX-3" at the bottom.

vim is my editor of choice, but, unfortunately, currently there isn’t anything special that vim does for HL7 messages. The one thing that I miss the most is not knowing which segment/field the cursor is currently in. Knowing that vim is very customizable, I set out to see if I could add this feature myself. What I ended up was two functions with about 35 lines of code.

The first one SetHL7StatusLine changes the vim statusline to call the second function, which looks at the current line and figures out the HL7 segment name and field number, and then outputs that to the statusline. Below are the instructions if you would like to add this functionality.

  1. Copy the code below and paste it in your vimrc file (:e $MYVIMRC)
  2. Make sure that status line is visible (:set statusline=2)
  3. Once you have the message in vim, call the function to set the appropriate variables (:call SetHL7StatusLine())
  4. Enjoy! 🙂

I realize that this is a lot of work, but right now there isn’t enough functionality to turn this into a vim plugin.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function! SetHL7StatusLine()
  set statusline=%<%f\ (HL7:\ %{HL7SegmentInfo()})\ %h%m%r%=\ %-14.(%l,%c%V%)\ %P
endfunction
" This function adds the segment name and field number to the status line.
function! HL7SegmentInfo()
  let line=getline(".")
  let pieces=split(line,"|")
  let cursorAt = col(".")
  if len(pieces) == 0
    return "No Segment"
  elseif (len(pieces) == 1) || (cursorAt <= strlen(pieces[0]))
    if strlen(pieces[0]) > 3
      return "Invalid segment?"
    else
      return pieces[0]
    endif
  endif
  let seg=pieces[0] . "-"
  let position=strlen(pieces[0])
  let segNum=0
  for index in range(1, len(pieces) - 1)
    let segNum += 1
    let position += 1
    let piece = pieces[index]
    if cursorAt <= (position + strlen(piece))
      return seg . segNum
    endif
    let position += strlen(piece)
  endfor
  " If the last piece was the separator (|) then VIM doesn't treat that
  " as a separate piece so we have to account for this special case.
  if strpart(line, strlen(line)-1, 1) == "|"
    let segNum += 1
  endif
  return seg . segNum
endfunction
Back to blog...