URL:         http://www.math.fu-berlin.de/~guckes/howto/vim/text.html
URL:         https://www.vim8.org/howto/text.html (mirror)
Created:     Mon Jul 14 00:00:00 CEST 1997
Last update: Wed Sep 24 11:50:49 CEST 1997

VIM - Text Objects

This page explains "text objects" (words, WORDs, paragraphs, and sentences) which you can operate on with Vim commands. And it pleads for options to define these text objects.

So far this is the email I had sent to the vimdev mailing lists (see below). If you have any comments, additional examples, whatever - let me know:

Sven Guckes [email protected]


Text Objects - Definition

Vim-5.0n: See ":help text-objects". [to be continued...]

Vim-5.0k defines the terms paragraph, section, and sentence as follows:

*sentence* A sentence is defined as ending at a '.', '!' or '?' followed by either the end of a line, or by a space. {Vi: two spaces} Any number of closing ')', ']', '"' and ''' characters my appear after the '.', '!' or '?' before the spaces or end of line. A paragraph and section boundary is also a sentence boundary. The definition of a sentence cannot be changed. *paragraph* A paragraph begins after each empty line, and also at each of a set of paragraph macros, specified by the pairs of characters in the 'paragraphs' option. The default is "IPLPPPQPP LIpplpipbp", which corresponds to the macros ".IP", ".LP", etc. (These are nroff macros, so the dot must be in the first column). A section boundary is also a paragraph boundary. Note that this does not include a '{' or '}' in the first column. *section* A section begins after a form-feed (<c-l>) in the first column and at each of a set of section macros, specified by the pairs of characters in the 'sections' option. The default is "SHNHH HUnhsh". RANT: I just love definitions which use undefined terms - NOT! What the hell is a "paragraph macro", anyway?

How are you you supposed to understand all those text object commands when you do not know what the text object really are? And just *why* can't I change the definition of "sentence"? hrmpf!

As far as I understand the definition for sentence I can describe the command ')' (jump to end of a sentence) with this search command:

/[.!?][])'"]* \=$/e Note: (1) The second character set starts with the closing bracket. It must be given as the first character - otherwise it ends the character set. (2) The meta sequence "\=" is a Vim special - it is not available with Vi. It means "zero or one occurrence", so here it means that at most one space is allowed. [I wish I could remember how to represent a space with a non-space sequence. I think it was "\032" - but this does not seem tot work everywhere.] (3) The second '/' ends the search string and the 'e' after it is a "search offset" which makes Vim move the cursor to the "end" of the found match.


From: Sven Guckes <guckes@math.fu-berlin.de> To: vimdev@prz.tu-berlin.de Cc: Bram Moolenaar <mool@oce.nl> Subject: Re: text objects - paragraphs and sentences X-Old-Subject: Re: paragraph In-Reply-To: &lt;9707140706.AA07355@smtp02.oce.nl&gt;; from Bram Moolenaar on Mon, Jul 14, 1997 at 09:14:09AM +0200 Quoting Bram Moolenaar (mool@oce.nl): &gt; What would you like to redefine paragraphs for? [...] we can just &gt; add a flag for that situation. Or is the request more generic? I would like to be able to define the terms "paragraph" *and* "sentence". This would make it possible to apply these terms according to the language or the current format of a file to apply commands to parts of the structure. Let me give some examples: Usually a "sentence" ends with a full stop (aka "dot"): This is a sentence. And this is another. VIM can keep them apart alright. But sometimes a sentence ends with three dots (aka ellipsis): You know what I mean... Delete the previous sentence and VIM delete this one, too. :-( And some sentences end with a colon: Please take a look at the VIM Home Page: http://www.math.fu-berlin.de/~guckes/vim/ Now, I would like to "teach" VIM to recognize these as sentences so I can copy/delete/move them. So far this does not seem possible. Making VIM smart about this would probably solve this problem. And I suppose it would also allow those Spanish speaking people (hol�! :-) to recognize sentences with questions which (I think) can start with an upside down question mark: "� Qu� ?" "It's alright, he's from Barcelona..." (Oh, yes, quoted and parenthized sentences are a problem, too.) And with paragraphs you often don't care whether the next seemingly empty line is really empty when it actually contains a few spaces and/or tabs. Generally, I would like to be able to define paragraphs and sentences by arbitrary delimiters, ie regular expressions. I think that editing text would be a lot more fun if you could tune those text objects to your language and style. I think we need an option for both sentences and paragraphs which lets us define a list of start and end patterns. Should I add a new page to the VIM pages about this? http://www.math.fu-berlin.de/~guckes/vim/text.objects.html ? A hint to "nroff macros" may be nice for purists, but I think learning about regular expressions is quite enough as is. ;-) Sven

Misc

Texts to be added later:

Q: Why can't there be easier names for some text objects? A: The names of text objects must not be confused with names of jump commands. This limits the number of choices.


Send feedback on this page to
Sven Guckes [email protected]