# Introduction to Vim¶

Vim is a text editor for the command line that you will use often when following these instructions. Vim can be very intimidating for new users, but for the purposes of these instructions you only need to know a few commands.

To start Vim, type vim /path/to/file on the command line to edit a new or existing file. Vim has two modes that you will use regularly: command mode and insert mode. Command mode allows you to execute commands like saving and quitting. Insert mode lets you actually edit text.

When you first start Vim you will be in command mode, so you cannot immediately begin editing text. You must first switch into insert mode by pressing i. When you do this, you should notice the last line in the terminal window says “-- INSERT --“. When in insert mode, you can type text and it will appear on screen as you would expect. Press Esc to return to command mode, and the insert mode indicator at the bottom of the terminal window will disappear.

If you press the wrong keys in command mode, unexpected and confusing things may happen. If you get stuck in some other mode, press Esc a few times to return to command mode. If that fails, try typing :q followed by the Enter key.

Below is a list of useful commands. Notice that commands beginning with a colon (:) or slash (/) appear on the bottom line in the terminal window when typed in command mode.

• Save and quit: In command mode, type :w followed by the Enter key to save (write) the file. Type :q followed by the Enter key to quit Vim. If you have made unsaved changes to the file before trying to quit, you will get an error, “No write since last change”. To quit and discard unsaved changes, type :q! followed by the Enter key.
• Undo and redo: In command mode, press u to undo your last action. Press Ctrl + r to redo.
• Search: In command mode, type /, followed by a word or phrase, followed by the Enter key to begin searching for the phrase. Press n to cycle to the next instance of the phrase or N to cycle to the previous instance.
• Line numbers: In command mode, type :set number followed by the Enter key to turn on line numbering. Use :set nonumber to turn it off again.
• Jump: In command mode, press gg to jump to the top of the file. Type a line number before this command to jump to that line. Press G to jump to the end of the file.
• Paste: You can use your terminal application’s normal paste command to paste text while in insert mode. However, sometimes when you paste a multi-line block of text into Vim from another source (such as from this document), the pasted content will be auto-indented or auto-commented in undesirable ways. To prevent this behavior, in command mode, type :set paste followed by the Enter key before entering insert mode and pasting.
• Mouse: In command mode, type :set mouse=a followed by the Enter key to enable interaction with the text using your mouse. This will allow you to click anywhere to place the cursor or to select blocks of text.

## Vim Configuration File¶

The following instructions will customize your Vim configuration so that syntax highlighting and line numbers are turned on by default. It will also enable interaction with text using the mouse, so you can place the cursor by clicking with the mouse. It also enables a “persistent undo” feature that allows Vim to retain the history of edits for a file after it is closed, so you can undo or redo edits even after closing and reopening a file.

The persistent undo feature depends on the existence of a directory. Create this directory now:

mkdir -p ~/.vim/undodir


Finally, download and install the .vimrc configuration file:

wget -O ~/.vimrc https://neurowiki-docs.readthedocs.io/en/latest/_downloads/.vimrc


If you are curious about the contents of .vimrc, you can view it here:

.vimrc

Direct link

" An example for a vimrc file.
"
" Maintainer:   Bram Moolenaar <Bram@vim.org>
" Last change:  2002 Sep 19
"
" To use it, copy it to
"     for Unix and OS/2:  ~/.vimrc
"             for Amiga:  s:.vimrc
"  for MS-DOS and Win32:  $VIM\_vimrc " for OpenVMS: sys$login:.vimrc

" When started as "evim", evim.vim will already have done these settings.
"if v:progname =~? "evim"
"  finish
"endif

" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible

" allow backspacing over everything in insert mode
set backspace=indent,eol,start

if has("vms")
set nobackup          " do not keep a backup file, use versions instead
else
set backup            " keep a backup file
endif
set history=50          " keep 50 lines of command line history
set ruler               " show the cursor position all the time
set showcmd             " display incomplete commands
set incsearch           " do incremental searching

" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
" let &guioptions = substitute(&guioptions, "t", "", "g")

" Don't use Ex mode, use Q for formatting
map Q gq

" This is an alternative that also works in block mode, but the deleted
" text is lost and it only works for putting the current register.
"vnoremap p "_dp

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif

if has("gui_running")
" turn off the toolbar
set guioptions-=T
set columns=84
endif

" Only do this part when compiled with support for autocommands.
if has("autocmd")

" Enable file type detection.
" Use the default filetype settings, so that mail gets 'tw' set to 72,
" 'cindent' is on in C files, etc.
" Also load indent files, to automatically do language-dependent indenting.
filetype plugin indent on

" Put these in an autocmd group, so that we can delete them easily.
augroup vimrcEx
au!

" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78

" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
\ if line("'\"") > 0 && line("'\"") <= line("\$") |
\   exe "normal g\"" |
\ endif

augroup END

else

set autoindent                " always set autoindenting on

endif " has("autocmd")
set tabstop=4
set shiftwidth=4
color desert
set guifont=Dejavu\ Sans\ Mono\ 12
hi Normal guifg=Green guibg=Black
hi Constant guifg=Yellow
hi Statement guifg=Yellow gui=none
hi Type guifg=Yellow gui=none
hi Special guifg=SeaGreen
hi Search guibg=DarkBlue

"set lines=46
set expandtab
"setlocal spell spelllang=en_us
set number
set mouse=a

function! s:insert_gates()
let gatename = substitute(toupper(expand("%:t")), "\\.", "_", "g")
execute "normal! i#ifndef " . gatename
execute "normal! o#define " . gatename . " "
execute "normal! Go#endif /* " . gatename . " */"
normal! kk
endfunction
autocmd BufNewFile *.{h,hpp} call <SID>insert_gates()

"Spell checking
"set spell spelllang=en_us

" persistent undo support
" " note: you need to mkdir ~/.vim/undodir before this will work
set undofile " turn on persistent undo's
set undodir=~/.vim/undodir " where to store the undo files
set undolevels=1000  " max number of changes to undo
set undoreload=10000 " max number of lines to save for undo

" allow switching to a different buffer without saving
set hidden
`