Skip to main content
Share Your Experience: Take the 2024 Developer Survey
33 votes

How to prettify/format an XML buffer?

The built-in sgml-mode has a command to do this: sgml-pretty-print. If you're in nxml-mode it looks like you need switch to sgml-mode first. You could write a command to temporarily switch to sgml-...
glucas's user avatar
  • 20.6k
22 votes
Accepted

Can I format cells in an org-mode table differently depending on a formula?

I got whole-table formatting to work with some Elisp: A formula is evaluated for the cells' contents, and converted to a color using a gradient. Org file including code: #+name: item-prices |-------...
Vladimir Panteleev's user avatar
16 votes

How to prettify/format an XML buffer?

Mark your xml and do: M-x sgml-pretty-print Or just run the command without a marked region to prettify the whole buffer.
draxil's user avatar
  • 161
11 votes

Command that formats (prettifies) Elisp code

Listing Elisp formatting tools here for completeness: Elisp-Autofmt Emacs integration to run on-save.Formats files following this style by default (available on melpa). lispy includes auto-formatting ...
ideasman42's user avatar
  • 8,826
8 votes
Accepted

Formatting of usepackage commands in LaTeX document

If you want a command to stay in a line on its own, add it to LaTeX-paragraph-commands: (setq LaTeX-paragraph-commands '("documentclass" "usepackage")) But, honestly, I didn't ever see anyone trying ...
giordano's user avatar
  • 3,255
7 votes

How to prettify/format an XML buffer?

write this into your ~/.emacs.d/init.el: (require 'sgml-mode) (defun ninrod/reformat-xml () (interactive) (save-excursion (sgml-pretty-print (point-min) (point-max)) (indent-region (...
ninrod's user avatar
  • 1,506
6 votes

Automatically formatting SQL code

If you don't mind calling out to a python process, then this works (pip install sqlparse): (defun sqlparse-region (beg end) (interactive "r") (shell-command-on-region beg end "python -c '...
5fec's user avatar
  • 196
6 votes

I want to make format out its result to a variable instead of printing it

#'format actually returns the formatted value; it doesn't print it out. However, many ways of evaluating code do print the returned value -- e.g., #'eval-last-sexp. Here's some code (run in ielm) ...
zck's user avatar
  • 9,102
5 votes
Accepted

Why does M-x underline-region doesn't work?

It appears to work as advertised. Note that what looks like ^H in the emacs buffer is really a backspace character. If you were to print _^HS on an old fashioned dot matrix printer, it would print the ...
Harald Hanche-Olsen's user avatar
5 votes

How to prettify/format an XML buffer?

In nxml-mode, C-x H will mark the whole buffer and TAB will indent the selection.
SergiyKolesnikov's user avatar
5 votes
Accepted

How to set project-specific javscript indentation using js2-mode

Perhaps there is a dot-file that I can add to my project's root folder where I can configure settings? Yes. It is called "Per-Directory Local Variables" aka ".dir-locals.el". Create a file named ....
Hariharan's user avatar
  • 473
5 votes

Can I format cells in an org-mode table differently depending on a formula?

Emacs provides the function hi-lock-face-buffer M-s h rwhich highlights a regular expression in the buffer as you type. All we need is a regular expression which matches any number which is not 1 or ...
Rip Torn's user avatar
5 votes
Accepted

clang-format malfunctioning(?) Or am I not fully understanding how clang-format works?

I found the same thing (not following my styles as defined in a .clang-format file, which happens to be in the parent directory in my case), and I think that the problem is in the way that the assume-...
William Gallafent's user avatar
5 votes
Accepted

Disabling quote conversion in docstrings

Basil's answer covers the needs of the end-user. If you're writing elisp docstrings for other people, however, you (unfortunately) can't rely on everyone configuring text-quoting-style to grave, so ...
phils's user avatar
  • 51.6k
5 votes

How to run a custom formatting tool on save?

This can be done using: call-process-region to run the command and catch the stdout/stderr. replace-buffer-contents to update the region without causing the entire buffer to be replaced. This is ...
ideasman42's user avatar
  • 8,826
4 votes

Automatically formatting brackets

Another, terser, way is to use a smartparens insertion spec. (sp-local-pair 'c-mode "{" nil :post-handlers '(:add ("||\n[i]" "RET")))
PythonNut's user avatar
  • 10.4k
4 votes
Accepted

How do you correctly escape characters in string for example %

Instead of: (message (format "test %s" dc-str-addresses)) You want: (message "test %s" dc-str-addresses) message accepts the same arguments as format: (message FORMAT-STRING &rest ARGS) ...
phils's user avatar
  • 51.6k
4 votes

How can I use par as my paragraph formatter

Here I have taken what @phils had posted and have modified it to: Take more par options Apply par formatting only on the region, without limiting to paragraph marking because mark-paragraph relies ...
Kaushal Modi's user avatar
  • 25.7k
4 votes
Accepted

Is there a way to recognize `-- vim: et:sw=4:ts=8:sts=4:tw=80` in emacs?

I wouldn't be surprised if there's already a package out there that does it, but I couldn't find one. You should be able to cover most of it with something along the lines of the untested code below: ...
Stefan's user avatar
  • 26.5k
4 votes

Disabling quote conversion in docstrings

TL;DR (setq text-quoting-style 'grave) and see the answer by phils. Is it possible to have them displayed in their original form? Yes, see (elisp) Text Quoting Style, specifically the ...
Basil's user avatar
  • 12.5k
3 votes

How to export org files to plain text files as it looks like in clean view?

This gets pretty close I think although I haven't tested extensively: # -*- org-ascii-headline-spacing:(0 . 0); org-ascii-bullets:((ascii 42) (latin1 167) (utf-8 8226)) -*- #+OPTIONS: H:0 num:nil toc:...
Ollie Saunders's user avatar
3 votes
Accepted

How can I use par as my paragraph formatter

Essentially you want your function to do the equivalent of marking the paragraph and typing C-uM-| par RET Something like: (defun my-fill-paragraph-using-par (&optional _justify) "Invoke shell ...
phils's user avatar
  • 51.6k
3 votes

Automatically reformat org buffers

This is trickier than it seemed! Here is some code that seems to do what you want, I think. It only adds lines at the beginning of a heading, and at the end of what I call the heading content (not ...
John Kitchin's user avatar
  • 11.9k
3 votes

Org mode - Parsing rich HTML directly when pasting?

I wrote an add-on Copy as Org-Mode for Firefox which can do this in browser directly (copying the rich HTML directly, instead of copying the raw HTML code), it even can handle HTML tables into Org-...
kuanyui's user avatar
  • 1,040
3 votes

Can I format cells in an org-mode table differently depending on a formula?

I created the following mechanism for highlighting positive and negative numbers in org tables similar to how a spreadsheet program might do it: (defface positive-face '((t :foreground "green&...
Erki der Loony's user avatar
3 votes

How can I hide formatting marks after the formatting they define has been applied in markdown mode?

Markup hiding is now part of the current development version of Markdown mode. Markup for bold, italics, links, code, etc. can be hidden and toggled interactively using C-c C-x C-m (M-x markdown-...
Jason Blevins's user avatar
3 votes

format java source code natively based on CodeFormatterProfile

You can do that using https://github.com/emacs-lsp/lsp-java by setting lsp-java-format-settings-url to https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml (setq lsp-java-...
Ivan Dimitrov Yonchovski's user avatar
3 votes

Non-breaking space in Org-mode

I ran across this problem myself while writing up some reports. Beyond the answer provided by @Andrew Swann (and mentioned by @manandearth above), according to the manual (https://orgmode.org/manual/...
Xaldew's user avatar
  • 1,231
3 votes

How to make strings literal when formatting?

C-hf format and (for your example) C-hv mode-line-format both use the same escaping mechanism for the % metacharacters: The format control string may contain %-sequences meaning to substitute the ...
phils's user avatar
  • 51.6k
3 votes
Accepted

How to type a^b in org-mode?

As @rpluim mentions in a comment, setting org-export-with-sub-superscripts to nil would disable the special handling of ^ and _ for subscripts and superscripts. There is one more variable that is ...
NickD's user avatar
  • 30.6k

Only top scored, non community-wiki answers of a minimum length are eligible