Emacs tips and tricks

Forum rules
Share your brain ;)
User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Emacs tips and tricks

Unread post by xaos52 »

Navigating and editing in emacs using the keyboard.
Re-read it until you master it...
Make a cheat-sheet of the mentioned commands.
http://www.masteringemacs.org/articles/ ... -movement/
Connected. Take this REPL, brother, and may it serve you well.
User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Emacs tips and tricks

Unread post by DebianJoe »

...but all those wacky keybinds. 8)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC
User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: Emacs tips and tricks

Unread post by xaos52 »

No perfection without some amount of suffering...
Connected. Take this REPL, brother, and may it serve you well.
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

thanks, bookmarked!
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

One of my favorite tricks is the column selection. It is really helpful when I'm editing a file and I want to copy/cut a rectangular portion of the text.
Here's what I do:

M-x cua-mode
Place point at the beginning of the text I want to select
C-Enter
Select the text using C-f and C-n.
C-k to cut (or M-w to copy)
C-y to paste somewhere else in the file

cua-mode also enables the """standard""" copy/cut/paste using C-c/C-x/C-v, but I stick with Emacs keys. You can also enable just cua-selection-mode, if you don't care about C-c/C-x/C-v.
User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: Emacs tips and tricks

Unread post by xaos52 »

easily start a version of emacs with alternate configuration
http://www.emacsbites.com/episode/scratch-emacs
Connected. Take this REPL, brother, and may it serve you well.
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

^ cool one, especially for testing random stuff I pulled in from the repos.
User avatar
bones
Clooney
Posts: 2385
Joined: Fri Jun 28, 2013 11:47 pm
Location: Cascadia

Re: Emacs tips and tricks

Unread post by bones »

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

What!?
RMS doesn't know how to use org-mode? Damn, that hurts.
machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Emacs tips and tricks

Unread post by machinebacon »

^ I suppose he doesn't even know how to use a web browser, since he sends snapshots of websites via a crawler to his email box.
..gnutella..
machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Emacs tips and tricks

Unread post by machinebacon »

http://www.finseth.com/craft/

Title The Craft of Text Editing: Emacs for the Modern World
Author(s) Craig A. Finseth
Publisher: Lulu.com; 3rd edition (March 17, 2006)
Paperback 228 pages
eBook Online, HTML, PDF files
Language: English
ISBN-10: 1411682971
ISBN-13: 978-1411682979
..gnutella..
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

yeah, thanks!
User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: Emacs tips and tricks

Unread post by xaos52 »

I prefer directories grouped at the beginning, in an xterm as well as in emacs.
For the xterm, update your ~/.bashrc to include the alias

Code: Select all

     
alias ls='ls --color=auto --group-directories-first'
For emacs, custumize group 'dired': Alt-x customize-group, enter dired and edit the entry 'Dired Listing Switches' as in the scrot.
Then click 'Apply and Save' if you want it to stick.

Image
Connected. Take this REPL, brother, and may it serve you well.
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

^ nice one!
User avatar
bones
Clooney
Posts: 2385
Joined: Fri Jun 28, 2013 11:47 pm
Location: Cascadia

Re: Emacs tips and tricks

Unread post by bones »

So, you use an OS for over a decade, and it turns out you don't know shit about it. *facepalm* It's been there all along. Duh.

Image

EDIT: Jeezus, nano and vim are there, too.
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: Emacs tips and tricks

Unread post by xaos52 »

Package managers for emacs:

- the native `package.el'
- el-get: if you prefer the latest and greatest from git repo's
- cask: built on `package.el'
now at version 0.6. Allows you to build your own packages.

Pick the one you prefer, depending also on how adventurous you are.
Connected. Take this REPL, brother, and may it serve you well.
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

Firefox like Emacs? Yes, here it is.
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

Ok, finally my days of elisp are paying off. I also know DebianJoe would be proud of me because of this one.
I don't know if any of you have to deal with .po files. Dealing with translations (and translators) I have to deal with those files. I don't want to use poedit for that because of gtk3, so Emacs to the rescue.

Emacs has a mode for .po files: po-mode. If you have followed DebianJoe's tips and tricks about el-get in Emacs (and if not, shame on you), po-mode is just a matter of el-get-install.

By default, when you finish your .po file editing and validate it via po-mode hitting 'V', no .mo file is generated. Po-mode redirects msgfmt to /dev/null and that's not what I need. Basically I need the .mo file, because of some wordpress themes and plugins I usually work with.

So I opened ~/.emacs.d/el-get/po-mode/po-mode.el and changed this function:

Code: Select all

