Linux Basics - Vim Manual
7 minute read
Note: Neovim has replaced vim on the cluster, however most (if not all) items here still apply. Load Neovim via the module system, like so
module load neovim
. It may also be useful to aliasnvim
tovim
, like soalias vim=nvim
. Thia alias can be added to your~/.bashrc
file for convenience.
Vim Manual
This is an extensive overview of vim features and operations.
Basics
vim <my_file_name> # open/create file with vim
Once you are in Vim the most important commands are i
, :
and ESC
. The i
key brings you into the insert mode for typing. ESC
brings you out of there. And the :
key starts the command mode at the bottom of the screen. In the following text, all commands starting with :
need to be typed in the command mode. All other commands are typed in the normal mode after hitting the ESC
key.
Modifiers
Moving Around
Lines
Multiple Files
Spell Checking
Syntax Highlighting
Undo and Redo
Deleting
Copy and Paste
Search
Replacements
Great intro: A Tao of Regular Expressions
Quick reference to some replacement techniques:
Uses ‘global’ command to apply replace function only on those lines that match a certain pattern. The ‘copy $’ command after the pipe ‘|’ prints all matching lines at the end of the file.
Command ‘args’ places all relevant files in the argument list (buffer); ‘all’ displays each file in separate split window; command ‘argdo’ applies replacement to all files in argument list (buffer); flag ‘e’ is necessary to avoid stop at error messages for files with no matches; command ‘update’ saves all changes to files that were updated.
Utilities
Matching Parentheses
- Place cursor on (, [ or { and type % # cursor moves to matching parentheses
Printing and Inserting Files
:ha # Prints entire file
:#,#ha # Prints specified lines: #,#
:r <filename> # Inserts content of specified file after cursor
Convert Text File to HTML Format
:runtime! syntax/2html.vim # Run this command with open file in Vim
Shell Commands in Vim
:!<SHELL_COMMAND> <ENTER> # Executes any shell command, hit <enter> to return
:sh # Switches window to shell, 'exit' switches back to vim
Using Vim as Table Editor
v
starts visual mode for selecting charactersV
starts visual mode for selecting lines`CTRL-V
starts visual mode for selecting blocks (use CTRL-q in gVim under Windows). This allows column-wise selections and operations like inserting and deleting columns. To restrict substitute commands to a column, one can select it and switch to the command-line by typing:
. After this the substitution syntax for a selected block looks like this:'<,'>s///.
:set scrollbind
starts simultaneous scrolling of ‘vsplitted’ files. To set to horizontal binding of files, use command:set scrollopt=hor
(after first one). Run all these commands before the:split
command.:AlignCtrl I= \t
then:%Align
This allows to align tables by column separators (here ‘\t’) when the Align utility from Charles Campbell’s is installed. To sort table rows by selected lines or block, perform the visual select and then hit F3 key. The rest is interactive. To enable this function, one has to include in the.vimrc
file the Vim sort script from Gerald Lai.
Settings
The default settings in Vim are controlled by the .vimrc
file in your home directory.
- see last chapter of vimtutor (start from shell)
- useful .vimrc sample
- when vim starts to respond very slowly then one may need to delete the
.viminf*
files in home directory
Help
- Online Help
- Find help on the web. Google will find answers to most questions on vi and vim (try searching for both terms).
- Purdue University Vi Tutorial
- Animated Vim Tutorial: https://linuxconfig.org/vim-tutorial
- Useful list of vim commands:
You can run a tutor from the command Line:
vimtutor # Open vim tutorial from shell, ":q" to quit
You can also get help from within Vim: