Sometimes, I have a text file like this in Emacs:
some text 123 17
other text 1 0
still more 12 8
last one 1234 123
I would like to right-align the numbers (using spaces), changing it into something like this:
some text 123 17
other text 1 0
still more 12 8
last one 1234 123
How can this be done in Emacs?
align-regexp can do this. Mark the region, and then use:
C-uM-x align-regexp RET \(\s-+[0-9]*\)[0-9] RET -1 RET 4 RET y
That should be the simplest approach.
(Edit: In fact, you don't even need to separate out that final digit; \(\s-+[0-9]+\) works just as well for the regexp.)
See the interactive prompts and C-hf align-regexp RET and the align-rules-list variable for what that is actually doing.
The noteworthy part is that by specifying a negative number for the group, align-regexp sets the justify attribute:
`justify'
It is possible with `regexp' and `group' to identify a
character group that contains more than just whitespace
characters. By default, any non-whitespace characters in
that group will also be deleted while aligning the
alignment character. However, if the `justify' attribute
is set to a non-nil value, only the initial whitespace
characters within that group will be deleted. This has
the effect of right-justifying the characters that remain,
and can be used for outdenting or just plain old right-
justification.
Alternatively the various table-editing options can also deal with this (e.g. org, ses, table-capture/release), or you could do it with an elisp replacement pattern.
e.g. The following should do more or less what you're looking for, provided that the file is already using spaces for alignment (you can use untabify to remove the tabs if not), and that all lines are the same length (i.e. trailing spaces are needed on some lines if the final column is of varying length).
C-M-% \([0-9]+\)\([[:space:]]+\) RET \,(format (concat "%" (number-to-string (1- (length \&))) "d ") (string-to-number \1)) RET
Related
In Emacs, I would like to search certain characters and replace them. They can be separated by their unicode number. For example, below 3 characters has different unicode number.
á(#xe1), ⓐ(#x24d0), 𝒶(#x1d4b6)
What if I want to search characters between the range #x1d000 to #x1dfff, and then I will use regxp replace to add a double quote("") for each of these characters?
First of all, you can enter Unicode characters by their hexadecimal codes using the key binding C-x 8 C-m (the command is called insert-char). So type C-x 8 C-m, enter 1d000, and then hit RET to insert the character with Unicode code point 1d000.
Then we can use this to search and replace.
Type C-M-% to run the command query-replace-regexp
For the search expression, enter [, then C-x 8 C-m 1d000 RET , then -, and then C-x 8 C-m 1dfff RET, and finally ]. That is, search for any character in the range between 1d000 and 1dfff. (This is similar to the "normal" regexp [a-z], which matches all characters between a and z, i.e. all lowercase characters.)
For the replace expression, enter "\&". \& is a special sequence for inserting the text matched by the search expression, so we're going to wrap every matching character in double quotes.
Then, hit y to replace matches one by one, or ! to replace all remaining matches.
I'm using wc-mode to count characters in org-mode. However, when I do org-narrow-to-subtree, my modeline continues to count all the characters in the entire org file.
How do I direct wc-mode to count only the characters in the currently active buffer, i.e. the current subtree?
You need to customize wc-modeline-format. The following line, for example, will give you the total number of words and total number of characters in the buffer (even when narrowed).
(setq wc-modeline-format "[Words: %tw, Chars: %tc]")
This answer nicely provides a way to display characters rather than tabs (in the example it suggests ">", but I confirmed it works for ".").
It uses setting the active window display table to do it.
Now my goal is to display 4 spaces as 4 dots. Using the font-face and a regular expression, I am confident that I can display it nicely. I am aware that I could have Emacs automatically use tab characters rather than whitespaces, but I always prefer to have whitespace characters in my files.
I've also looked at whitespace mode, but I tweaked many parameters and in the end I never get the simple dots (with a face that makes it a little less "jump" out).
So: how can I, rather than display tab characters as dots, display 4 spaces elegantly as dots in Emacs?
OK, here's how to mark 4 or more spaces at beginning of line
(setq whitespace-space-regexp "^\\( \\{4,\\}\\)")
And here's how to get rid of the centered dot character for space:
(setq whitespace-display-mappings
'((space-mark ?\ [?\ ] [?.])
(space-mark ?\xA0 [?\ ] [?_])
(newline-mark ?\n [?$ ?\n])
(tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t])))
The changes take effect not immediately but when you revert-buffer or
close it and open again with customizations above already set.
Right now I'm using:
(setq show-trailing-whitespace t)
In my .emacs to show trailing whitespace for my CC mode. I can't seem to figure out how to have it not show the whitespace font for whitespace only lines.
Empty lines separating indenting code are sometimes indented at the code level and sometimes not indented at all, and I don't want to draw my attention to a line I don't care to change.
I'd like to stick to built-in emacs modules, but I'm willing to use whitespace.el but it's not clear how to configure it to do this.
Since you want to use built-in modules, I'd advise using the whitespace.el link you specified - as it is shipped with Emacs 23. This answer works when using that whitespace.
As long as you have 'trailing in your 'whitespace-style variable (which it is by default), the following change to the regular expression for what indicates "trailing" whitespace should give you what you want:
(setq whitespace-trailing-regexp
"\\b\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$")
Note: It is just the default value, with the \b prepended, indicating that the whitespace should follow a word.
With
"\\b.*\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$"
the word does not need to be directly in front of the trailing whitespaces but there can be e.g. punctuation characters between them (i.e. this also highlights trailing whitespaces behind non-word characters).
Edit:
Using
"\\b.*?\\(\\(\t\\| \\|\xA0\\|\x8A0\\|\x920\\|\xE20\\|\xF20\\)+\\)$")
highlights all the trailing whitespaces and thus eliminates the drawback mentioned in comment #1.
How can I make org-mode markup work for a part of a word? For example, I'd like it to work for cases like this:
=Class=es
and this:
/Method/s
Based on my tests it seems like org-mode markup syntax works on complete words only.
These days, there is a way to do this (without using quoted HTML tags):
(setcar org-emphasis-regexp-components " \t('\"{[:alpha:]")
(setcar (nthcdr 1 org-emphasis-regexp-components) "[:alpha:]- \t.,:!?;'\")}\\")
(org-set-emph-re 'org-emphasis-regexp-components org-emphasis-regexp-components)
Explanation
The manual says that org-emphasis-regexp-components can be used to
fine tune what characters are allowed before and after the markup characters [...].
It is a list containing five entries. The first entry lists characters that are allowed to immediately precede markup characters, and the second entry lists characters that are allowed to follow markup characters. By default, letters are not included in either one of these entries. So in order to successfully apply formatting to strings immediately preceded or followed by a letter, we have to add [:alpha:] (which matches any letter) to both entries.
This is what the calls to setcar do. The purpose of the third line is to rebuild the regular expression for emphasis based on the modified version of org-emphasis-regexp-components.
I don't think you can do it so that it shows up in the buffer as bold. If you just need it so that it appears bold when you export it to html, you can use:
th#<b>is is ha#</b>lf bold
See Quoting HTML tags
No, you can't do that. I searched for the same solution before and found nothing. A (very) bad hack is to do something like *Class* es (with a whitespace).
Perhaps you can write a short message to the creator, Carsten Dominik (Homepage), and ask him for a solution. He seems to be a nice guy.
A solution that has not been mentioned is to use a unicode zero width space (U+200B) in between the desired bolded and unbolded parts of a word.
To get the desired bolding of the word "Classes":
Type 'Class*es' in the buffer (without quotes).
Move the cursor between the '*' and 'e' characters.
Press C-x 8 RET (to execute the insert-char command).
Type 'zero width space' (without quotes) and press RET.
Move the cursor to the beginning of the word and insert a '*' character.
The word "Classes" should now have the desired appearance.
Note that there is the possibility that this will cause problems when exporting.
src_latex{\textbf{Class}es and \textit{Method}s}
Building up on the previous excellent answer
I had to modify it a bit in order to make it work with spacemacs. Indeed, from the spacamacs org-layer documentation, available here, we can read
Because of autoloading, calling to org functions will trigger the
loading up of the org shipped with emacs which will induce conflicts.
One way to avoid conflict is to wrap your org config code in a
with-eval-after-load block [...]
So, I put the following lines inside my dotspacemacs/user-config ()
(eval-after-load "org"
'(progn
(setcar org-emphasis-regexp-components " \t('\"{[:alpha:]")
(setcar (nthcdr 1 org-emphasis-regexp-components) "[:alpha:]- \t.,:!?;'\")}\\")
(org-set-emph-re 'org-emphasis-regexp-components org-emphasis-regexp-components)
))