User Tools

Site Tools


tools:vim

Plugin for the Vim editor

  syntax highlighting and editing features for CP2K input files.

Plugin setup

Syntax highlighting for CP2K input files can be enabled with the (g)vim editor using the vim plugin file cp2k.vim. Download the file cp2k.vim and copy it to the folder ~/.vim/syntax in your home directory, e.g. using

mkdir -p ~/.vim/syntax
wget -O ~/.vim/syntax/cp2k.vim http://manual.cp2k.org/trunk/cp2k.vim

In addition create a file with the name filetype.vim in the ~/.vim folder containing the following lines

if exists("did_load_filetypes")
   finish
endif
augroup filetypedetect
   au! BufNewFile,BufRead *.inp set filetype=cp2k
   au! BufNewFile,BufRead *.restart set filetype=cp2k
augroup END

which causes that all files with the name extensions .inp and .restart will be considered as CP2K input files by the Vim editor. You may change the extension due to your favored settings.

Alternative for pathogen.vim/vim-plug users

For developers using pathogen or similar VIM runtime path managers, a separate vim-cp2k repository is provided and the setup procedure is even easier:

git clone https://github.com/cp2k/vim-cp2k.git ~/.vim/bundle/vim-cp2k

Usage Tips

Syntax folding

The syntax folding based on the CP2K input line indentation can be activated in the ~/.vimrc file by

au FileType cp2k setlocal foldlevel=99 foldmethod=indent

. The following Vim editor commands might be useful for large CP2K input files:

  • za :: Toggle the folding of the current fold level
  • zA :: Toggle the folding of all fold levels recursively
  • zM :: Close all folds in the current buffer
  • zR :: Open all folds in the current buffer

Automatic indenting

An automatic indenting of the CP2K input lines while typing in insert mode is activated by default. The number of blanks used for the indentation can be defined in the ~/.vimrc file by

au FileType cp2k setlocal shiftwidth=1 tabstop=1

which changes the indentation only for the specified file type cp2k, whereas

set shiftwidth=1 tabstop=1

will change the indentation for all file types and buffers.

The indentation can also be changed at any time during a Vim editing session by

:set shiftwidth=2 tabstop=2

Likewise, the automatic indenting can be switched off by setting

:set shiftwidth=0 tabstop=0

The vim indent command “=” can be used to properly indent a section (region) of a file or even the full file, e.g. by typing “=G” when the cursor is positioned at the first line of the input file.

Vim support for the CP2K code development

Vim may also support the code development by folding comment line blocks like in the headers of each FUNCTION or SUBROUTINE which will give a better focus on the relevant source code. The following vim command

:set foldmethod=expr foldexpr=getline(v:lnum)[0:8]=='!>\ \\param'

will just fold all

!> \param

comment lines and

:set foldmethod=expr foldexpr=getline(v:lnum)[0:1]=='!>'

will fold even all lines starting with

!>

These settings are automatically applied to all Fortran source files on edit if such a line

au FileType fortran setlocal shiftwidth=3 tabstop=3 foldmethod=expr foldexpr=getline(v:lnum)[0:8]=='!>\ \\param'

is added to the .vimrc file in the home directory. shiftwidth and tabstop define the preferred indentation of code blocks starting with IF or DO for instance. As described above for the input editing, the following vim command might be useful:

  • zR :: Open all folds in the current buffer
  • zM :: Close all folds in the current buffer

whereas

  • zo :: Opens just the fold under the cursor
  • zc :: Closes the fold under the cursor
tools/vim.txt · Last modified: 2023/11/10 15:31 by krack