editing commands

# file transfer commands

The sftp command in the terminal, with -p meaning to retain the dates and permissions for any files transferred.

> sftp -p account@domain.tld:/some/path

Similarly, in emacs the dired command can be evoked with a remote path, f.e. after using Controlx d type at the prompt in the minibuffer:


Of course, that also means that can be done within dired, too. For example, Shiftc is for copying a file, and it asks for the path. Simply type a remote path and the marked files will be copied to the remote destination, even with ssh.

# searching files

# searching with find and grep

Find all html files then grep for "Text".

> find . -name "*.html" -exec grep -l "Text" {} +

For file name searches only (f.e. for all HTML files: -name '*.htm*'), the emacs command ESC x find-name-dired prompts for a directory for recursively searching and then a shell wildcard pattern (case-insensitive, without quotes), which returns a dired listing of results. Then use % g to mark all files whose contents contain a match for a regular expression.

# searching files within dired

Within dired the command ESC x dired-do-find-regexp (shortcut: Shifta) will search marked files or recursively search a marked directory for a regexp matching file contents.

Within dired the command ESC x dired-do-isearch-regexp (shortcut: ESC s a ESC Controls) will interactively search file contents of marked files.

# ImageMagick

The -⁠crop option will crop multiple times within an image when the geometry is only the size without a position ("#x#", not "#x#+#+#"). Saving to a TIFF results in multiple images within it.

Using -⁠repage is generally a good idea when cropping or rotating.

The -⁠virtual-pixel option is needed for affecting the color of pixels for rotations other than 90° increments, and its default method value is edge. The -⁠virtual-pixel method black is similar to setting the background color with -⁠background "#000" and then using -⁠virtual-pixel background.

Exporting all images from a TIFF can be done with the percent format "%d" in the output name, along with the -⁠scene option for the starting number.

Compression for JPG for line drawings has seemed pretty much unnoticeable at 50% (-⁠quality 50), and minor blurring noticeable at 25% when zoomed. However, 20% compression seems okay and also the lowest acceptable, which has resulted in approximately 1% of the TIFF size. At 15% compression there has been large swaths of color noticeable.

Errors along the lines of "cache resources exhausted" probably indicate the "/etc/ImageMagick-[version number]/policy.xml" has been changed, perhaps when upgraded. Use convert -list resource to check the resources. Typically, the policy has had nothing set at all, so simply comment out anything in that file to use defaults.

# image info

identify the current dimensions, the resolution depth and units, and the compression type and quality

> identify -format "%wx%h, %xx%y %U, %C %Q" image.tif

identify the original dimensions (before resizing), image depth, and image colorspace

> identify -format "%G, %[bit-depth], %[colorspace]" image.tif

identify the list of any embedded profiles (or a warning)

> identify -format "%[profiles]" image.tif

# IM with dired's ! shortcut in emacs

Mark some files within dired and then type ! to evoke a shell command on them. Within the shell command ? is one file at a time, * is all files at once.

rotate clockwise

mogrify +distort SRT 90 +repage ?

crop based on gravity and percentages of size

convert ? -set filename:f '%t' -gravity South -crop "100%x54%+0+0" +repage '%[filename:f]-2.jpg'

create thumbnail

convert ? -set filename:f '%t' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'

convert to a JPG file and then as an additional thumbnail, too, with relative filename

convert ? -set filename:f '%t' +write '%[filename:f].jpg' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'
convert ? -set filename:f '%t' -resize "2560x2560>" -density 72 +write '%[filename:f]_2560px.jpg' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'

convert to JPG at 20% quality, with same filename

convert ? -set filename:f '%t' -quality 20 '%[filename:f].jpg'

create PDF from a list of selected image files (but recompresses them)

convert * name-of-document.pdf

prepare image for a PDF

Size a 600 DPI image into a multiple of 50 for width and height (for scaling simply to 72 DPI in PDFs later), by first making the dimensions an even number. Also annotate the four corners with label indicating the side of paper, f.e. Front, Inside, or Back.

magick ? -set filename:f '%t' -background "#000" -splice "%[fx:w%2]x%[fx:h%2]" +repage -gravity Center -crop "%[fx:50*(trunc(w/50)-2)]x%[fx:50*(trunc(h/50)-2)]+0+0" +repage -bordercolor "#000" -border 150 -gravity NorthWest -font "Verdana" -pointsize 100 -fill "#FFF" -annotate +25+20 'Front' -annotate "90x90+%[fx:w-40]+25" 'Front' -annotate "180x180+%[fx:w-25]+%[fx:h-20]" 'Front' -annotate "270x270+40+%[fx:h-25]" 'Front' -quality 20 '%[filename:f].jpg'

And then the thumbnail:

magick ? -set filename:f '%t' -background "#000" -splice "%[fx:w%2]x%[fx:h%2]" +repage -gravity Center -crop "%[fx:50*(trunc(w/50)-2)]x%[fx:50*(trunc(h/50)-2)]+0+0" +repage -resize "200x200>" -density 72 -bordercolor "#000" -border 20 -gravity NorthWest -font "Verdana" -pointsize 16 -fill "#FFF" -annotate +20+2 'Front' -annotate "90x90+%[fx:w-4]+20" 'Front' -annotate "180x180+%[fx:w-20]+%[fx:h-2]" 'Front' -annotate "270x270+4+%[fx:h-20]" 'Front' -quality 20 '%[filename:f]_240px.jpg'

# multi-image TIFF

Use a shell when a TIFF has multiple images inside it to reference its first image.

convert to a JPG file and then as an additional thumbnail, too, with relative filename

> convert image.tif[0] -set filename:f '%t' +write '%[filename:f].jpg' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'

rotate clockwise, then output as a JPG file and also a thumbnail

> convert *.tiff[0] +distort SRT 90 +repage -set filename:f '%t' +write '%[filename:f].jpg' -resize "240x240>" -density 72 '%[filename:f]_240px.jpg'

# renumber file names

View a directory of files named with numbers within dired with Controlx d (command: ESC x dired), hide details with the shortcut ( (command: ESC x dired-hide-details-mode) to show only filenames, and then use Controlx Controlq (command: ESC x dired-toggle-read-only) to switch to write mode. A message appears saying how to save changes (Controlc Controlc), or cancel (Controlc ESC).

Search with a regular expression using ESC Controls for matching the numbers, for example this matches at least two digits:


Then replace matches using ESC % by adding a number to the matched number. For example, add 1 to matched numbers:

\,(format"%d"(+ \#& 1))

The \, is for using lisp for the replacement; see 15.10.2 Regexp Replacement. Within that, the \#& conveniently converts the whole match to a number. The same works for parenthesized matches like \#1 and so on. Also, multi-digit parenthesized references are possible when using lisp with a replacement, t.i. beyond 1–9. For example, \27 for the 27th parenthesized match, or the numerical value of the match with \#27.

After all desired editing is done, use Controlc Controlc to save the changes to the filenames, and read-only mode will be enabled again.

# inserting text results

Insert the results of a calculation using the common command "eval-expression" for evaluating LISP (shortcut ESC :).

ESC:(insert(format"%d"(*(/ 5400 50)6)))

Insert the number of characters or bytes between point and mark, such as of the current selection.


The emacs command ESC x shell-command (shortcut: ESC !) will insert the results of a shell command at point in the current buffer when used with the universal argument (Controlu). For example, to insert the width of an image into the buffer using identify from ImageMagick:

ControluESC!identify -format "%w" image.jpg

More in:
installing emacs on macOS