Next · Web Coding Navigation · Home > Etcetera > Techology > Vim
  Life Skills :: Nethack , Latex

Text editing in Vim

The Vim text editor (or the older Vi) is standard on Linux/*NIX computers or servers. It has a high learning curve, but allows for extremely efficient work once learned, in both writing and programming.

Page contents
1.  Text Editing in VIM

Whenever you're editing a file and think Gee, it'd be nice if you could... rest assured that someone, somewhere has thought of this before you, and added it to Vim.

Add a link12 links in this section
C programming with VIM
relevant for PHP, Java, Perl, etc
Comfortable PHP Editing with Vim
a series by Tobias Schlitt, covering most of the cool tricks.
Efficient Editing with VIM
for intermediate VIM users
My life with Text Editors
Mark Stosberg writes: After committing some time to learn the unituitive vi, I had a shift in thinking. vi felt difficult to learn initially, but was much faster to use once I had learned it. I realized I was very willing to invest a little time in learning to use my text editor especially if it was going to repay me many-times over in efficiency later.
Remapping VIM keystrokes
some very helpful options
Seven habits of effective text editing
by Bram Moolenaar, maintainer of VIM
Text Processing With Integrated Spell Checking
...and Paragraph Formatting in the Vim and Vi Editors. By David Highley.
Vi-Reference · PDF
a very handly 1-page printout; paste beside monitor!
VIM
powerful text/coding editor; VI is the standard Linux editor, VIM is an improved version for Linux/Mac/PC/etc
Vim Macros for Editing DocBook Documents
From Linux Journal, an excellent demonstration of how far VIM can be customized for efficient editing of complex documents
VIM Quick Reference Card · PDF
Lots of useful operations, of a few keystrokes each. A good complement to the Vi Reference card also listed here.
VIM Tutorial
or just use the vimtutor command on Linux/*NIX
2.  Shell stunts

While available for most operating systems, Vim is strongest in a Linux shell, because it can take advantage of command-line power-user options. For example, you can open files in a hierarchy matching a certain text or filename pattern using:

vim `grep -rnil 'pattern' *`
vim `find . name '*.php'`

Or you can automate Vim's search and replace on files (again, recursively), with some simple shell scripting:

for i in `find . -name '*.txt'`; do vim -c "%s/[aeiou]/*/g | w | q" "$i"; done

(Obviously, do NOT execute that exact command, or anything like it, unless you know what you're doing.)

Add a linkOne link in this section
Bash Guide for Beginners
an introduction to shell scripting
Add a link 3.  Useful ~/.vimrc settings

The nicest thing about VIM is that you can change the command keys to do pretty much whatever you like. For example, I recently toyed with having the backspace key erase a word instead of a character. Anything you set can be made permanent by adding it to your ~/.vimrc (the invisible VIM control file in your home directory). My current favourite Vim trick is using Mozilla Firebird's Mozex extension to start up gvim (the graphical version) when editing text in a web page form (thus giving me spell-checking, shell access, HTML markup shortcuts, automated text formatting, and so on).

" --------------------------------------------------------------
" SUMMARY OF SHORTCUTS  (Control keys)
" --------------------------------------------------------------
" C-O      open a file with file explorer (split screen)
" C-I      open notes.txt (split screen), e.g. for notes
" C-F      toggle nice formatting (kill during cut & paste)
" C-G      toggle indent-based page folding (nice & compact)
" C-J      rewrap a paragraph or a program comment
" C-L      spell check a document
" C-E      edit my ~/.vimrc settings (splitscreen)
" C-U      update (reload) my ~/.vimrc settings
" C-Space  cycle through split screens (maximize each)
" Q        quit a file without writing
" S        write a file back to disk
" C-N      cycle through multiple open files (next)
" C-P      cycle through multiple open files (previous)
"          e.g. when using vim `grep -rnil 'pattern' *`
" --------------------------------------------------------------

" expedite ~/.vimrc edit & reload
map <C-e> :split ~/.vimrc <CR>
map <C-u> :source ~/.vimrc <CR>

" navigation
map <C-o> :split . <CR>
map <C-e> :split notes.txt <CR>
map <C-p> :prev <CR>
map <C-n> :next <CR>

" editing
map S :w <CR>
map Q :q <CR>

" block editing
map <S-Left> {
map <S-Right> }
map <S-Up> zk
map <S-Down> zj

" move between split screens 
map <C-Space> <C-w><C-w><C-w>_<C-w>5-

" make it easy to grab HTML tags
map ] f>
map [ F<

" general settings
set incsearch
set ignorecase
set smartcase
set history=100
set shiftwidth=4
set tabstop=4
set expandtab

" C-f to toggle formatting
map <C-f> :set number! <CR> :set autoindent! <CR>

" other formatting options
map <C-g> :set foldenable! <CR>
map <C-j> gqap
map <C-h> :noh <CR>

" for email: wrap text to 72
map <C-m> :set textwidth=72

" initialise formatting
set number
set autoindent
syntax on

" auto-complete PHP tags (Ctrl-X Ctrl-K)
set dictionary=~/php.dictionary
set filetype=php

" folding
set foldenable
set foldmethod=indent

" Integrate aspell spelling checker
set autowrite
map <C-l> <Esc>:!aspell -c --dont-backup "%"<CR>:e! "%"<CR><CR>
Email editors · Add feedback No feedback items for this page
Top of page About this page · Display