Jump to content

Some Input Mode Shortcuts for vi and Vim

+ 1
  chco's Photo
Posted Mar 02 2011 02:41 PM

Many Unix, Linux, and Mac OS X users enjoy using the powerful, platform-agnostic text editors vi and Vim, but there are far too many commands for anyone to remember. The following excerpt from the recent O'Reilly publication vi and Vim Editors Pocket Reference, Second Edition helps with some input mode shortcuts.

vi provides two ways to decrease the amount of typing you have to do: abbreviations and maps.

Word Abbreviation


:ab abbr phrase


Define abbr as an abbreviation for phrase.


:ab


List all defined abbreviations.


:unab abbr


Remove definition of abbr.


Command and Input Mode Maps


:map  x sequence


Define character(s) x as a sequence of editing commands.


:unmap  x


Disable the sequence defined for x.


:map


List the characters that are currently mapped.


:map!  x sequence


Define character(s) x as a sequence of editing commands or text that will be recognized in insert mode.


:unmap!  x


Disable the sequence defined for the insert mode map x.

:map!


List the characters that are currently mapped for interpretation in insert mode.


For both command and insert mode maps, the map name x can take several forms:


One character


When you type the character, vi executes the associated sequence of commands.


Multiple characters


All the characters must be typed within one second. The value of notimeout changes the behavior.


# n


Function key notation: a # followed by a digit n represents the sequence of characters sent by the keyboard’s function key number n.


To enter characters such as Escape (^[) or carriage return (^M), first type CTRL-V (^V).

Executable Buffers

Named buffers provide yet another way to create “macros”—complex command sequences you can repeat with a few keystrokes. Here’s how it’s done:

  • Type a vi command sequence or an ex command preceded by a colon; return to command mode.

  • Delete the text into a named buffer.

  • Execute the buffer with the @ command followed by the buffer letter.


The ex command :@buf-name works similarly.

Some versions of vi treat * identically to @ when used from the ex command line. In addition, if the buffer character supplied after the @ or * commands is *, the command is taken from the default (unnamed) buffer.

Automatic Indentation

Enable automatic indentation with the following command:

:set autoindent


Four special input sequences affect automatic indentation:


^T


Add one level of indentation; typed in insert mode


^D


Remove one level of indentation; typed in insert mode



^ ^D


Shift the cursor back to the beginning of the line, but only for the current line[1]

[1] ^ ^D and 0 ^D are not in elvis.



0 ^D


Shift the cursor back to the beginning of the line and reset the current auto-indent level to zero[2]

[2] The nvi 1.79 documentation has these two commands switched, but the program actually behaves as described here.


Two commands can be used for shifting source code:


<<


Shift a line left eight spaces


>>


Shift a line right eight spaces


The default shift is the value of shiftwidth, usually eight spaces.

vi and Vim Editors Pocket Reference

Learn more about this topic from vi and Vim Editors Pocket Reference, 2nd Edition.

Many Unix, Linux, and Mac OS X geeks enjoy using the powerful, platform-agnostic text editors vi and Vim, but there are far too many commands for anyone to remember. Author Arnold Robbins has chosen the most valuable commands for vi, Vim, and vi's main clones—vile, elvis, and nvi—and packed them into this easy-to-browse pocket reference. You'll find commands for all kinds of editing tasks, including programming, modifying system files, writing and marking up articles, and more.

See what you'll learn


Tags:
0 Subscribe


0 Replies