version 0.6.0
Loading...
Searching...
No Matches
Vim usage

The following guidelines will help you to setup and use your Vim text editor effectively when working with NTS files and navigating into the code.

Introduction to Vim philosophy

Vim is a modal text editor, meaning it operates in different modes, each tailored to a specific type of interaction:

  • Normal mode: default mode for navigation and editing commands (e.g., copy, paste, delete).
  • Insert mode: for typing and editing text like a traditional editor.
  • Visual mode: for selecting text blocks.
  • Command-line mode: accessed via : to run commands like saving or opening files.

Understanding the modes is key to using Vim efficiently.

Switch between modes:

  • Press i, a, o to enter Insert mode.
  • Press Esc to return to Normal mode.
  • Press : in Normal mode to enter Command-line mode.
  • Press v, V, or Ctrl-v for Visual modes (character, line, block).

If you're new to Vim, we strongly encourage to run vimtutor from a terminal. It's an excellent interactive guide to learn the basics and use Vim effectively in few minutes.

Basic Vim usage and navigation

Moving around in files

  • h, j, k, l: move left, down, up, right.
  • w, b: move forward/backward by word.
  • 0, ^, $: move to beginning of line, first non-whitespace, end of line.
  • gg, G: go to top or bottom of file.
  • :n: go to line number n.

Basic editing commands

  • dd: delete line.
  • yy: copy (yank) line.
  • p: paste below.
  • u: undo.
  • Ctrl+r: redo.

Useful file commands

  • :w: save.
  • :q: quit.
  • :wq: save and quit.
  • :e filename: open file.
  • :tabe filename: open a new file in a new tab (tabpage). Navigate between tabs with gt (next tab) and gT (previous tab).
  • :vsp filename: open file in vertical split.
  • :sp filename: open file in horizontal split.
  • Ctrl-w h/j/k/l: switch between split windows.
  • :s/toto/titi/gc: replace all occurrences of toto by titi in the whole file (%) with confirmation (c asks before each substitution).

Search

  • /pattern: search forward.
  • ?pattern: search backward.
  • n / N: repeat search in same/opposite direction.
  • :noh: remove highlighting.

Visual mode tips

Vim's Visual mode is extremely powerful for selecting and editing text blocks:

  • v: enter character-wise visual mode.
  • V: enter line-wise visual mode.
  • Ctrl-v: enter block-wise (column/rectangular) visual mode.

Once in block mode (Ctrl-v), you can:

  • Use arrows or hjkl to select a block.
  • Press I followed by a string and <Esc> to insert that string at the beginning of every selected line.
  • Press A followed by a string and <Esc> to append text at the end of each selected line.
  • Use d, y, p to delete, yank, or paste block selections.

This makes it easy to, for example, insert a comment character at the start of multiple lines or align columns of text.

Notus with vim

To enable proper syntax highlighting and filetype detection for Notus files in Vim, follow these steps:

mkdir -p "${HOME}/.vim/{ftdetect,syntax}"
cp tools/vim_syntax/ftdetect/nts.vim "${HOME}/.vim/ftdetect"
cp tools/vim_syntax/syntax/nts.vim "${HOME}/.vim/syntax"

This setup allows Vim to automatically detect .nts files as Notus-specific and apply custom syntax highlighting.

Configuration of ~/.vimrc

Replace tabulation with spaces (4) is necessary to write NTS files with uniform indentation. You can use a custom configuration file (~/.vimrc) to define your preferred settings.

This file is automatically loaded by Vim at startup and defines editor behavior (indentation, colors, plugins, etc.).

Below is a sample .vimrc file you can use as a base configuration:

"enable syntax highlighting
syntax enable

" show line numbers
set number

" set tabs to have 4 spaces
set ts=4

" indent when moving to the next line while writing code
set autoindent

" expand tabs into spaces
set expandtab

" when using the >> or << commands, shift lines by 4 spaces
set shiftwidth=4

" show a visual line under the cursor's current line
set cursorline

" show the matching part of the pair for [] {} and ()
set showmatch

" enable highlighting search results
set hlsearch

" enable incremental search
set incsearch

" draw a vertical line at column 80
set colorcolumn=80

" install vim-plug if it's missing
if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

" plugin section starts here
call plug#begin('~/.vim/plugged')

" EditorConfig for consistent code formatting
Plug 'editorconfig/editorconfig-vim'

" Status line customization
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'

" Comment/uncomment code easily
Plug 'preservim/nerdcommenter'

" Fuzzy file finder
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'

call plug#end()

" disable fancy fonts in airline
let g:airline_powerline_fonts = 0

" use 'elflord' color scheme
colorscheme elflord

" customize tab highlight color
highlight TabLineSel ctermfg=Red ctermbg=Yellow

" nerdcommenter custom delimiters for .nts files
let g:NERDCustomDelimiters = {
    \ 'nts': { 'left': '#' },
\ }

" remove all trailing whitespace by pressing F7
nnoremap <F7> :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar><CR>

Plugin notes

  • vim-plug is used to manage Vim plugins easily.
  • fzf enables fast fuzzy searching in files and directories.
  • NERDCommenter simplifies code commenting across languages.
  • vim-airline provides a beautiful and customizable status line.
  • editorconfig enforces consistent indentation and file formatting rules across editors.

Ressources

Find here a list of very useful Vim shortcuts and here an HTML version of the Vim help pages.