[[vim]]

vim

The editor to rule them all.

aptitude install vim
update-alternatives --set editor /usr/bin/vim.basic

If you need to temporarily set vim as your preferred editor (because your crazy colleague set up nano as the systems default) you can do like so:

env EDITOR=vim crontab -e

Here vim will be your editor of choice for the crontab command.

Or make vim your default editor for the current session:

export EDITOR=vim

This will last as long as your EDITOR environment variable exists (usually until you logout).

⇒ General overview (LPIC 103.8) relevant stuff about vim.

Config File

vim config file with minimal changes compared to default debian squeeze:

~/.vimrc
" All system-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
" /usr/share/vim/vimcurrent/debian.vim) and sourced by the call to :runtime
" you can find below.  If you wish to change any of those settings, you should
" do it in this file (/etc/vim/vimrc), since debian.vim will be overwritten
" everytime an upgrade of the vim packages is performed.  It is recommended to
" make changes after sourcing debian.vim since it alters the value of the
" 'compatible' option.

" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim

" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'.  Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
"set compatible

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

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
"set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
"if has("autocmd")
"  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"endif

" 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 ignorecase         " Do case insensitive matching
"set smartcase          " Do smart case matching
"set incsearch          " Incremental search
"set autowrite          " Automatically save before commands like :next and :make
"set hidden             " Hide buffers when they are abandoned
"set mouse=a            " Enable mouse usage (all modes)
set number              " display line numbers

set autoindent          " copy the indentation from the previous line, when starting a new line
set tabstop=4           " Insert 4 spaces for a tab
set expandtab           " To insert space characters whenever the tab key is pressed
set shiftwidth=4        " To change the number of space characters inserted for indentation

set pastetoggle=<F2>    " disable/enable autoindent to paste without messing up formatting

" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
  source /etc/vim/vimrc.local
endif

" enable setting title
  set title

" configure title to look like: Vim /path/to/file
  set titlestring=VIM:\ %-25.55F\ %a%r%m titlelen=70

" show the `best match so far' as search strings are typed
  set incsearch

" Highlight search results once found:
  set hlsearch

" highlight the current line the cursor is on
  set cursorline

" move to the next tab with <ctrl>+n
  map <C-n> :tabnext<CR> 

An alternative would be to specify some of the above to the file you edit, which will override the settings vim reads from its config files. Remember to use the comments applicable to the programming language you're using (bash in this case):

# vim: tabstop=4:shiftwidth=4:expandtab:autoindent # 

vim Addons

To integrade available addons into vim the addon-manager is needed:

# 1st the manager is needed:
aptitude install vim-addon-manager
 
#2nd an addon can be installed:
aptitude install vim-puppet
 
#3rd activate the addon 
#   (only for current user)
vim-addons install puppet
#   (for all users system wide)
vim-addons -w install puppet
 
# list what is configured:
vim-addons
 
# Name                     User Status  System Status
editexisting                removed       removed
justify                     removed       removed
matchit                     removed       removed
puppet                      installed     removed

Undo/Redo

Undo a change:

u

Redo an undo:

ctrl-R

or just

U

Tabs/Indent

To change all the existing tab characters to match the current tab settings, use:

:retab

To convert only the current line to to match the current tab settings, use:

:.retab

To insert space characters when you press the tab key, set the 'expandtab' option. If you need a real tab charater with this option set, use Ctrl-V<Tab> key sequence

:set expandtab

# or, shorter:

:set et

To disable expandtab:

:set expandtab!

# or, shorter:

:set noet

To insert a tab character when expandtab is on:

<ctrl><V><tab>

Regex Replace (Reformat Comments)

To comment out a block of code (#): enter visual mode (V), then search & replace:

" comment out:
:s/^/# /

" remove comment:
:s/^# //

Remove 4 spaces in front of (#) as below:

    # Some comment(s)
    # lorem ipsum
:s/^\s\{4}//g

or remove any number of spaces from the beginning:

:s/^\s\+//g

the result will be:

# Some comment(s)
# lorem ipsum

do the opposite, insert 4 spaces:

:s/^/    /g

Organize Your Screen With Tabs

Use tabs within vim.

Open two files at once in tabs:

vim -p file1 file2 

Open a file into a new tab:

:tabf filename

Switch between tabs:

:tabn
:tabp
:tabfirst
:tablast

Make switching tabs even easier:

# add the following line to your ~/.vimrc and you can press <ctrl><n> to switch between tabs!
map <C-n> :tabnext<CR>

List tabs:

:tabs

Run a command in all open tabs (replace alle occurrences of “old” with “new”):

:tabdo %s/old/new/g

The big tab manual:

:help tab-page-intro

Draw a Line

Imagine you want to draw a line (e.g. 75 dashes). The basic workflow is like so:

<ESC> 
<the number of repetitions you want>
i
<the character you want to repeat> 
<ESC>

For our dashes this would look like:

<ESC>75i-<ESC>

Convert Text to Lower-/Uppercase

# lowercase
guu
 
# uppercase
gUU

Plugins

linux/vim.txt · Last modified: 2015-09 by tb
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 ipv6 ready