Scripts/Pretty Print

From J Wiki
Jump to navigation Jump to search

This script is a *nix shell script to format J source file.


jpp [files]

If file names are given, it will format those files in-place. Otherwise it will read from stdin and write formatted output to stdout.


After the file jpp downloaded, at command prompt type

mv jpp && chmod u+x jpp && mv jpp $HOME/bin

Assuming $HOME/bin is in $PATH or move it to other directories as appropriate.


The script expects your jconsole is symlinked to /usr/bin/ja, change the #! (first) line to the path of your jconsole or its symlink.

By default jpp will remove all trailing blanks during formating, remove the overriding commentline verb to preserve trailing blanks.

Trouble Shooting:

If it raises error on undefined FORMAT_j_, either update base library from svn or do change as that suggested in jpp file.

If jpp somefile gives error message saying can not find script ...addons/jpp.ijs, either update base library from svn or use the name instead of jpp.

Formating all files under a directory recursively

change directory to the top level directory then type

find . -name '*.ijs' | xargs jpp

or if pathnames contain spaces

find . -name '*.ijs' -print0 | xargs -0 jpp

Integration with vim:

Add the following to ~/.vimrc

" format j script
nmap jpp :call Jpp()<CR>
command! Jpp call Jpp()
function! Jpp()
  let linenumber = line(".")
  if 0 < v:shell_error
    if 127 == v:shell_error | let linenumber = getline('$') | endif
    silent undo
    exe linenumber
  elseif 0 == v:shell_error
    exe linenumber

Typing jpp or :Jpp in normal mode to format the buffer content. If error occurs during formating, the cursor line will be positioned to the line where error ocurred.

The ability of jumping to the error line depends on the shellredir setting in vim. The default should work for bash. For other shells and in case it does not work, type :help shellredir to see how to make filter read both stdout and stderr.

Contributed by Bill Lam.