VIM tips

Content

  1. Modelines
  2. Completion
  3. Search
  4. Selection
  5. Markers
  6. Indenting
  7. Registers
  8. Multiwindow mode
  9. Tabs
  10. Execute

Modelines

modelines allow you to set variables specific to a file. By default, the first and last five lines are read by vim for variable settings. For example, if you put the following in the last line of a C program, you would get a textwidth of 60 chars when editing that file:

/* vim: tw=60 ts=2: */

Completion

Typing is very booring. try this

ctrl-n, ctrl-p              - next/previous word completion
(similar word in current file)

ctrl-x ctrl-l (ctrl-n/p)    - line completion

:set dictionary=/usr/share/dict/words
ctrl-x ctrl-k     - dictionary completion

also

ctrl-w      - erases word (insert mode... ctrl-u      - erases line  ...or on command line)

Search

Basic:

/pattern       - search forward for pattern
?pattern       - search backward
n              - repeat forward search
N              - repeat backward

Some variables you might want to set:

:set ignorecase - case insensitive
:set smartcase  - use case if any caps used
:set incsearch  - show match as search proceeds
:set hlsearch   - search highlighting

More cool searching tricks:

*    - search for word currently under cursor
g*  - search for partial word under cursor
(repeat with n) ctrl-o, ctrl-i  - go through jump locations
[I   - show lines with matching word under cursor

Search and replace…

:%s/search_for_this/replace_with_this/    - search whole file and replace
:%s/search_for_this/replace_with_this/c   - confirm each replace

Selection

V       - selects entire lines
v       - selects range of text
ctrl-v  - selects columns
gv      - reselect block

After selecting the text, try d to delete, or y to copy, or :s/match/replace/, or :center, or !sort, or…

Markers

Use markers to set places you want to quickly get back to, or to specify a block of text you want to copy or cut.

mk      - mark current position (can use a-z)
'k      - move to mark k
d'k     - delete from current position to mark k
'a-z    - same file
'A-Z    - beteween files

Indenting

Some variables you might want to set:

 :set tabstop 8     - tabs are at proper location
 :set expandtab     - don't use actual tab character (ctrl-v)
 :set shiftwidth=4  - indenting is 4 spaces
 :set autoindent    - turns it on
 :set smartindent   - does the right thing (mostly) in programs
 :set cindent       - stricter rules for C programs

I like having auto on, but smart does funny things based on keywords.

To indent the current line, or a visual block:

ctrl-t, ctrl-d  - indent current line forward, backwards
                  (insert mode)
visual > or <   - indent block by sw (repeat with . )

To stop indenting when pasting with the mouse, add this to your .vimrc:

:set pastetoggle=<f5>

then try hitting the F5 key while in insert mode (or just :set paste).

Registers

When you copy and cut stuff, it gets saved to registers. You can pull stuff from those registers at a later time.

 :reg     - show named registers and what's in them
 "5p      - paste what's in register "5

You can also record a whole series of edits to a register, and then apply them over and over.

 qk       - records edits into register k
            (q again to stop recording)
 @k       - execute recorded edits (macro)
 @@       - repeat last one
 5@@      - repeat 5 times

 "kp      - print macro k
            (e.g., to edit or add to .vimrc)
 "kd      - replace register k with what cursor is on

Multiwindow mode

If you want edit more then one file in your screen you can do this:

:split filename     - split window in horizontal mode and open file filename
:vsplit filename    - split window in vertical mode and open file filename
ctrl-w <arrows>     - navigate you between windows

Tabs

:tabnew filename   - open new tab with filename
:tabn              - switch to next tab
:tabp              - switch to previous tab

Execute
:!ls ~ – show you files of your home directory
:r !ls ~ – list files in your home directory and insert output into file

One Response to “VIM tips”

  1. vangori Says:

    Прочитай статью вот здесь, там все подробно написано.

Leave a Reply