Emacs is wonderful for text editing and related stuff with text. It has many modes which helps in editing and managing text, which help increase the productive overall, here I explored some of the major modes and commands which can quickly help for editing text.

Text Editing in Emacs

Emacs has several major modes for handelling text, there is text mode for ordinary text which customizes emacs in small ways for syntactic convention of text. Outline mode provides special commands to handle text, Org Mode extends Outline mode and turns emacs into a full fledged organizer: we can manage TODO Lists, store notes and publish them in many formats and I would speak more about this in my coming days learnings. TeX and LaTeX mode contains embedded commands; HTML mode for HTML and SGML Modes; nxml-mode for XML Mode; Nroff Mode for Groff and Nroff Mode.

It is also possible to edit pictures made out of text characters, also referred as *“ASCII art”* using Picture mode.


There are several commnads in emacs for operating on words and many of these I have already covered in my Day-1 and Day-4, but let me redo, so I will remeber it for a long time. Again, the M refers the Meta i.e the Alt or Esc key on the keyboard and is followed by the character keys.

</tr> </tbody> </table> *NOTES:* *1. These keys parrellel with the character-based movements* *2. The numeric arguments serves as the repeat count and the negative arguments would make the commands move in the opposite direction.* *3. M- deletes the word to the point where M-b would end, it will kill the comma and space as well, M-b M-d to just kill the word but not the comma.* *4. If we choose there keybindings in our shell **(set -o emacs)**, these word keybindings can help us in moving around the long commands.* ## Sentences The meta keys are also helpful in moving around the sentences.
Key bindings commands description
M-f forward-word Self descriptive as it moves forward on word
M-b backward-word it moves one word back.
M-d kill-word kills up to the end of the word
M-</td> backward-kill-word kill backward to the beginning of the word.
M-@ mark-word Mark to the end of the word
M-t transpose-words Transpose the word and drag across others
M-= count-words-region gives the count of the words in the region.
</tr> </tbody> </table> ### Quoting from EMACS Manual \#+BEGINSRC Quote The sentence commands assume that you follow the American typist's convention of putting two spaces at the end of a sentence. That is, a sentence ends wherever there is a \`.', \`?' or \`!' followed by the end of a line or two spaces, with any number of \`)', \`]', \`'', or \`"' characters allowed in between. A sentence also begins or ends wherever a paragraph begins or ends. It is useful to follow this convention, because it allows the Emacs sentence commands to distinguish between periods that end a sentence and periods that indicate abbreviations. \\#+ENDSRC Quote *NOTE:* *1. To use just one space between sentences, set the variable sentence-end-double-space to nil and to make the sentence commands stop for single spaces.* *2. The variable sentence-end controls how to recognize the end of the sentence; its value is a regular expression, which is used to match the last few characters of the sentence.* *3. Some languages such as Thai, doesn't end with periods; such cases can be handeld by setting variable sentence-end-without-period to t.* ## Paragraphs
Keybindings commands description
M-a backward-sentence This will move the point to the beginning of the sentence
M-e forward-sentence Will move forward to the end of sentence.
M-k kill-sentence Kill forward to the end of the sentence.
C-x </td> backward-kill-sentence kill back to the beginning of the sentence.
Keybindings Commands Description
M-{ backward-paragraph move to the beginning of paragraph.
M-} forward-paragraph Move to the end of the next paragraph.
M-h mark-paragraph put point and mark around this or next paragraph.
M-h C-w   kills the paragraph around or after point.
*Notes:* *1. The definition of a paragraph boundary is controlled by the variables **paragraph-separate** and **paragraph-start**. The value of these are regular expression.* *2. In fundamental mode, Paragraph-start is \*"\f\\\\|[ \t]\*$"\* and matches lines that either start or separate paragraph.* *3. Paragraph separate is \*"[ \t\f]\*$"\* and matches lines that separate paragraph without being part of any paragraph.* ## Pages The files are divided into pages delimited by the "formfeed character" (ASCII code 2, denoted as ) and in emacs is escape sequence .
Keybindings Commands Description
M-x What-page   Display the page number and line number within that page
C-x [ backward-page Move point to the previous page boundary.
C-x ] forward-page Move point ot next page boundary
C-x C-p mark-page put point and mark around this page.
C-x C-p C-w   select and kill the page
C-x l count-lines-page Count the lines in this page.
*Note:* *page-delimiter control where the page begins. Its a regular expression \*"f"\* which matches a beginning of a line that separate pages.* ## Filling Text Filling text means breaking up the lines that fit a specified width. Emacs does the filling in 2 ways. **Auto Fill Mode** inserts text with self-inserting characters and automatically fills it. **Explicit fill Commands** can be used for editing texts. ### Auto Fill This is a minor mode in which the lines are broken automatically when they become too wide, breaking up of lines only happens when you type or . 'M-x auto-fill-mode' : Toggles the Auto Fill mode. '/' : Break lines when appropriate. From the command line the auto-fill mode enables or disables the mode for current buffer. From LISP *auto-fill* is enabled when called with omitted or nil argument. for enabling in the major modes, add auto-fill-mode in mode hooks. When the mode it enabled it will show **Fill** in the mode line. This mode breaks any line which goes longer than the desired width by pressing or , and to insert a SPACE or any newline without breaking the lines tye **C-q ** or **C-q C-j** and **C-o** for inserting new line. Auto fill mode does not refill the paragraphs; it break lines but does not merge lines, editing in the middle of the paragraph can result in paragraph which is not correctly filled. To fill them correctly we may need to use explicit Fill Commands. ### Fill Commands
Key bindings Command Description
M-q fill-paragraph Fill current paragraph
C-x f set-fill-column Set the fill column
M-x fill-region fill-region Fill each paragraph in the region.
M-x fill-region-as-paragraph   Fill the region consider it as paragraph.
M-o M-s   Center a line.
*Notes:* *1. M-q and fill-region uses the usual emacs criteria for finding paragraph boundaries, which refills everything between point and mark as single paragraph.* *2. Numeric argument to **M-q** justifies the text as well as fill it.* *3. To Remove extra spaces, use M-q with no argument.* *4. The default vault for fill column is 70.* *5. **C-x f** (sets-fill-column) sets the numeric value for fill column. With just C-u as argument it sets fill-column to the current horizantal position of point.* *6. By default, emacs consider a period followed by two spaces or by a newline as the end of sentence; a period with just one space will be called as abbrevation and not the end of the sentence.* *7. Setting 'sentence-end-double-space' to nil the fill commands will break a line after a period followed by a period and a space.* ### The Fill Prefix
Keybindings Command Description
C-x . set-fill-prefix Set the fill prefix
M-q fill-paragraph Fill a paragraph using current fill prefix
M-x fill-individual-paragraphs   Fill the region, considering each change of indentation as starting a new paragraph.
M-x fill-nonuniform-paragraphs   Consider only paragraph separator lines as starting a new paragraph.
*Notes:* *1. Move to a line which starts with desired prefix, put point at the end of the prefix and type C-x .* *2. To turn off a prefix, specify an empty prefix: type C-x . with point at the beginning of line.* *3. The fill prefix is stored in fill-prefix variable. if the value is nil, then there is no fill prefix.* This feature allows paragraphs to be filled so each line starts with a special string of characters (such a sequence of spaces, giving and indented paragraph). you can specify fill prefix explicitly; otherwise emacs tries to deduce automatically. ### Adaptive Fill. The fill command deduce the proper fill prefix for a paragraph automatically in some cases; either white spaces or certain precaution characters at the beginning of line are propagated to all lines of the paragraph. If the paragraph has two or more lines, the fill prefix is taken from the paragraph's second line, but only if it appears on the first line as well. If a paragraph has just one line, fill commands may take a prefix from that line. The decision is complicated because there are three reasonable things to do in such a case: - Use the first line's prefix on all the lines of the paragraph. - Indent subsequent lines with whitespace, so that they line up under the text that follows the prefix on the first line, but don't actually copy the prefix from the first line. - Don't do anything special with the second and following lines. All three of these styles of formatting are commonly used. So the fill commands try to determine what you would like, based on the prefix that appears and on the major mode. Here is how. If the prefix found on the first line matches \`adaptive-fill-first-line-regexp', or if it appears to be a comment-starting sequence (this depends on the major mode), then the prefix found is used for filling the paragraph, provided it would not act as a paragraph starter on subsequent lines. Otherwise, the prefix found is converted to an equivalent number of spaces, and those spaces are used as the fill prefix for the rest of the lines, provided they would not act as a paragraph starter on subsequent lines. In Text mode, and other modes where only blank lines and page delimiters separate paragraphs, the prefix chosen by adaptive filling never acts as a paragraph starter, so it can always be used for filling. The variable \`adaptive-fill-regexp' determines what kinds of line beginnings can serve as a fill prefix: any characters at the start of the line that match this regular expression are used. If you set the variable \`adaptive-fill-mode' to \`nil', the fill prefix is never chosen automatically. You can specify more complex ways of choosing a fill prefix automatically by setting the variable \`adaptive-fill-function' to a function. This function is called with point after the left margin of a line, and it should return the appropriate fill prefix based on that line. If it returns \`nil', \`adaptive-fill-regexp' gets a chance to find a prefix. ## Case
Keybindings Commands Description
M-l downcase-word Convert the following word to lower case.
M-u upcase-word Convert the following word to upper case.
M-c capitalize-word Capitalize the following word.
C-x C-l downcase-region Convert region to lowercase.
C-x C-u upcase-region Convert region to upper case.
*Note:* *1. When a negative argument, the case conversion applies to appropriate no. of words before point, but do not move the point, its convinent if I typed something in wrong case and want to correct the case.* *2. In case the command to change case is given in the middle of the word, the case change would happen from the point to the end of the word.* ## Text Mode This is the major mode for editing the text language, any file ending with .txt will automatically enables this mode; and this mode can also be called by M-x text-mode. Blank lines and page delimiters separate paragraphs, and as a result paragraphs can be indented, and adaptive filling determines what indentation to use when filling a paragraph. The usually inserts whitespace up to the next tab stop, instead of indenting the current line. Text mode turns off comments features except when you explicitly invoke them. It changes so that the single quotes are considered part of the words. But if a word starts with a single quote it is treated as a prefix for the purpose of capitalization. Entering text-mode invokes text-mode-hook. ## Outline Mode This is major mode derived from Text mode, which is specialized for editing outlines; it provides commands to navigate between entries in the outline structure. Type M-x outline-mode to switch to outline mode, entering this mode runs the hook text-mode-hook followed by outline-mode-hook. When in outline mode use command to make a line invisible it disappears the line and ellipsis(three periods in a row) is displayed at the end of the previous visible line. Editing command on lines, such as C-n and C-p treat the text of the invisible line as part of the previous visible line. outline-minor-mode toggle outline minor mode in the current buffer, it is a buffer-local minor mode which provides the same commands as the major mode for the current buffer, this provides special keybindings with C-c prefix, and C-c @ as the prefix in minor-mode. ### Outline Format Outline mode assumes the the lines in the buffer are of two types: "heading lines" and "body lines" Heading line: start with one or more asterisk ('\*'); the number of asterisks determines the depth of the heading in the outline structure. Body Line: Any line which is not a heading line is a body line. Body line belong with a preceding heading line. A heading line and a body line collectively is called as an entry. ### Outline mode commands
Key bindings Commands Description
C-c C-n outline-next-visible-heading Move point to the next visible heading
C-c C-p outline-previous-visible-heading Move point to the previous visible heading.
C-c C-f outline-forward-same-level Move point to the next visible heading line at the same level.
C-c C-b outline-backward-same-level Move point to previous heading line at the same level.
C-c C-u outline-up-heading Move point up to a lower-level.
### Outline visibility commands
Keybindings Commands Description
C-c C-c hide-entry Make the current heading line's body invisible
C-c C-e show-entry Make the current heading line's body visible
C-c C-d hide-subtree Make everything under the heading invisible excluding the heading
C-c C-s show-subtree Make everything under the current heading visible
C-c C-l hide-leaves Make the body of the current heading line, and all of its subheadings
C-c C-k show-branches Make all subheading of the current heading line, at all levels, visible
C-c C-i show-children Make immediate subheadings of the current heading line visible
C-c C-t hide-body Make all body lines in the buffer invisible
C-c C-a show-all Make all line in the buffer visible
C-c C-q hide-sublevels Hide everything except the top N level of heading lines
C-c C-o hide-other HIde everything except the heading or the body that point is in
And most of these commands may work differently in org-mode, the org-mode is an advance implementation of outline-mode. I will be writing about this mode in my later days. It may confuse me at a later stage as mainly I use org-mode for my purposes of note taking and writing this blog. Let me provide some brief introduction to the org-mode. ## Org Mode Org mode, as in outline mode, each entry has a heading line that starts with one or more asterisk (\*); Any line beginning with a '#' is considered as a comment. and it also provides commands to view and manipulate the outline structure. The simplest of these commands is (org-cycle). If invoked on heading line, it cycles through different visiblity states of that subtree: 1. Showing only the heading line. 2. Show only the heading lines and the heading lines of its direct children. 3. Show the entire subtree. The (org-shifttab) in an org mode cycle visiblity of entire online subtree 1. Show only top-level heading lines. 2. Show all heading lines but no body lines. 3. Show everything. We can move the entire entry up and down in the buffer, including its body lines and subtree, by typing M- (org-metaup) or M- (org-metadown) on the heading line. Similarly, it can promote or demote the heading line with M- (org-metaleft) and M-<right (org-metaright). For details, The Org Mode Manual. ### Org as an Organizer You can tag an Org entry as a "TODO" by typing **C-c C-t** (org-todo) anywhere in the entry. This adds *TODO* keyword and typing the keyword **C-c C-t** again switches the keyword to *DONE*; press **C-c C-t** again and it will remove the keyword entirely. They keywords can be customized using the variable 'org-todo-keywords' With the possiblity of marking an entry as TODO, we can also attach a date to it by typing **C-c C-s** (org-schedule), This prompts for a date by popping a calendar, and then adds the tag 'SCHEDULED', together with the selected date, beneath the heading line. **C-c C-d** (org-deadline) have the same effect except it adds the tag 'DEADLINE'. Once you have some planned TODO items, you can add that file to the list of "agenda fileS" by typing **C-c [** (org-agenda-file-to-front). It allows us to maintain multiple agenda files. The list of these agenda files are stored in the variable 'org-agenda-files'. Typing M-x org-agenda lets view items coming from agenda file, this command prompts for what we want to see: a list of things to do this week, a list of TODO items with specific keywords. ### Org as an authoring system To format org notes nicely and to prepare them for export and publication, **C-c C-e** (org-export) to export buffer and prompts for a export format; includes HTML, LaTeX, OpenDocument(.odt), and PDF. To export several files at once to a specified directory, either locally or over the network, we can define a list of projects through the variable 'org-publish-project-alist' Org supports a simple markup scheme for applying text formatting to exported documents: - This text is *emphasized* - This text is **in bold** - This text is underlined - This text uses `a teletype font` > \`\`This is a quote.'' This is an example. ## TeX Mode Emacs provides special major modes for editing files written in TeX and its related formats. TeX is a powerful text formatter written by Donald Knuth; like GNU Emacs. Latex is a simplified input format for TeX implemented using TeX macros. DocTeX is a special file format in which the LateX sources are written, combining sources with documentation. TeX mode has four variants: 1. Plain TeX mode 2. LateX mode. 3. DocTeX mode. 4. SliTeX mode. These distinc major modes differ only slightly, and are designed for editing the four different format. Emacs selects the appropriate mode by looking at the contents of the buffer. Emacs chooses the mode specified by the variable 'tex-default-mode'; its default value is latex-mode. If Emacs doesn't get it right you can select the correct variant of TeX using the command 'M-x plain-tex-mode', 'M-x latex-mode', 'M-x slitex-mode', or 'M-x doctex-mode'. ## SGML and HTML Modes HTML mode is slightly customizable variant of SGML mode. </tr> </tr> </tbody> </table> There is a major mode for editing XML documents called as nXML mode. This is a mode which can recognize any XML schema and use them to provide completion on XML elements via 'M-, as well as on the fly XML validation with error highlighting. To enable nXML mode for existing buffer type **M-x nxml-mode** or **M-x xml-mode** For files with extension .xml nxml-mode is used by default; and for files with xhtml html-mode is used. ## Nroff mode This is a major mode derived from Text mode and is specialized for editing nroff files e.g. UNIX man pages. **M-x nroff-mode** enters into this mode, and runs the hook 'text-mode-hook', then 'nroff-mode-hook'; nroff command lines are treated as paragraph separators, pages are separated by '.bp' commands, and comments start with backslash-doublequote (\\")
Keybindings Command Description
C-C C-n sgml-name-char Interactively specify a special character and insert the SGML '&' command for that character
C-c C-t sgml-tag Specify a tag and its attribute.
C-c C-a sgml-attributes insert attribute value for the current tag
C-c C-f sgml-skip-tag-forward skip across a balanced tag group.
C-c C-b sgml-skip-tag-backward skip backward across a balanced tag group.
C-c C-d sgml-delete-tag Delete the tag
C-c ? TAG </td> sgml-tag-help display the description and the meaning of the tag
C-c / sgml-close-tag insert a close tag for the innermost undetermind tag
C-c 8 sgml-name-8bit-mode Toggle a minor mode in which Latin-1 characters insert the corresponding SGML commands.
C-c C-v sgml-validate run a shell commands to validate the current buffer as SGML.
C-c </td> sgml-tags-invisible Toggle the visiblity of existing tags in the buffer.
Keybindings Commands Description
M-n forward-next-line move to the beginning of next line, which is not a nroff command
M-p backward-next-line same but moves up
M-? count-text-lines display in the echo area the number of text lines.
**M-x electric-nroff-mode** toggles the Electric Nroff mode and is a minor-mode and can be used with Nroff mode. ## Enriched Text This is a minor mode for editing formatted text files in a WYSIWYG ("what you see is what you get") fashion. When Enriched mode is enabled we can apply various formatting properties to the text in the buffer. This mode is typically used by Text mode and is not compatible with Font Lock Mode which is used by many major modes, for syntax highlighting. When you save a buffer with enriched mode enabled, it is saved using the 'text/enriched' format, including the formatting indentation. To create a new file of formatted text, visit file and type **M-x enriched-mode** ## Editing Text Based Tables The table package provides commands to easily edit the text-based tables. A "table" consist of t a rectangular text area which is divided into "cells". Each cell must be one character wide and one character high, not counting its border lines. A cell can be subdivided into more cells, but they cannot overlap.
Commands Default Description
table-cell-vertical-char Pipe The character used for vertical lines.
table-cell-horizontal-chars - = character used for horizontal lines.
table-cell-intersection-char + character used for intersection of horizontal and vertical lines
The following are examples of invalid tables:
a b c From left to right: 1. Overlapped cells or non-rectangular cells are not allowed. 2. The border must be rectangular. 3. Cells must have a minimum width/height of one character. ## Two column editing Two columns lets you convinently edit two side-by-side columns of text. It uses two side-by-side windows, each showing its own buffer. There are three ways to enter two column mode: 1. 2 or C-x 6 2 - Enter two columns mode with the current buffer on the left and on the right, a buffer whose name is based on the current buffer's name. 2. s or C-x 6 s - Split the current buffer, which contains two column text, into two buffers and display them side by side. 3. b BUFFER or C-x 6 b BUFFER - Enter two columns mode using the current buffer as the left-hand buffer, and using buffer BUFFER as the right-hand buffer.