Vi notes

From Helpful
Jump to: navigation, search
This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)


Intro

Vi is perhaps the most available editor in the *nix world, because it's small, and pretty powerful once understood. (vim, Vi IMproved, is also commonly there)

It takes a different approach from many many editors. It does not revolve around cursors, arrows, and typing as you see in notepads, text fields, and such - it is more procedural and generally geekier than that.

This makes it powerful for specific purposes, and a learning curvy pain in the butt for some others.


Some arguments for vi are often also arguably arguments against. A common one is that "the more you can do quickly, the more you can focus on stay in the editing zone without having to meander off to find some hidden feature or menu option somewhere"

This is true, but only eventually. The learning curve is very steep, and somewhat discouraging. Yes, there is more functionality under fewer keystrokes, but only after you've scoured many pages, picked the ones you like and forgotten all the ones you won't use.

Modes

There are two or three basic modes in vi:

in command mode all keys are interpreted as commands

  • This is the mode vi starts up in, and is generally considered the neutral mode
  • ESC goes from other modes to command mode
  • almost all commands put/keep vi in command mode

insert mode lets you enter text until you switch back to command mode.

There is also visual mode, meant for selecting text. Could be considered part of command mode.


Switching between them:

  • from command mode to insert mode, you'ld often use one of:
    • i
      starts inserting before the cursor
    • a
      starts inserting after the cursor
    • I
      starts inserting at the start of the line
    • A
      starts inserting at the end of the line
    • o
      starts inserting after a newly created line
    • O
      starts inserting before a newly created line
    • c
      is basically a delete up to a move command, which then stays in insert mode. Can be seen as a 'replace'; cc would replace the current line with what you type next.
  • from command mode to visual mode:
    • v
      - character oriented
    • V
      - line oriented
    • Ctrl-V
      - block oriented (rectangles)


  • back to command mode: Escape


On the surface, insert mode is what makes vi like any other editor - and if you spend all your time in insert mode, you may be happier using another editor. If you wish to explore the things you can do in vi, that's command mode stuff.


There are a few more ways to input different types of commands.

Commands

Commands are quite central to vi. You can't particularly use vi until you've learned at least a handful. At the same time, there are too many to introduce all at once, so you'll have to get out there and figure out what you'll want.

You do not see commands while you type them. Partial entries that you think may be bad can be abandoned/forgotten by using Ctrl-G or Esc.


Simple commands

Commands, in general, apply once they are a complete command.

You could call those commands that consist of a single keystroke simple. Most navigation consists of simple commands.


