Skip navigation

Tag Archives: tips&tricks

Escaping a forwardslash in awk like

awk  '/javascript\//'

doesn’t work
The way to do so is using the hexadecimal value of “/” which you can figure with the “od” unix command:

echo "/" | od -x

which returns:
0000000 0a2f

then doing:

awk '/javascripta2f/'

works fine!

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” .

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 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”).

I have been enjoying the window placement hotkeys in Ubuntu ever since I accidentally discover them. The possibility to center/maximize or reposition windows to any side of the screen becomes essential when you tasted the power of mouseless interactions. I only wished to find something similar for my cygwinized laptop, and was happy to learn that Windows 7 alows some window management via the keyboard with the following shortcuts:
:: windows-key + up-arrow: maximizes the current window
:: windows-key + (left/right)-arrow: docks the current window in the left or right side of the screen
:: windows-key + down-arrow: minimizes, or restores the size of a maximized window

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 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

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.