old-www/LDP/intro-linux/html/sect_06_02.html

495 lines
8.0 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Using the Vim editor</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Introduction to Linux"
HREF="index.html"><LINK
REL="UP"
TITLE="Text editors"
HREF="chap_06.html"><LINK
REL="PREVIOUS"
TITLE="Text editors"
HREF="sect_06_01.html"><LINK
REL="NEXT"
TITLE="Linux in the office"
HREF="sect_06_03.html"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Introduction to Linux: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="sect_06_01.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 6. Text editors</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sect_06_03.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="sect_06_02"
></A
>6.2. Using the Vim editor</H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_06_02_01"
></A
>6.2.1. Two modes</H2
><P
>The <B
CLASS="command"
>vi</B
> editor is a very powerful tool and has a very extensive built-in manual, which you can activate using the <B
CLASS="command"
>:help</B
> command when the program is started (instead of using <B
CLASS="command"
>man</B
> or <B
CLASS="command"
>info</B
>, which don't contain nearly as much information). We will only discuss the very basics here to get you started.</P
><P
>What makes <B
CLASS="command"
>vi</B
> confusing to the beginner is that it can operate in two modes: command mode and insert mode. The editor always starts in command mode. Commands move you through the text, search, replace, mark blocks and perform other editing tasks, and some of them switch the editor to insert mode.</P
><P
>This means that each key has not one, but likely two meanings: it can either represent a command for the editor when in command mode, or a character that you want in a text when in insert mode.</P
><DIV
CLASS="note"
><P
></P
><TABLE
CLASS="note"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="../images/note.gif"
HSPACE="5"
ALT="Note"></TD
><TH
ALIGN="LEFT"
VALIGN="CENTER"
><B
>Pronunciation</B
></TH
></TR
><TR
><TD
>&nbsp;</TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>It's pronounced <SPAN
CLASS="QUOTE"
>"vee-eye"</SPAN
>.</P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_06_02_02"
></A
>6.2.2. Basic commands</H2
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_06_02_02_01"
></A
>6.2.2.1. Moving through the text</H3
><P
>Moving through the text is usually possible with the arrow keys. If not, try:</P
><P
></P
><UL
><LI
><P
><B
CLASS="command"
>h</B
> to move the cursor to the left</P
></LI
><LI
><P
><B
CLASS="command"
>l</B
> to move it to the right</P
></LI
><LI
><P
><B
CLASS="command"
>k</B
> to move up</P
></LI
><LI
><P
><B
CLASS="command"
>j</B
> to move down</P
></LI
></UL
><P
>SHIFT-G will put the prompt at the end of the document.</P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_06_02_02_02"
></A
>6.2.2.2. Basic operations</H3
><P
>These are some popular <B
CLASS="command"
>vi</B
> commands:</P
><P
></P
><UL
><LI
><P
><B
CLASS="command"
>n dd</B
> will delete n lines starting from the current cursor position.</P
></LI
><LI
><P
><B
CLASS="command"
>n dw</B
> will delete n words at the right side of the cursor.</P
></LI
><LI
><P
><B
CLASS="command"
>x</B
> will delete the character on which the cursor is positioned</P
></LI
><LI
><P
><B
CLASS="command"
>:n</B
> moves to line n of the file.</P
></LI
><LI
><P
><B
CLASS="command"
>:w</B
> will save (write) the file</P
></LI
><LI
><P
><B
CLASS="command"
>:q</B
> will exit the editor.</P
></LI
><LI
><P
><B
CLASS="command"
>:q!</B
> forces the exit when you want to quit a file containing unsaved changes.</P
></LI
><LI
><P
><B
CLASS="command"
>:wq</B
> will save and exit</P
></LI
><LI
><P
><B
CLASS="command"
>:w newfile</B
> will save the text to <TT
CLASS="filename"
>newfile</TT
>.</P
></LI
><LI
><P
><B
CLASS="command"
>:wq!</B
> overrides read-only permission (if you have the permission to override permissions, for instance when you are using the <EM
>root</EM
> account.</P
></LI
><LI
><P
><B
CLASS="command"
>/astring</B
> will search the string in the file and position the cursor on the first match below its position.</P
></LI
><LI
><P
><B
CLASS="command"
>/</B
> will perform the same search again, moving the cursor to the next match.</P
></LI
><LI
><P
><B
CLASS="command"
>:1, $s/word/anotherword/g</B
> will replace <TT
CLASS="filename"
>word</TT
> with <TT
CLASS="filename"
>anotherword</TT
> throughout the file.</P
></LI
><LI
><P
><B
CLASS="command"
>yy</B
> will copy a block of text.</P
></LI
><LI
><P
><B
CLASS="command"
>n p</B
> will paste it n times.</P
></LI
><LI
><P
><B
CLASS="command"
>:recover</B
> will recover a file after an unexpected interruption.</P
></LI
></UL
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_06_02_02_03"
></A
>6.2.2.3. Commands that switch the editor to insert mode</H3
><P
></P
><UL
><LI
><P
><B
CLASS="command"
>a</B
> will append: it moves the cursor one position to the right before switching to insert mode</P
></LI
><LI
><P
><B
CLASS="command"
>i</B
> will insert</P
></LI
><LI
><P
><B
CLASS="command"
>o</B
> will insert a blank line under the current cursor position and move the cursor to that line.</P
></LI
></UL
><P
>Pressing the <B
CLASS="keycap"
>Esc</B
> key switches back to command mode. If you're not sure what mode you're in because you use a really old version of <B
CLASS="command"
>vi</B
> that doesn't display an <SPAN
CLASS="QUOTE"
>"INSERT"</SPAN
> message, type <B
CLASS="keycap"
>Esc</B
> and you'll be sure to return to command mode. It is possible that the system gives a little alert when you are already in command mode when hitting <B
CLASS="keycap"
>Esc</B
>, by beeping or giving a visual bell (a flash on the screen). This is normal behavior.</P
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_06_02_03"
></A
>6.2.3. The easy way</H2
><P
>Instead of reading the text, which is quite boring, you can use the vimtutor to learn you first <SPAN
CLASS="application"
>Vim</SPAN
> commands. This is a thirty minute tutorial that teaches the most basic <SPAN
CLASS="application"
>Vim</SPAN
> functionality in eight easy exercises. While you can't learn everything about <B
CLASS="command"
>vim</B
> in just half an hour, the tutor is designed to describe enough of the commands that you will be able to easily use Vim as an all-purpose editor.</P
><P
>In UNIX and MS Windows, if <SPAN
CLASS="application"
>Vim</SPAN
> has been properly installed, you can start this program from the shell or command line, entering the <B
CLASS="command"
>vimtutor</B
> command. This will make a copy of the tutor file, so that you can edit it without the risk of damaging the original. There are a few translated versions of the tutor. To find out if yours is available, use the two-letter language code. For French this would be <B
CLASS="command"
>vimtutor <TT
CLASS="parameter"
><I
>fr</I
></TT
></B
> (if installed on the system).</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sect_06_01.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sect_06_03.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Text editors</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="chap_06.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Linux in the office</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>