Skip navigation

Category Archives: programming

general stuff about languages and software tools

This is a way to select whatever text we had just inserted inside a buffer. I forgotten this trick until now that used it again. A small tip worth annotating that might come in handy for somebody else as well:
1) yank(paste) the text “C-y”
2) mark the point where the cursor landed, (using point-to-register), do “C-x r SPC” and pick any letter ( a-z)
3) go back to the point where you initially were, by doing “C-u SPC”
4) set the mark there,“C-SPC”
5) use “C-x r j” (+ the letter picked) to go up the point where the yanked text ends inside your buffer.
6) Voila, the text yanked is highlighted ready for whatever manipulation you need to do onto it.

ps: Of course in case you pasted text at the end of the buffer is simpler,
just a matter of:
“C-u SPC”
“C-SPC” to mark
“M – >” to go to the end

UPDATE: Actually, thanks to Peter (see comment below) I learned that this is possible just doing “C-x C-x after the yank if you have transient mark mode enabled” .

Advertisements

A probably ridiculous thing to do, I know, but in hope of getting valuable help for you elisp expert guys out there (I’m all ears) I’m going ahead to show the unusual path I followed to simply bind the org-cicle invoked with an argument 64 (C-u C-u C-u)

(This is also a litle probe of how magnanimously flexible the emacs lisp environment can actually be to allow us to record processes we want automated for later reuse)
Anyway:
1) I started to record the macro “C-(
2) made the call to org-cycle to reveal-all by previously pressing C-u three times “C-u C-u C-u M-x org-cycle
3) stopped recording macro “C-)
4) did “M-x kmacro-name-last-macro” (which provides the lambda form of the last created macro)
5) ran “M-x insert-kbd-macro
6) assigned a key binding to it (global-set-key(kbd “C-+”) ‘my-org-reveal-all)


(fset 'my-org-reveal-all
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ([21 21 21 134217816 111 114 103 45 99 121 99 108 101 return] 0 "%d")) arg)))

(global-set-key(kbd “C-+”) ‘my-org-reveal-all)

Due to a disk failure on a server I needed to preserve several databases created for students. Some of them were empty, consecuently doing:
mysqldump with the option --all-databases yielded an error and didn’t work.

What worked instead, was iterating through and mysqldumping them individually with this shell oneliner:

for I in $(mysql -uroot -p****** -e 'show databases'); do  mysqldump -uroot -p****** $I > $I.sql; done;

I compressed those created files into one tar inmediately after:

ls -1 | xargs tar -cvzf backupOfDatabases.tgz

I just wanted to free a shortcut I had for “C-1” to let it behave the way it was meant with the command “digit-arguments”. This answer http://stackoverflow.com/questions/7549259/is-there-a-quick-way-to-unbind-keys-in-emacs came in handy.

(global-unset-key (kbd “C-1”))
(define-key global-map (kbd “C-1”) ‘digit-argument)

By the way (a little tip) you can go: C-<digit ARG) instead of the C-u method; say to move 11 characters back, “C-11 C-b” is quicker than “C-u 11 C-b”, one less hit!

I needed to inspect a relatively small portion of a large log file (~1Gb), which will make chock even powerfull text-editors like vi(m) or emacs

I proceded in two steps:
1) found the match in the file and pulled the line number

—————————————————————————————-
awk ‘/ May 10 /{a=$0; b = NR;}END{print a,” :: “,b}’ log.txt
—————————————————————————————-
which yielded:
Thu May 10 02:17:05 ART 2012 :: 29199076

2) then I dumped the content from the that line and filtered it with head
—————————————————————-
tail -n +29199076 log.txt | head -n 100
—————————————————————-
That is possible with the trick of using “tail -n +(N)” which brings lines from the N line onwards

As and alternative to the last one, as explained here, sed could’ve been used in the following manner:
—————————————————————-
sed -n -e 29199076,29199176 -e 29199077q log.txt
—————————————————————-
(the last parameter, for efficiency, tells to quit at the limit line + 1 )

