Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Google   


Line Ranges in Ex

Most ex commands can accept a line range in front of them. By specifying the line range you restrict the command execution to this particular part of text only. Line range consists of one line specifier or two line specifiers, separated with a comma or semicolon. Specifiers can be line numbers, search expressions or bookmarks. In the latter case you need to mark the nessesary positions in the text typing ml , where "l" can be any letter, and use it later defining the line address.

Specifier
Description
number
an absolute line number
.
the current line
$
the last line in the file
%
the whole file. The same as 1,$
't
position of mark "t"
/pattern[/]
the next line where text "pattern" matches.
?pattern[?]
the previous line where text "pattern" matches
\/
the next line where the previously used search pattern matches
\?
the previous line where the previously used search pattern matches
\&
the next line where the previously used substitute pattern matches

If no line range is specified the command will operate on the current line only.

Ranges of lines that can be given to edit commands include:

Absolute line number

6 refers to line 6

1,6 refers to lines 1 to 6

Relative line numbers

-2 refers to 2 lines before the current line

+3 refers to 3 lines after the current line

-2,+3 refers to a range from 2 lines before the current line to 3 lines after the current line

Special symbols

$ refers to the last line in the file e.g. $p to display last line, 1,$p to display entire file

. refers to the current line e.g. .,$p to display from the current line to the end

Examples:

6d			- deletes lines the sixth line1,6d			- deletes the first six lines1,$d			- deletes all lines3a			- append text after line three.,+10w new	- saves the next ten lines to a file called new

The = operator gives the line number, with the last line the default, so typing = gives you the number of lines in a text. The number of the current line is obtained by typing .=.

 

Each may be followed (several times) by "+" or "-" and an optional number. This number is added or subtracted from the preceding line number. If the number is omitted, 1 is used.

/\#Step 1/+,/\#Step 2/-

- all lines between Step 1 and Step 2, non-inclusively, i.e. the lines containing Section 1 and Section 2 will not be affected.

The /pattern/ and ?pattern? may be followed by another address separated by a semicolon. A semicolon between two search patterns tells Vim to find the location of the first pattern, then start searching from that location for the second pattern.

/sub scan/;/^\{$/-,/sub log/+

The next example shows how you can reuse you search pattern:

:/lineno/+;+2 y

- this will search for the Section line and yank (copy) one line after into the memory.

:// normal p

- and that will search for the next Section line and put (paste) the saved text on the next line.


Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Reference

 
     N | Command            | Meaning
    ---+--------------------+-----------------------------------------------
     * | h | ^H | <erase>   | <*> chars to the left.
                            |
     * | j | <lf> | ^N      | <*> lines downward.
                            |
     * | l | <sp>           | <*> chars to the right.
                            |
     * | k | ^P             | <*> lines upward.
                            |
     * | $                  | To the end of line <*> from the cursor.
                            |
     - | ^                  | To the first CHAR of the line.
                            |
     * | _                  | To the first CHAR <*> - 1 lines lower.
                            |
     * | -                  | To the first CHAR <*> lines higher.
                            |
     * | + | <cr>           | To the first CHAR <*> lines lower.
                            |
     - | 0                  | To the first char of the line.
                            |
     * | |                  | To column <*> (<ht>: only to the endpoint).
                            |
     * | f<char>            | <*> <char>s to the right (find).
                            |
     * | t<char>            | Till before <*> <char>s to the right.
                            |
     * | F<char>            | <*> <char>s to the left.
                            |
     * | T<char>            | Till after <*> <char>s to the left.
                            |
     * | ;                  | Repeat latest `f'|`t'|`F'|`T' <*> times.
                            |
     * | ,                  | Idem in opposite direction.
                            |
     * | w                  | <*> words forward.
                            |
     * | W                  | <*> WORDS forward.
                            |
     * | b                  | <*> words backward.
                            |
     * | B                  | <*> WORDS backward.
                            |
     * | e                  | To the end of word <*> forward.
                            |
     * | E                  | To the end of WORD <*> forward.
                            |
     * | G                  | Go to line <*> (default EOF).
                            |
     * | H                  | To line <*> from top of the screen (home).
                            |
     * | L                  | To line <*> from bottom of the screen (last).
                            |
     - | M                  | To the middle line of the screen.
                            |
     * | )                  | <*> sentences forward.
                            |
     * | (                  | <*> sentences backward.
                            |
     * | }                  | <*> paragraphs forward.
                            |
     * | {                  | <*> paragraphs backward.
                            |
     - | ]]                 | To the next section (default EOF).
                            |
     - | [[                 | To the previous section (default begin of file).
                            |
     - | `<a-z>             | To the mark.
                            |
     - | '<a-z>             | To the first CHAR of the line with the mark.
                            |
     - | ``                 | To the cursor position before the latest absolute
                            |   jump (of which are examples `/' and `G').
                            |
     - | ''                 | To the first CHAR of the line on which the cursor
                            |   was placed before the latest absolute jump.
                            |
     - | /<string>[/]       | To the next occurrence of <string>.
                            |
     - | /[/]               | To the next occurrence of the latest search
                            |   item.
                            |
     - | ?<string>[?]       | To the previous occurrence of <string>.
                            |
     - | ?[?]               | To the previous occurrence of the latest search
                            |   item.
                            |
     - | /<string>/+[n]     | To n-th (default 1st) line after next occurrence
                            |   of <string>.
                            |
     - | ?<string>?+[n]     | Idem, searching in the opposite direction.
                            |
     - | /<string>/-[n]     | To n-th (default 1st) line before next occurrence
                            |   of <string>.
                            |
     - | ?<string>?-[n]     | Idem, searching in the opposite direction.
                            |
     - | <find>[;<find>]    | Perform successive `/'|`?' actions.  For example,
                            |   /foo/;/bar        - to next `foo', then
                            |                        to next `bar'
                            |   ?foo?-;/bar       - to line before previous
                            |                        `foo', then to next `bar'
                            |
     - | :/<string>[/]      | To the next line containing an occurrence of
                            |   <string>.
                            |
     - | :/[/]              | To the next line containing an occurrence of
                            |   the latest searched for item.
                            |
     - | :?<string>[?]      | To the previous line containing an occurrence
                            |   of <string>.
                            |
     - | :?[?]              | To the previous line containing an occurrence
                            |   of the latest searched for item.
                            |
     - | :/<string>/+[n]    | To n-th (default 1st) line after next occurrence
                            |   of <string>.
                            |
     - | :?<string>?+[n]    | Idem, searching in the opposite direction.
                            |
     - | :/<string>/-[n]    | To n-th (default 1st) line before next occurrence
                            |   of <string>.
                            |
     - | :?<string>?-[n]    | Idem, searching in the opposite direction.
                            |
     - | :<find>[;<find>]   | Perform successive `/'|`?' actions.  Each <find>
                            |   is of the form /<string/[+[n]] or
                            |   ?<string>?[+[n]].
                            |
     - | n                  | Repeat latest `/'|`?' (next).
                            |
     - | N                  | Idem in opposite direction.
                            |
     - | %                  | Find the next bracket and go to its match
                            |   (also with `{'|`}' and `['|`]').
                            |
     - | :[x]               | To the line specified by x.
                            |
     - | :[x]+[n]           | To n-th (default 1st) line after line specified
                            |   by x.
                            |
     - | :[x]-[n]           | To n-th (default 1st) line before line specified
                            |   by x.

Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: February 28, 2008