emacs - text manipulation
- Undo
- Vim Golf challenges
- Add semicolons at the end of each non blank line
- Prepend an asterisk to every non-blank line in the input file
- Indentation - Remove all spaces in front of these lines
- Insert white spaces before a line
- Insert a Column of Numbers
- Insert A to Z Using rectangle-number-lines
- Replace unwanted white spaces
- Case conversion commmands
- Copy and paste text
- Replace functions
- Rectangle commands
- Typing New line in the mini buffer
- Escape pipe-character in org-mode
- Refactor variable names in programming
Undo
How to undo in emacs?
Ctrl /
Ctrl _
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
- Put the cursor at the first line. Ctrl + S
- Move the cursor to the last line
- Alt X - mc/editlines
- Ctrl A
- Space key
- 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
- Put cursor to beginning of “cat”
- M-x set-mark-command 【Ctrl+Space】
- Move cursor to beginning of “bird”
- M-x rectangle-number-lines
to this
21 cat
22 dog
23 bird
Solution
- Move cursor to before cat.
- set-mark-command 【Ctrl+Space】.
- Move cursor to before bird.
- universal-argument 【Ctrl+u】
- M-x rectangle-number-lines. It will prompt you to enter arguments.
- Type 21
- 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:
- Move cursor to before cat.
- set-mark-command 【Ctrl+Space】.
- Move cursor to before bird.
- universal-argument 【Ctrl+u】
- M-x rectangle-number-lines. It will prompt you to enter arguments.
- Type 65 (Letter A has Unicode codepoint 65.).
- 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.
- Ctrl y would yank the more recent kill.
- But the previous text is not lost.
- You can get back to it using the Meta y command.
- After you have done
~Ctrl y~
to get the most recent kill, typing~Meta y~
replaces that yanked text with the previous kill. - Typing
Meta y
again and again brings in earlier and earlier kills. - When you have reached the text you are looking for, just keep that. You do not have to do anything else.
- Just go on with your editing, leaving the yanked text where it is.
- 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.
-
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
-
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
-
https://www.gnu.org/software/emacs/manual/html_node/emacs/Rectangles.html
-
http://xahlee.info/emacs/emacs/emacs_string-rectangle_ascii-art.html
Using these, we can do things like:
- Add a prefix to every line
- Delete the first few n chars of every line
- Delete a column of text
- replace-rectangle
- Paste Rectangle
yank-rectangle
Paste a column of text (after you usedkill-rectangle
). - 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.
C-o
C-q C-j
C-q
for quoted-insertC-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: