VimStuff

From HerzbubeWiki
Jump to: navigation, search

.vimrc

~/.vimrc is loaded automatically by vim when it launches. The file may contain various commands, such as:

syntax enable
set encoding=utf-8

For more examples see "vim on Debian" further down.


Syntax coloring

To enable syntax coloring:

syntax enable

To load a specific syntax file:

set syntax=foobar

Custom syntax files are stored here:

~/.vim/syntax/foobar.vim


Navigate

  • Cursor keys work only if the terminal supports them. The keys that always work are: hlkj (left, right, up, down)
  • Inside the help:
    • Jump to a topic: Place cursor on topic title, then press Ctrl+[
    • Jump back: Ctrl+T
  • Paranthesis/brackets/braces: % jumps to the appropriate sibling


Windows

Display help on a topic:

:help usr_08

To create a new window:

:split

To close a window:

:close

Close all windows except the current one:

:only

Close all windows and exit vim:

:qall

Save changes in all windows:

:wall

Jump from one window to the next:

Ctrl+W

Create a a new window and load a file inside that new window:

:split foo.txt

Create a new window from a new, empty file:

:new

Change window size:

Ctrl+W +
Ctrl+W -

Change window size to a certain value:

Ctrl+W _

Vertical windows are created with

vsplit

instead of just "split".

To jump between windows

Ctrl+W [hjkl]


Encodings

Help on the topic from within vim:

:help multibyte
:help mbyte-encoding

Set the encoding for editing from within vim to, for instance, UTF-8:

set encoding=utf-8

Set the encoding for the file currently loaded:

set fileencoding=utf-8

Set the encoding for the current screen display:

set termencoding=utf-8
  • If "encoding" and "fileencoding" are different, corresponding conversion is attempted when a file is loaded or saved.
  • If "encoding" and "termencoding" are different, corresponding conversion is attempted between screen display and keyboard entry
  • vim usually tries to detect the encoding of a file when it is loaded. Search order and which encodings are to be considered in the first place can be determined as follows. vim uses the first encoding from the list that does not trigger an error.
set fileencodings=utf-8 latin1


There are three groups of encodings:

  • 8bit: Every byte corresponds to one character. 256 different characters are possbiel. Example: ISO-8859-1 (Latin1)
  • 2byte: 2 bytes correspond to one character. These encodings are mainly used in Asian countries.
  • Unicode: The actual encoding determines how many bytes correspond to one character. Example: UTF-8


To list all known encoding names in vim:

help encoding-names

A selection:

  • latin1: Actually ISO-8859-1, but vim uses the name latin1
  • iso-8859-n: Variants of ISO-8859, where 2 <= n <= 15
  • utf-8: 32 bit UTF-8 encoded Unicode (alias: utf8)
  • ucs-2: 16 bit UCS-2 encoded Unicode (alias: unicode)
  • ucs-2le: Like ucs-2, but little-endian
  • ucs-4: 32 bit UCS-4 encoded Unicode


Characters in different encodings

Hex Octal Decimal Binary Hex Octal Decimal Binary Hex Octal Decimal Binary Hex Octal Decimal Binary
ä 0xc3 0xa4 \303 \244 195 164 11000011 10100100 0xe4 \ 0x8a \212 138 10001010 0xe4 \344 228 11100100
ë 0xc3 0xab \ \ 0xeb \ 0x91 \ 0x \
ï 0xc3 0xaf \ \ 0xef \ 0x95 \ 0x \
ö 0xc3 0xb6 \ \ 0xf6 \ 0x9a \232 10011010 0xf6 \366 11110110
ü 0xc3 0xbc \ \ 0xfc \ 0x9f \237 10011111 0xfc \374 11111100
á 0xc3 0xa1 \ \ 0xe1 \ 0x87 \ 0x \
é 0xc3 0xa9 \ \ 0xe9 \ 0x8e \ 0x \
í 0xc3 0xad \ \ 0xed \ 0x92 \ 0x \
ó 0xc3 0xb3 \ \ 0xf3 \ 0x97 \ 0x \
ú 0xc3 0xba \ \ 0xfa \ 0x9c \ 0x \
à 0xc3 0xa0 \ \ 0xe0 \ 0x88 \ 0x \
è 0xc3 0xa8 \ \ 0xe8 \ 0x8f \ 0x \
ì 0xc3 0xac \ \ 0xec \ 0x93 \ 0x \
ò 0xc3 0xb2 \ \ 0xf2 \ 0x98 \ 0x \
ù 0xc3 0xb9 \ \ 0xf9 \ 0x9d \ 0x \
â 0xc3 0xa2 \ \ 0xe2 \ 0x89 \ 0x \
ê 0xc3 0xaa \ \ 0xea \ 0x90 \ 0x \
î 0xc3 0xae \ \ 0xee \ 0x94 \ 0x \
ô 0xc3 0xb4 \ \ 0xf4 \ 0x99 \ 0x \
û 0xc3 0xbb \ \ 0xfb \ 0x9e \ 0x \
Ä 0xc3 0x84 \ \ 0xc4 \ 0x80 \ 0x \
Ë 0xc3 0x8b \ \ 0xcb \ 0xe8 \ 0x \
Ï 0xc3 0x8f \ \ 0xcf \ 0xec \ 0x \
Ö 0xc3 0x96 \ \ 0xd6 \ 0x85 \ 0x \
Ü 0xc3 0x9c \ \ 0xdc \ 0x86 \ 0x \
Á 0xc3 0x81 \ \ 0xc1 \ 0xe7 \ 0x \
É 0xc3 0x89 \ \ 0xc9 \ 0x83 \ 0x \
Í 0xc3 0x8d \ \ 0xcd \ 0xea \ 0x \
Ó 0xc3 0x93 \ \ 0xd3 \ 0xee \ 0x \
Ú 0xc3 0x9a \ \ 0xda \ 0xf2 \ 0x \
À 0xc3 0x80 \ \ 0xc0 \ 0xcb \ 0x \
È 0xc3 0x88 \ \ 0xc8 \ 0xe9 \ 0x \
Ì 0xc3 0x8c \ \ 0xcc \ 0xed \ 0x \
Ò 0xc3 0x92 \ \ 0xd2 \ 0xf1 \ 0x \
Ù 0xc3 0x99 \ \ 0xd9 \ 0xf4 \ 0x \
 0xc3 0x82 \ \ 0xc2 \ 0xe5 \ 0x \
Ê 0xc3 0x8a \ \ 0xca \ 0xe6 \ 0x \
Î 0xc3 0x8e \ \ 0xce \ 0xeb \ 0x \
Ô 0xc3 0x94 \ \ 0xd4 \ 0xef \ 0x \
Û 0xc3 0x9b \ \ 0xdb \ 0xf3 \ 0x \
ç 0xc3 0xa7 \ \ 0xe7 \ 0x8d \ 0x \
Ç 0xc3 0x87 \ \ 0xc7 \ 0x82 \ 0x \
ñ 0xc3 0xb1 \ \ 0xf1 \ 0x96 \ 0x \
Ñ 0xc3 0x91 \ \ 0xd1 \ 0x84 \ 0x \


vim on Debian

Packages

Packages to install

vim


Make vi the default editor

The default text editor on Debian these days is usually nano. With the following command you can interactively change the default editor to vi:

update-alternatives --config editor


Configuration

Some global settings can be changed in

/etc/vim/vimrc.local

(this file is loaded by default by /etc/vim/vimrc)

My sample file looks like this:

root@pelargir:~# cat /etc/vim/vimrc.local 
" ----------------------------------------
" Parts of this file are copied from /etc/vim/vimrc
" ----------------------------------------

" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default. 
syntax on

" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
if has("autocmd")
  filetype plugin indent on
endif

" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
set showcmd            " Show (partial) command in status line.
set showmatch          " Show matching brackets.
set incsearch          " Incremental search


Automatically display compressed files

With proper configuration it is possible to open a .gz or other compressed file directly in vi, without having to sidestep to temporarily decompress and then open the temporary file.

Unfortunately configuration for this feature is not easy, so I don't go into details here. I'm satisfied that it "just works" when the following folder exists:

/usr/share/vim/vim74/autoload

(replace "vim74" by something else if necessary; it's the autoload folder that is important)


The autoload folder contains a number of scripts, which are automatically installed by the package

vim-runtime

Note: If you only have vim-tiny installed, then there's a good chance that vim-runtime is not installed because vim-runtime is not a dependency of vim-tiny. I recommend you replace vim-tiny with vim.