emacs - text manipulation

Undo

How to undo in emacs?

  1. Ctrl /
  2. Ctrl _
  3. Ctrl x u

Vim Golf challenges

Add semicolons at the end of each non blank line

Add semicolons

Simply add a semicolon at the end of each non-blank (non-empty) line

Before:

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
Intent intent = getIntent()
String text = intent.getStringExtra("text")

TextView view = findViewById(R.id.textView2)
view.setText(text)

After:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String text = intent.getStringExtra("text");

TextView view = findViewById(R.id.textView2);
view.setText(text);

TODO: This is pending

Prepend an asterisk to every non-blank line in the input file

Prepend an asterisk to every non-blank line in the input file.

Before

This is a
very short

file, but it is
still
full

of

surpises.
i

After

*This is a
*very short

*file, but it is
*still
*full

*of

*surpises.
*i

TODO: This is pending

Indentation - Remove all spaces in front of these lines

Before

Example line
    This is part of the parsed line
    Thats goes one
End of line

After

Example line
This is part of the parsed line
Thats goes one
End of line

TODO: This is pending

Insert white spaces before a line

Before

- select a chunk of code using capital V and the arrow keys (or j, k)
- type colon
- then type s/^/   /
- hit return

After

- select a chunk of code using capital V and the arrow keys (or j, k)
- type colon
- then type s/^/   /
- hit return

Answer

  1. Put the cursor at the first line. Ctrl + S
  2. Move the cursor to the last line
  3. Alt X - mc/editlines
  4. Ctrl A
  5. Space key
  6. Enter

Insert a Column of Numbers

http://xahlee.info/emacs/emacs/emacs_string-rectangle_ascii-art.html

Insert sequence of numbers in a vertical column.

try change this

cat
dog
bird

to this

1 cat
2 dog
3 bird

Solution

  1. Put cursor to beginning of “cat”
  2. M-x set-mark-command 【Ctrl+Space】
  3. Move cursor to beginning of “bird”
  4. M-x rectangle-number-lines

to this

21 cat
22 dog
23 bird

Solution

  1. Move cursor to before cat.
  2. set-mark-command 【Ctrl+Space】.
  3. Move cursor to before bird.
  4. universal-argument 【Ctrl+u】
  5. M-x rectangle-number-lines. It will prompt you to enter arguments.
  6. Type 21
  7. Select the default %2d

Insert A to Z Using rectangle-number-lines

try change this

cat
dog
bird

to this

A. cat
B. dog
C. bird

Solution:

  1. Move cursor to before cat.
  2. set-mark-command 【Ctrl+Space】.
  3. Move cursor to before bird.
  4. universal-argument 【Ctrl+u】
  5. M-x rectangle-number-lines. It will prompt you to enter arguments.
  6. Type 65 (Letter A has Unicode codepoint 65.).
  7. Remove the default %2d , type %c. (the “%c” is for character format)

Replace unwanted white spaces

If you have text that looks like this

Percent                      I4-15
b Percent and Fractions                   16-19
c Percent and Decimals                    20
d EstimatingPercents                     21

and you want to replace “multiple whitespaces” with a single whitespace

The double quotes are shown here only to highlight that there are two whitespaces before the + in the first part of the command. And that there is one whitespace in the second part of the command. We should not include double quotes when running the emacs command.

M-x replace-regexp <white-space><white-space>+ ENTER <white-space>  ENTER

Case conversion commmands

https://www.gnu.org/software/emacs/manual/html_node/emacs/Case.html

command function name description
M-l downcase-word Convert following word to lower case.
M-u upcase-word Convert following word to upper case.
M-c capitalize-word Capitalize the following word.
C-x C-l downcase-region Convert region to lower case.
C-x C-u upcase-region Convert region to upper case

Convert a word from smaller case to upper case, upper case to smaller case or invert the case of each of the letters in the word.

https://github.com/akicho8/string-inflection

command result
(string-inflection-underscore-function “EmacsLisp”) ; => “emacs_lisp”
(string-inflection-pascal-case-function “emacs_lisp”) ; => “EmacsLisp”
(string-inflection-camelcase-function “emacs_lisp”) ; => “emacsLisp”
(string-inflection-upcase-function “emacs_lisp”) ; => “EMACS_LISP”
(string-inflection-kebab-case-function “emacs_lisp”) ; => “emacs-lisp”
(string-inflection-capital-underscore-function “emacs_lisp”) ; => “Emacs_Lisp”

Copy and paste text

Copying text

Press Ctrl-Space to mark start of block.

Move cursor until end of block.

Press Alt-w to copy.

Press Ctrl-w to cut.

Pasting/Yanking text

Move to insert position.

Press Ctrl-y to paste.

It reinserts the last killed text, at the current cursor position.

Kill ring

What do you do if you have some text you want to yank back, and then you kill something else?

yank-pop

This function gives a visual selection option in the mini buffer for all the previously copied text.

Cycle through previous kill buffers.

Alt-y

This command replaces the just-yanked kill buffer with the contents of the previous kill buffer. It only works after a yank or yank-pop command.

  1. Ctrl y would yank the more recent kill.
  2. But the previous text is not lost.
  3. You can get back to it using the Meta y command.
  4. After you have done ~Ctrl y~ to get the most recent kill, typing ~Meta y~ replaces that yanked text with the previous kill.
  5. Typing Meta y again and again brings in earlier and earlier kills.
  6. When you have reached the text you are looking for, just keep that. You do not have to do anything else.
  7. Just go on with your editing, leaving the yanked text where it is.
  8. If you ~Meta y~ enough times, you come back to the starting point (the most recent kill).

Replace functions

How to not replace all occurances of a word? Do not use replace-string. Use query-replace and query-replace-regex instead. And use the ? operator. It is very helpful.

There are multiple ways to do this.

  1. There are built in functions for replacing text.

    Meta x replace-string
    Meta x replace-regexp
    Meta x replace-rectangle
    

    Using these, it is easy to perform replace operations.

    Example: How to insert something at the beginning of every line? Meta x replace-regexp - ^ - String that you want to put at the beginning of each line

    How to delete something at the beginning of every line? Meta x replace-regexp - String that you want to delete at the beginning of each line - (replace with nothing) - RET

  2. Use multiple markers package.

    Mark the lines that you want to change and use the functions
    mc/edit-beginning-of-lines
    mc/edit-end-of-lines
    mc/edit-lines
    

    Using this, we can also do things like deleting/changing a common word/text in multiple lines in the file.

Rectangle commands

  1. https://www.gnu.org/software/emacs/manual/html_node/emacs/Rectangles.html

  2. http://xahlee.info/emacs/emacs/emacs_string-rectangle_ascii-art.html

    Using these, we can do things like:

    1. Add a prefix to every line
    2. Delete the first few n chars of every line
    3. Delete a column of text
    4. replace-rectangle
    5. Paste Rectangle yank-rectangle Paste a column of text (after you used kill-rectangle).
    6. Insert a Column of Numbers rectangle-number-lines

Typing New line in the mini buffer

How to replace a character in the mini buffer with a newline?

Or, how to insert a new line in the mini buffer?

There are a few ways to put a newline into the minibuffer.

  1. C-o
  2. C-q C-j
    1. C-q for quoted-insert
    2. C-j is a newline.

Escape pipe-character in org-mode

Use a broken-bar character, ¦, Unicode 00A6 BROKEN BAR. This may or may not work for your specific needs, but it’s a good visual approximation.

Refactor variable names in programming

Use this:

https://github.com/victorhge/iedit