Home Explore Blog CI



neovim

2nd chunk of `runtime/doc/usr_12.txt`
40b98dbcb85db3405f50be60f9b9a6bda51d1b4ea13d0edf0000000100000fa2

==============================================================================
*12.2*	Change "Last, First" to "First Last"

You have a list of names in this form:

	Doe, John ~
	Smith, Peter ~

You want to change that to:

	John Doe ~
	Peter Smith ~

This can be done with just one command: >

	:%s/\([^,]*\), \(.*\)/\2 \1/

Let's break this down in parts.  Obviously it starts with a substitute
command.  The "%" is the line range, which stands for the whole file.  Thus
the substitution is done in every line in the file.
   The arguments for the substitute command are "/from/to/".  The slashes
separate the "from" pattern and the "to" string.  This is what the "from"
pattern contains:
							\([^,]*\), \(.*\) ~

	The first part between \( \) matches "Last"	\(     \)
	    match anything but a comma			  [^,]
	    any number of times				      *
	matches ", " literally					 ,
	The second part between \( \) matches "First"		   \(  \)
	    any character					     .
	    any number of times					      *

In the "to" part we have "\2" and "\1".  These are called backreferences.
They refer to the text matched by the "\( \)" parts in the pattern.  "\2"
refers to the text matched by the second "\( \)", which is the "First" name.
"\1" refers to the first "\( \)", which is the "Last" name.
   You can use up to nine backreferences in the "to" part of a substitute
command.  "\0" stands for the whole matched pattern.  There are a few more
special items in a substitute command, see |sub-replace-special|.

==============================================================================
*12.3*	Sort a list

In a Makefile you often have a list of files.  For example:

	OBJS = \ ~
		version.o \ ~
		pch.o \ ~
		getopt.o \ ~
		util.o \ ~
		getopt1.o \ ~
		inp.o \ ~
		patch.o \ ~
		backup.o ~

To sort this list, filter the text through the external sort command: >

	/^OBJS
	j
	:.,/^$/-1!sort

This goes to the first line, where "OBJS" is the first thing in the line.
Then it goes one line down and filters the lines until the next empty line.
You could also select the lines in Visual mode and then use "!sort".  That's
easier to type, but more work when there are many lines.
   The result is this:

	OBJS = \ ~
		backup.o ~
		getopt.o \ ~
		getopt1.o \ ~
		inp.o \ ~
		patch.o \ ~
		pch.o \ ~
		util.o \ ~
		version.o \ ~


Notice that a backslash at the end of each line is used to indicate the line
continues.  After sorting, this is wrong!  The "backup.o" line that was at
the end didn't have a backslash.  Now that it sorts to another place, it
must have a backslash.
   The simplest solution is to add the backslash with "A \<Esc>".  You can
keep the backslash in the last line, if you make sure an empty line comes
after it.  That way you don't have this problem again.

==============================================================================
*12.4*	Reverse line order

The |:global| command can be combined with the |:move| command to move all the
lines before the first line, resulting in a reversed file.  The command is: >

	:global/^/move 0

Abbreviated: >

	:g/^/m 0

The "^" regular expression matches the beginning of the line (even if the line
is blank).  The |:move| command moves the matching line to after the imaginary
zeroth line, so the current matching line becomes the first line of the file.
As the |:global| command is not confused by the changing line numbering,
|:global| proceeds to match all remaining lines of the file and puts each as
the first.

This also works on a range of lines.  First move to above the first line and
mark it with "mt".  Then move the cursor to the last line in the range and
type: >

	:'t+1,.g/^/m 't

==============================================================================
*12.5*	Count words

Sometimes you have to write a text with a maximum number of words.  Vim can
count the words for you.
   When the whole file is what you want to count the words in, use this
command: >

	g CTRL-G

Do not type a space after the g, this is

Title: Vim User Manual: Clever Tricks - Reformatting Names, Sorting Lists, Reversing Lines, and Counting Words
Summary
This section demonstrates more clever tricks in Vim. It explains how to reformat a list of names from 'Last, First' to 'First Last' using backreferences in a substitute command. It then shows how to sort a list of files in a Makefile using an external sort command and addresses the issue of backslashes at the end of the lines. It further shows reversing the order of lines using global and move commands, and how to count words in a file using the 'g CTRL-G' command.