Navigation is probably the concept with the most related commands, including:

  • k
    means (go) up
  • j
    and Ctrl
    n
    mean (go) down (newline isn't the same; it also moves to the start of the next line)
  • h
    and Ctrl
    h
    mean (go) left
  • l
    and space mean (go) right

That the arrow keys (but not the numpad arrows) act like the hjkl navigation, and can also be used in the combinations.


  • b
    means (go to) 'beginning of word / previous word'
  • e
    means (go to) 'end of word / next word'
  • w
    means (go to) 'beginning of next word'

There are also the rather similar B, E and W, the 'bigword' variations. Bigwords are anything split with whitespaces, whereas the above commands are somewhat smarter than that.

  • 0
    means (go to) 'start of line'
  • ^
    means (go to) 'first character online'
  • $
    means (go to) 'end of line'
  • (
    means (go to) 'start of/previous sentence'
  • )
    means (go to) 'end of/next sentence'
  • {
    means (go to) 'start of/previous paragraph'
  • }
    means (go to) 'end of/next paragraph'


These are not entirely basic, but while we're at listing navigation commands...

  • Ctrl
    f
    means (go) 'forward a page'
  • Ctrl
    b
    means (go) 'back a page'
  • H
    ,
    M
    ,
    L
    means (go) top/middle/bottom of current page
  • gg
    means (go to) 'start of file'
  • G
    means (go to) 'end of file'
  • numberG
    means (go to) 'a specific line' (see 'argument' section below)

Compound commands

Compound commands usually combine a basic concept with a simple command.

For example: deleting is based on d, and usually combines with a movement command to specify up to what point to delete. From the navigation list, you can think of a delete command to go along with each, including e.g.

  • dh
    deletes character to the left of the cursor (only inside line)
  • dl
    deletes character to the left of the cursor (only inside line)
  • dk
    deletes this line, and the last one
  • dj
    deletes this line, and the next one
  • db
    deletes to beginning of / previous word
  • de
    deletes to end of / next word
  • d0
    deletes to start of line
  • d$
    deletes to end of line

...and so on.


Compound commands regularly see an odd one out that you'll have to remember, in this case:

  • dd
    deletes whole line


Combinations can also work in other ways. For example, fixed-character arguments after a 'base command':

  • fchar
    finds the next occurrence of the character. F finds the previous one. (in some situations you'll like t and T, which are something like the 'inner' variations of the same function)


Numeric arguments

Various commands can take numeric arguments. Often its meaning is repetition of the command:

  • 14j
    moves down 14 lines,
  • 3dd
    deletes 3 lines, as does
    d3d
  • 14dw
    deletes fourteen words.
  • 55u
    undo 55 steps,

...but not always like that:

  • y3,
    , yanks from the current line, from the the cursor up to three commas from now.

...and in fact not always:

  • 123G
    , which goes to line 123

Delimiter awareness

%
moves between highlit ends of delimited ranges - between { and }, between ( and ) (which is function-call aware; sitting on the function name also works) and so on. This can be useful in coding, e.g. to cut part out of a complex expression with
d%
.


There are also the inner and outer modifiers. For example,
diw
removes the entire word you have selected,
di'
or
di"
empty out a literal string you're standing in
di)
empties out arguments to a call, and so on. The outer version of the same uses a. For example,
da"
cuts a literal string including the delimiters.


*
moves to the next mention of the word under the cursor (and keeps all highlit).
#
does the same but goes to the previous. This could be generally useful, but probably more so to coding, as it is a quick way of seeing declaration/use of variables, moving to likely closing tags in XML, etc.


There is B, referring to a curly-delimited block, useful for coding. For example,
daB
cuts out the innermost block of code.

Colon-prompt-thing

Pressing colon moves you into a prompt. Though also character based, this prompt does not take vi commands. Apparently this prompt originated in imitation of the now pretty ancient ex and ed editors.

They are also usable in combinations. For example, where:

  • :w
    is save, and
  • :q
    is quit (won't quit if file not saved)

there are common combinations like:

  • :wq
    write, then quit
  • :q!
    exclamation is force. In this case it means force quit, without saving.

File operations:

  • :e filename
    close current file and open another (blocks if current unsaved)
  • :f
    print filename and line length.
  • :n
    next file
  • :p
    previous file

Navigation:

  • :number
    skip to line number



Regexp replace

Based on a bare slashed regexp expresion, like:
:s/pattern/replacement/

Note:

  • By default only replaces the first match. Add a
    /g
    to replace al matches
  • By default works on the current line. Start with
    %s
    to have it apply globally.


Searching

Pressing
/
will give you a prompt for a (regexp) search, as also present in less and other utilities. Note this is also present
?
is the same, but backwards.


Things of interest

Marks

You can memorize positions in your text, to go back to later.

  • mletter
    stores positions in a file, referred to by a single letter
  • `letter
    moves you to the respective saved position


Clipboard

Copy, Cut and Paste

Copy is called yanking, based on y, so:

  • yw
    yank rest of word, etc.
  • yy
    (and Y) yanks current line (verify)
  • ...and so on.

Cut is an extra feature from delete operations based on d, so:

  • dw
    is effectively 'cut rest of word'
  • dd
    'cut line'
  • ...and so on

For cut and copy, remember that using the i and a modifier may be useful.


Paste:

  • p
    paste after current line
  • P
    paste before current line
  • ]p
    is a code-indent-aware paste

Registers

Those who keep on overwriting their clipboards may find it interesting that you can store things into specific registers instead of the general clipboard.

Registers are identified by letters.

  • "aY
    copies the current line to register a
  • "ap
    pastes from register a

...and so on.

To see register contents, use
:reg
.


Windows

In later vi versions such as vim and elvis, you can get windowing. Windows have their own edit buffer, and act like separate editors (for example, :q will close the window and not the editor).

  • :sp filename
    creates a new window and opens the file in it.
  • Ctrl-W Ctrl-W
    switches you through the windows


Macros

You can record series of keystrokes. These too are stored in letter-identified registers.

  • qa
    starts recording into register a
  • q
    (while recording) stops recording
  • @a
    plays back register a.

In practice you probably want to record things that place the cursor at the next thing to edit so that you can repeat the command using a numeric argument.



Quasi-sorted

Visual mode

In improved vis (vim, elvis, etc.) you can visually select things.

  • v
    enters this mode with the current position as one end of the selection
  • V
    is an alternative: line-based visual mode
  • Ctrl
    v
    is another: blockwise visual mode
  • movement sets the other end
  • y
    copies the selection,
    d
    cuts the selection. Both move back to general command mode.
  • p
    pastes


General

  • u undo previous change
  • . redo (verify)




See also

Cheat sheets and mini-references:


Tutorial-like things

Random interesting things: