vi cheat sheet


vi is a text editor with somewhat strange usage ways. The reason for using vi are many. Some argue it is so fast to use, some maybe do it because it is retro and cool. 

I mostly use it because it is a bit of an industry standard and is often available when other editors are not.
Like in open source router software with limited storage space like Tomato. Sometimes you are not permitted to install other editors, like on web hosts and so on.

So, you should learn vi because it is available, that's my view of it.

vi versions

There are two versions of vi, vi and vi improved (vim).

If you have the chance install vim, because it has more functionality and other improvements.

apt-get install vim 

Much of the info here probably applies to vim, you have to find out for yourself what works for you in your environment.

[key] shows when a named keyboard key should be pressed, like [insert] for insert key, regular character keys are written as they are, like i, w and so on.

vi modes

vi has two modes, insert/edit mode and command mode.

In insert mode you write text like in a normal editor.

In command mode you work with commands, this is the most confusing mode for newcomers as regular writing does not work. vi starts in this mode by default.

Switch to insert mode:

Switch to command mode

command mode

Undo changes

Move left, up, down, right

NOTE!!! The arrow keys may sometimes not work when keyboard layout is not matching the locale and so on, resulting in garbage characters. Then you use the letter keys.

Write current file


Force quit without saving

Write and quit

Remove current whole line:

:/text to find/

Switch to hex editor (actually streams through xxd program...)

Exit from hex editor
:%!xxd -r

vim configuration

vim has a config file where you can put in your own customization. 

It is located in your user directory:

It is most likely not there, so you may create it yourself.

Here is an example of a .vimrc file with some improvements you can try out:

" enable syntax coloring
syntax on
" change color scheme
colorscheme desert
" make F5 key act like in notepad and write timestamp
nnoremap <F5> "=strftime("%Y-%m-%d %H:%M:%S")<CR>P
" make F6 key write file last modified timestamp
nnoremap <F6> =strftime("%Y-%m-%d %H:%M:%S",getftime(expand('%:p')))<CR>P
" make F7 write both?
imap <F7> <C-R>="\t# ".strftime("%Y-%m-%d %H:%M:%S",getftime(expand('%:p')))."\n\t# ".strftime("%Y-%m-%d %H:%M:%S")<CR><CR>
" turn of wrapping
set nowrap
" start in insert mode
au BufRead,BufNewFile * start
" simulate tab with spaces
set softtabstop=2
" always uses spaces instead of tab
set expandtab

Cannot copy selected text to clipboard

This is a very frustrating kind of bug that disables the copy menu item when selecting text in vi in terminal programs.

The reason for this is that visual mode is activated when selecting text. And for some reason it is not permitted to copy text in this mode.

One workaround is to hold down shift and select the text with the mouse, then the menu item is available.

But a better fix is to edit ~/.vimrc so it has this line included amongst the others:
set mouse=v


This is a personal note. Last updated: 2020-05-04 03:39:13.







Don't forget to pay my friend a visit too. Joakim