by numlocked · 6y ago · 0 comments
(defun arrayify (start end quote)
"Turn strings on newlines into a QUOTEd, comma-separated one-liner."
(interactive "r\nMQuote: ")
(let ((insertion
(mapconcat
(lambda (x) (format "%s%s%s" quote x quote))
(split-string (buffer-substring start end)) ", ")))
(delete-region start end)
(insert insertion)))
What it does: Given a list of strings separated by newlines (commonly, something copy-pasted from a table) like: josh
sam
jed
C.J.
toby
You select the text, and run "arrayify". You can optionally provide a quote character and, in about half a second, get to: "josh", "jed", "sam", "C.J.", "toby"
This is incredibly useful for taking lists of IDs, or email addresses, or whatever, and transforming them for pasting into documents, or emails, or "in" clauses in SQL, etc.It is unbelievably useful.
s/^/"/
s/\n/“, /
(Change all beginning of line to double pling. Change all new lines to double pling, comma, space)I don’t even store this as a procedure, I just ctr-f and do it from whatever text editor I’m in (I’m not an Emacs user)
The nice thing about search and replacing your way out of problems is that it works on coworkers machines.
I’m not saying it’s better than a stored procedure or a macro but once you’re into it, it’s easy to regex your way out of all sorts of adhoc problems :)
Arrayify breaks down with the slightest change in requirements.
You go to the end of "josh", press F3, add a comma and a space and you delete the rest of the line, until "sam" is now the next symbol after your cursor. Then you go to the end of the line (C-e) and press F4. Your macro is now recorded. To call it, press F4 repeatedly until everything is on one line.
As a bonus, you can actually name your macro, assign a keybinding to it, and can also save it as Lisp code.
:'<,'>!awk '{printf("\"\%s\", ",$0)}'
or to do the whole file: :%!awk '{printf("\"\%s\", ",$0)}'
What I find nice about this approach is that I can use my knowledge of awk in other non-vim contexts. Similarly I can use my knowledge of other text processing commands to do things in vim, like reformat text into columns (:%!column -t) for example.Of course you could use vim commands to do the same thing:
0<ctrl-v>GI"<esc>$<ctrl-v>GA", <esc>VGJ
Which would have the benefit of making it more customizable at the expense of....well you can see what you have to type. noremap <Space> @q
These days I do this from evil-mode: (define-key evil-normal-state-map " " "@q")That’s a great point. On the other hand, truly, “What happens in emacs, stays in emacs.”
xclip -o | awk '{ print "\"" $0 "\"," }'
find it simpler than 10 lines of lisp but what do I know :pI note this mainly because it took me quite a long time to grok helm. Keep working with it!
In vim/unix you can run this command (keybinding not included but simple enough):
:'<,'>%!awk 'BEGIN {ORS=" "} {print "\" $1 "\"," }
As for a unified computing environment, it is unified with the OS and not a world unto itself, there is no need to have one way of doing things within the editor and another if you want to script something. Both work just as well with version control, plus it gives you the flexibility to change text editor, like from vim to kak.Speaking of kak, multiple cursors > any of these scripted options.
but that's something any IDE in the world can do ?
It's great for running sort, awk, sed, etc.
Keyboard macros: https://www.emacswiki.org/emacs/KeyboardMacros
Find/replace all:
^(.*)$\n
with "$1", C-M-% runs the command query-replace-regexp (found in global-map),
which is an interactive compiled Lisp function in ‘replace.el’.
It is bound to C-M-%, <menu-bar> <edit> <replace>
<query-replace-regexp>.
(query-replace-regexp REGEXP TO-STRING &optional DELIMITED START END
BACKWARD REGION-NONCONTIGUOUS-P)
Replace some things after point matching REGEXP with TO-STRING.
As each match is found, the user must type a character saying
what to do with it. For directions, type <help> at that time.I usually end up doing that in two steps, with the first being a recursive macro to quote each line:
q y s $ " A , <escape> 0 j @ q
Then visually selecting and replacing new lines with spaces: V G :s/CTRL+Q CTRL+J/ /gShowing off macros (in my case evil mode, but I suppose it's the same for emacs) is a nice party trick that has impressed some of my coworkers.
Sadly it does not really do so well at actual parties...