I finally got it on how to alter the combination of ALT + (backtick|tilde) keys (the backtick is the one above the tab key), which Ubuntu Oniric Ocelot (11.10) assigned to a new functionality for flipping through windows in the switcher. This system shortcut could surely come as a nasty surprise when you upgrade from version 10.xx, turning unusable whatever command you might had bond in your applications. My emacs setup specially does make a lot of use of these two keys, so I wanted to thank the guy and record his solution found as a response buried in this support thread
Simply all it takes for disabling the default hotkeys is to:
1) get the CompizConfig Setting Manager
"sudo apt-get install config-editor" (it does not come included in Ubuntu Ocelot 11.10)
2) bring it up, (typing “CCSM” in your terminal)
3) look under Desktop -> Ubuntu Unity Plugin -> Switcher
resetting ALT+`
4) pick the last two shown there, and create a key combination for them (which very non-intuitively appear listed as “disabled”). The functions in question are:
:: key to flip through windows in the switcher
:: key to flip through windows in the switcher backwards
(note how in this case I had already set them to C-Alt 7 and C-Alt 8)

Yeah, it’s over babe, we’re back in business!

Darn!, this is nothing but a little note of caution, a sad admission of stupidity or maybe even a tip to someone: if you ever find yourself in a tight schedule doing extra hours to finish up a project, don’t waste precious minutes debugging your app only due to having foolishly picked the php ISO-8601 numeric representation of the day of the week “N” (mon=0 … sun=7). Completing the missing dates from a set of database records will work flawlessly just with date(“w”), specially if you had pulled the existing days using mysql date_format(“w”).
Duhh!!

Writing heredocs in the shell from emacs presented to me a little nuisance: since the RETurn key is bound to ‘comint-send-input -a function that sends the text to the shell process- I coudn’t re-edit the previous line after having pressed it to insert a simple line break. (I should add that this is actually the usual unix bash behaviour, I only happen to use bash from emacs and so wanted to overcome the problem there)

Yesterday it dawned on me to just use “C-q C-j” under that situation, (that’s how you stick in a line feed in emacs regardless of the mode you are in).

Given that the perspective of typing “C-q C-j” several times looked rather awkward I thought of binding RET with the control key for that:

(global-set-key (quote [(control return)]) (quote newline))

But then, the idea of going the other way around showed more appealing: why not to make newlines with a simple RET, and use the Control key to send the input? Besides, it makes sense to prevent hitting return by accident, I hate when my pinky lands there when searching the pipe “|” key.

While at it I decided to also rebind the TAB functionality so I could further improve the readability of my scripts by indenting them at the same time.

Implementing those changes is simple when you’ve got the luxury of a highly-configurable tool:

(defun my-shell-hook ()
  (local-set-key (quote [(return)]) (quote newline))
  (local-set-key (quote [(control return)]) (quote comint-send-input))
  (local-set-key (kbd "M-i")  'my-unindent)
  (local-set-key (kbd "C-i")  'indent-or-complete))

(add-hook 'shell-mode-hook 'my-shell-hook)

UPDATE: After a day trying this set-up I think that I’d rather stick to the normal binding of the RET key. I’m leaning toward using C-j and C-m instead, as I caught myself reaching to either of them to insert carriage returns. Here are the modified bindings in replacement:

(defun my-shell-hook ()
  (local-set-key (kbd "M-i")  'my-unindent)
  (local-set-key (kbd "C-i")  'indent-or-complete)
; (local-set-key (quote [(return)]) (quote newline))
; (local-set-key (quote [(control return)]) (quote comint-send-input))
;; I actually liked these two better for adding  newlines, leaving the return with it's default behavior
  (local-set-key (quote [(control j)]) (quote newline))
  (local-set-key (quote [(control m)]) (quote newline))
  (local-set-key (quote [(return)]) (quote comint-send-input)))


(add-hook 'shell-mode-hook 'my-shell-hook)

I picked a neat trick at http://stackoverflow.com/ to cancel a command from the shell with “C-q C-c RET” if I need to interrupt a process in a remote shell opened via tramp (only with my windows laptop which I run with cygwin and emacs 23.1.91.1)

Until now, without it, I found it very frustrating for example trying to cancel a heredoc inside a shell: due to some obscure reason, typing “C-c C-c(comint-interrupt-job) rather than get me back at the shell prompt, would kill the connection with the message “Process shell interrupt”.

Again though this alternative is only necessary to me when connected to remote hosts using the windows version of emacs, it’s a sweet discovery nonetheless.

I had an encoding issue that was bugging me inside the remember-data-file. I don’t know exactly how some latin-1 characters copy-pasted there ended up being saved as raw-text and were shown like non-ASCII characters (so a multibyte characters like “é” will appear with it’s escaped octal code “303\251” )

I tried at first setting the file’s encoding system with the tag “-*-coding: utf-8 -*-“, though it seemed not sufficient. The raw characters remained there and I soon grew tired of having to type in: “utf-8” at the prompt to select the encoding every time I needed to save the file.

Today searching the manual found here one easy cure on the command “recode-region” which allows to convert the text that was decoded with the wrong coding system.

Really all it took after marking the whole buffer (C-x h) was doing: “M-x recode-region RET” “Text was was really in: utf-8” “But was interpreted as: raw-text”

That was it!, the drag is over, I’m back to storing notes quickly doing just C-c r and C-c C-x with the worthy remember mode.