(defun po-validate ()
  "Use 'msgfmt' for validating the current PO file contents."
  (interactive)
  ; The 'compile' subsystem is autoloaded through a call to (compile ...).
  ; We need to initialize it outside of any binding. Without this statement,
  ; all defcustoms and defvars of compile.el would be undone when the let*
  ; terminates.
  (require 'compile)
  (let* ((dev-null
          (cond ((boundp 'null-device) null-device) ; since Emacs 20.3
                ((memq system-type '(windows-nt windows-95)) "NUL")
                (t "/dev/null")))
         (compilation-buffer-name-function
          (function (lambda (mode-name)
                      (concat "*" mode-name " validation*"))))
         (compile-command (concat po-msgfmt-program
                                  " --statistics -c -v -o " dev-null " "
                                  (shell-quote-argument buffer-file-name))))
    (po-msgfmt-version-check)
    (compile compile-command)))
To this:

Code: Select all

(defun po-validate ()
  "Use 'msgfmt' for validating the current PO file contents."
  (interactive)
  ; The 'compile' subsystem is autoloaded through a call to (compile ...).
  ; We need to initialize it outside of any binding. Without this statement,
  ; all defcustoms and defvars of compile.el would be undone when the let*
  ; terminates.
  (require 'compile)
  (let* ((compilation-buffer-name-function
          (function (lambda (mode-name)
                      (concat "*" mode-name " validation*"))))
         (compile-command (concat po-msgfmt-program
                                  " --statistics -c -v -o "
                                  (substring buffer-file-name 0 -2) "mo "
                                  (shell-quote-argument buffer-file-name))))
    (po-msgfmt-version-check)
    (compile compile-command)))
Then, in Emacs you just need to do a M^x byte-compile-file. That's it. Restart Emacs, edit your .po files and enjoy the .mo files created for you in your working directory.

(there surely is a better solution to the problem, my elisp is not that good, but it works for me and no need to use poedit anymore)
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5649
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP »

So, org-mode.
A neat trick with this one is org2blog. What is org2blog? It is just an easy way to write and publish your blog posts without leaving your favorite text editor. I only tested it with my website, which is working on Wordpress, so I cannot guarantee it will work with other platforms. Also, this is what I did and it may not be the only and best way to setup org2blog.

Let's go.
First of all, my Emacs 24.3.1 had Org 7.x instead of 8.x. We need 8.x version, otherwise errors like "Cannot open load file ox-html" will scare us away. Check what version of Org you have with M^x org-version. If you don't have 8.x, just get it with git like this:

Code: Select all

git clone git://orgmode.org/org-mode.git
cd org-mode
make
Now we can safely add this to our .emacs (double check your paths, of course):

Code: Select all

;; Org-mode
(add-to-list 'load-path (expand-file-name "~/org-mode/lisp"))
(add-to-list 'auto-mode-alist '("\\.\\(org\\|org_archive\\|txt\\)$" . org-mode))
(require 'org)
Do a M^x org-version just to check that everything worked as you were expecting.
We need bzr to install org2mode, so:

Code: Select all

sudo apt-get install bzr
Now we need to install org2blog and some other packages it needs to properly work. In Emacs, using el-get or package-install or whatever you use to grab packages, install the followings:

Code: Select all

M^x el-get-install RET xml-rpc
M^x el-get-install RET metaweblog
M^x el-get-install RET org2blog
M^x el-get-install RET htmlize
Then we can add this to our lovely .emacs (double check your paths, of course):

Code: Select all

;; org2blog setup
(add-to-list 'load-path "~/.emacs.d/el-get/metaweblog")
(add-to-list 'load-path "~/.emacs.d/el-get/xml-rpc-el")
(add-to-list 'load-path "~/.emacs.d/el-get/org2blog")
(add-to-list 'load-path "~/.emacs.d/el-get/htmlize")
(require 'org2blog-autoloads)

(setq org2blog/wp-blog-alist
           '(("my-blog"

              :url "http://our.website.com/xmlrpc.php"
              :username "our_username")))

(setq org2blog/wp-use-sourcecode-shortcode 't)
(setq org2blog/wp-sourcecode-default-params nil)
We're almost there.
In Emacs now you can do M^x org2blog/wp-login and you will be prompted for your password. Once logged in, do a M^x org2blog/wp-new-entry and start writing your cool poetry.
Org2blog will let you define, among other things, categories, tags, description, title and of course the text you want to be published.
Once you are done with the writing, it is time for the publishing. Just hit M^x org2blog/wp-post-buffer and you will find your post in draft on your website.
Post Reply