A lightweight Fortran pretty-printer (indenter) for free-form source code.
fpretty is a simple command-line tool that automatically indents modern, free-form Fortran code. It reads from standard input or a file and writes the formatted code to standard output.
fpretty is built using the Fortran Package Manager (fpm). You'll need to have fpm installed to build the project.
-
Clone the repository:
git clone https://github.com/jmriff/format-fortran.git cd fpretty -
Build and install with
fpm:fpm install
fpretty can be used in several ways:
-
From standard input:
cat my_fortran_file.f90 | fpretty -
With a single file:
fpretty my_fortran_file.f90
The formatted output will be printed to the console. To save it to a new file:
fpretty my_fortran_file.f90 > formatted_file.f90 -
To overwrite a file in-place (use with caution):
fpretty my_fortran_file.f90 > temp.f90 && mv temp.f90 my_fortran_file.f90
You can configure fpretty to run automatically whenever you save a Fortran file in Vim or Neovim. Add the following to your .vimrc or init.vim:
" Run fpretty on Fortran files automatically on save
function! FprettyFormatAndWrite()
let l:curpos = getpos('.')
let l:winview = winsaveview()
silent %!fpretty
call winrestview(l:winview)
call setpos('.', l:curpos)
write!
edit!
endfunction
augroup fpretty_autocmd
autocmd!
autocmd BufWriteCmd *.f,*.f90,*.for,*.F,*.F90,*.FOR call FprettyFormatAndWrite()
augroup END- Not a full parser:
fprettyis based on heuristics and may not correctly handle all complex one-line constructs, preprocessor directives, or fixed-form source code. - It is designed for free-form Fortran source only.
Feedback, recommendations, issues, and code contributions are welcome! Please feel free to open an issue or submit a pull request.
