A much better way to use the narrowing commands is just to make emacs guess what you want whenever you press "C-x n", and that's what the following snippet does. I recommend that you put it in your .emacs:
#+BEGIN_SRC emacs-lisp
;; Also set up narrow dwin
(defun narrow-or-widen-dwim (p)
"Widen if buffer is narrowed, narrow-dwim otherwise.
Dwim means: region, org-src-block, org-subtree, or defun,
whichever applies first. Narrowing to org-src-block actually
calls `org-edit-src-code'.
With prefix P, don't widen, just narrow even if buffer is
already narrowed."
(interactive "P")
(declare (interactive-only))
(cond ((and (buffer-narrowed-p) (not p)) (widen))
((region-active-p)
(narrow-to-region (region-beginning) (region-end)))
((derived-mode-p 'org-mode)
;; `org-edit-src-code' is not a real narrowing
;; command. Remove this first conditional if you
;; don't want it.
(cond ((ignore-errors (org-edit-src-code))
(delete-other-windows))
((ignore-errors (org-narrow-to-block) t))
(t (org-narrow-to-subtree))))
((derived-mode-p 'latex-mode)
(LaTeX-narrow-to-environment))
(t (narrow-to-defun))))
;; This line actually replaces Emacs' entire narrowing
;; keymap, that's how much I like this command. Only copy it
;; if that's what you want.
(define-key ctl-x-map "n" #'narrow-or-widen-dwim)
#+END_SRC
You can easily create a rectangle with evil mode with C-v. Once you have a rectangle you can do these commands:
`C-u number C-x r + r`
If r contains a number, increment the number in that register by number. Note that command C-x r + (increment-register) behaves differently if r contains text. See Text Registers.
`C-x r i r`
Insert the number from register r into the buffer.
`C-x r i` is the same command used to insert any other sort of register contents into the buffer. `C-x r +` with no numeric argument increments the register value by 1; `C-x r n` with no numeric argument stores zero in the register.
Org-mode lets you easily insert headings and sub headings with "C-RET". If you press it many times, you'll have something like this:
A line with just one "*" is a top level heading. If it has a two "**" below it, then it now has a sub-heading. Just like the following:
You can use the tab key to show/hide any sub level headings.
Org-mode is also great for todo lists. Hitting C-c C-t
lets you mark an item as TODO or DONE.
One can easily create simple todo lists with org-mode. In any org file C-RET
inserts a "*" into the buffer. Pressing "C-c C-t" will add the words "TODO". It'll look like:
f a column in an org table contains , where N is a number, then that table column will be that many characters wide
|
<10> |
<5> |
<15> |
<20> |
I |
am |
a |
table |
|
|
|
|
rg babel is a the best approach towards literate programming ever attempted, and it works! Almost all programming languages treat code as the first order citizen and hides comments behind a simple syntax. For example here is some javascript |
// initialize the variable
var i = 5;
if (i < 6) {
i++;
}
console.log (i);
The comment "initialize the variable" comes after the comment syntax "//". In literate programming the code portion of the file is "commented" and the comments do not hide behind a comment syntax. Let me give you an example of the literate kind:
Let's write a trivial js function the literate way
,#+BEGIN_SRC js :exports code
var i = 5;
if (i < 6) {
i++;
}
console.log (i);
,#+END_SRC
5 + 5
10
How cool is that?
http://orgmode.org/manual/Specific-header-arguments.html
info:org#Specific header arguments
- :results
syntax:
:results [raw | silent | value | output ]
value is function mode. It means that org-mode will use the last executed command as the value of the output.
ie:
import time
print("Hello, today's date is %s" % time.ctime())
print('Two plus two is')
return 2 + 2
#+RESULTS:
4
echo "hello world"
echo "big cat"
ls -lh | grep emacs.org
#+END_SRC
#+RESULTS:
: hello world
: big cat
: -rw-r----- 1 joshua 1000 28K Mar 9 14:51 emacs.org
- :exports [code | results | node | both]
- :dir
Specify a default directory that the code is to be run in
:dir <dir>
*** Extending Org-mode
read about org's parser here http://orgmode.org/worg/dev/org-element-api.html
* Helpful Emacs modes
** bug-hunter
This will help you find bugs in your init file. Ensure that you have installed bug-hunter toward the top of your init file. That way, when something breaks later in your init file, you can run ~M-x bug-hunter-init-file~ to find the problem.
So your init file would look like this:
#+BEGIN_SRC emacs-lisp :exports code
(require 'package)
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))
;;; Also use Melpa for most packages
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
(use-package bug-hunter :ensure t :defer t)
;;the rest of your init file
#+END_SRC
https://github.com/Malabarba/elisp-bug-hunter
** helm mode
Helm mode is a completion framework. The default Emacs completion framework is ido-mode. You can see ido-mode in action with ~M-x ID-find-file~.
C-c C-f helm-find-files and or C-x C-f
In this mode typing "~/ manage js$"
will display a list of files in my home directory that contain the work 'manage' and end is js
Alternatively, after you have typed C-x C-f, you can then type "C-u C-s" to search recursively via rgrep.
Or M-g a (ag) or M-g g for git-grep.
Typing C-l will display the files is the parent directory
Typing C-z when point is on a directory, will show the files in that directory
Helm has nth commands. Instead of typing tab to get to the action menu
just press C-e for the 2nd action
and C-j for the 3rd action.
You can also bind a key to an action menu
(define-key helm-map (kbd "<C-tab>") 'helm-select-4th-action)
C-c C-u lets you view an image
Apparently helm lets you open files externally in helm-find-files with C-c C-x (not working though)
I can make personal actions on to run on files via eshell WOW!!!!
C-SPC will mark files that you can perform actions on
You can then hit copy files in the action menu, which will let you select a new destionation to copy the files. so cool!
You can do the same thing from the action menu with rename, symlink, hardlink
helm has support for etags
C-h e will search the tags file
the above should create the TAGS file with tags, but it's not working
find . -iregex .*\.el$ | xargs etags
check out pcomplete extension
https://github.com/thierryvolpiatto/pcomplete-extension
*** commands
I can also write my own helm commands cool!
[[http://wikemacs.org/wiki/How_to_write_helm_extensions][write your own helm extentions
]]
=C-c h m= open helm-man-woman
=C-c h h g= open helm info gnus
=C-c h h r= open the helm-emacs-info
=C-c h b= is helm-resume which opens up the last instance that you were on
=M-<space>= mark candidate
=C-h m= inside a helm window will show you all of helm's keybindings
*** helm-descbinds
** evil mode
" <letter> yy stores the whole line into register <letter>
" <letter> p puts the whole line back
=vip= select the paragraph that point is in.
check out filters. It lets you take a line, and throw that line as a command.
It lets you create macros that you can take a line as a command to line in bash.
interesting
*** filters
Evil-mode lets you put the contents of the buffer through a shell command!
You can take a line like
hello how are you :.!wc -w --> 4
hello how are you :.!wc -c --> 21
5 / 6 :.!bc -l --> .83333333333333333333
5 :105,109!sort --> 1
4 2
3 3
2 4
1 5
Sort has lots of options. I can do sort -r by reverse order, sort -k4 the 4th column, sort -n sort numerically
** El-doc
El-doc shows you what a function in the mini-bar as you write it. By default it works for emacs lisp extremely well. You'll notice that my emacs-lisp-mode-hook sharp quotes eldoc-mode, which means it's using the syntax =#'eldoc-mode=. Sharping quoting is only necessary if you are quoting named emacs lisp functions.
#+BEGIN_SRC emacs-lisp
(add-hook 'emacs-lisp-mode-hook #'eldoc-mode)
User commands
ttd-mode let's you specify a compile command. Everytime you save a file, it will run your compile command.
yasnippet
Important Characters
Undo tree
Paredit mode
Most emacs modes use a bunch of regular expressions to highlight source code. BUT semantic tries to make this better by
parsing the code and creating grammar with it.
C-c , j
prompt for a tag in the currect file and move point to it.
C-c , J
prompt for a tag in any file that emacs has parsed and move point to it.
If it finds an error, it will alert you at the bottom!
http://ergoemacs.org/emacs/yasnippet_templates_howto.html
$&
indents the line according to the major mode
`(some-lisp-code)`
embods lisp code
$0
where point will be when the snippet ends
$n
where n is a number ie: $1
, $2
, etc. If you have multiple $3, then typing some text in one $3 will also be put in
the other $3.
- =${n:}
learn more about undo tree
http://danmidwood.com/content/2014/11/21/animated-paredit.html
Paredit mode is a superior way to interact with lisps.
slurping
slurping elongates the current sexp by pulling in the closest sexp (either forward or backward)
#+BEGIN_SRC emacs-lisp
;; here I've called "C-c 0" paredit-forward-slurp-sexp
;; point was always on the "h" in hello
((hello) this is a nice little sentence)
((hello this) is a nice little sentence)
((hello this is) a nice little sentence)
((hello this is a) nice little sentence)
((hello this is a nice) little sentence)
((hello this is a nice little) sentence)
((hello this is a nice little sentence))
;; here I've called C-c 0 paredit-backward-slurp-sexp
;; which pulls in another sexp or atom into my current sexp
;; point was always on "e" in sentence
(hello this is a nice little (sentence))
(hello this is a nice (little sentence))
(hello this is a (nice little sentence))
(hello this is (a nice little sentence))
(hello this (is a nice little sentence))
(hello (this is a nice little sentence))
((hello this is a nice little sentence))
#+END_SRC
barfing
barfing shortens the currect sexp by pushing out the closest sexp (either forward or backward)
#+BEGIN_SRC emacs-lisp
;; here I've called "C-c ]" paredit-forward-barf-sexp
;; point was always at the "h" in hello
((hello this is a nice little sentence))
((hello this is a nice little) sentence)
((hello this is a nice) little sentence)
((hello this is a) nice little sentence)
((hello) this is a nice little sentence)
;; here I've called paredit-backward-barf-sexp
;; point was always at the "e" in sentence
((hello this is a nice little sentence))
(hello (this is a nice little sentence))
(hello this (is a nice little sentence))
(hello this is (a nice little sentence))
(hello this is a (nice little sentence))
(hello this is a nice (little sentence))
(hello this is a nice little (sentence))
(hello this is a nice little sentence())
#+END_SRC
Paredit
#+BEGIN_SRC emacs-lisp
#+END_SRC
paredit-splice-sexp
This removes the parentheses around the current sexpression
(cool (cool (cool (cool (cool)))))
(cool (cool (cool (cool (cool)))))
(cool (cool (cool (cool (cool)))))
(cool (cool (cool (cool (cool)))))
(cool (cool (cool (cool (cool)))))
Restclient mode
sunrise-commander is a dired derived mode that acts like filezilla inside emacs.
ediff
Commands
install restclient. It lets you query the server via get and post. I can view the results via emacs!
=C= copy the current file into the other directory
=C-u C= copy the current file/files into the other directory in a background process
Ediff is emacs's cool way of camparing two files. It's very cool.
a
copies buffer a diff to buffer b
b
copies buffer b diff to buffer a
A
toggles readonly mode of buffer a
B
toggles readonly mode of buffer b
wa
save buffer a
wb
save buffer b
- =!= update the differance regions. If you press
a
and b
multiple times, you should probably do a =!=
*
highlights the words in the diff region that differ
ra
restore the diff region in buffer a
rb
restore the diff region in buffer b
z
suspend the ediff session
s
make the merge buffer as small as possible
When you specify files, you can edit the files as root using tramp's syntax like this.
tramp is an emacs extension that lets you edit remote files
/su::/path/to/file
it's syntax is done by pressing C-x C-f (find-file) then typing one of the following:
/METHOD:USER@HOST:FILENAME
/METHOD:USER@HOST#PORT:FILENAME
method can be ftp, ssh, rlogin, or telnet
If ssh-agent is running, Emacs uses scp. This probably is a clue about how to use ssh without a password.
I need to use scp
Etags is Emacs solution to creating elisp based tagging in Emacs. It is the prefect tagging system for your init files.
The shell command Etags needs to be run to generate a TAGS file. This looks like etags files
or COMMAND | etags -
The simple way to do it is, which finds certain types of files and throws them in the TAGS file.
find . -wholename "*lisp/[a-zA-Z-]*.el" -print | etags -
The command (xref-find-definitions)
does the rest. I bind it like so:
(global-set-key (kbd "C-c .") #'xref-find-definitions)
auto-insert mode
Set up auto insertion
Auto-insert mode is a minor mode that will automatically populate a newly created file with default text. It is bundled with Emacs.
Since autoinsert mode is bundled with Emacs, enabling it is trivial. Simply, require the autoinsert library, and when you open up an empty file, perform auto-insertion, if applicable.
(require 'autoinsert)
(add-hook 'find-file-hook 'auto-insert)
auto-insert variables
- auto-insert-directory
A string variable that specifies where to look for default file templates to insert into newly created files.
- auto-insert-mode
Enable auto-insert mode.
Enable autoinsert mode, and do not ask me if I want to perform automatic insertion. Just do it.
- auto-insert-query
If the value is non-nil, then ask the user if they wish to perform auto-insertion.
- auto-insert-alist
A list specifying text to insert by default into a new file. Elements look like (CONDITION . ACTION) or ((CONDITION . DESCRIPTION) . ACTION). CONDITION may be a regexp that must match the new file's name, or it may be a symbol that must match the major mode for this element to apply. Only the first matching element is effective. Optional DESCRIPTION is a string for filling `auto-insert-prompt'. ACTION may be a skeleton to insert (see `skeleton-insert'), an absolute file-name or one relative to `auto-insert-directory' or a function to call. ACTION may also be a vector containing several successive single actions as described above, e.g. [\"header.insert\" date-and-author-update].
(setq auto-insert-alist '((web-mode . my/yas-web-mode-snippet)
(org-mode . my/yas-org-snippet)))
An minimal working setup of autoinsert-mode might look like this:
(require 'autoinsert)
(add-hook 'find-file-hook 'auto-insert)
(setq auto-insert-directory "~/.emacs.d/auto-insert-directory/")
(setq auto-insert-mode t
auto-insert-query nil)
(setq auto-insert-alist '((web-mode . my/web-mode-template-function)
(org-mode . "my-default-org-mode-file.org")
(text-mode . ["my-default-org-mode-file.org" my/finishing-function])))
Use Snippets instead of templates
Insertion of template files into buffers is cool, but auto-insert mode is more powerful than than. One can expand a yasnippet into a newly created file. For example, if you write your Emacs init file in several org-mode files in "~/.emacs.d/lisp/", then you could insert a snippet specific to your Emacs configuration. The following does this.
(defun my/yas-elisp-init-files-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<elisp-init-files")))
(add-to-list 'auto-insert-alist '((".*/lisp/.*\\.org$") . [my/yas-elisp-init-files-snippet]))
Set up yasnippet
Inserting a predefined snippet via Emacs lisp is defined here. In case you don't like reading, then just do the following:
(yas-expand-snippet (yas-lookup-snippet "SnippetName"))
I'm defining some functions that will automatically expand when I open a file. Cool eh?
(defun my/yas-org-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<org")))
(defun my/yas-elisp-init-files-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<elisp-init-files")))
(defun my/yas-web-mode-snippet ()
(interactive)
(yas-expand-snippet (yas-lookup-snippet "<web-mode")))
calc
Emacs calc mode is amazing! It can do all sorts of things. Just reading the manual will make your head swim!
To open it type M-x calc Or C-x * c. The latter command is cooler, because it lets you toggle calc's visibility.
How to solve systems of equations
First enter into Algebraic mode via "m a"
Enter a vector of equations
So we can put this into Emacs calc via:
' [43 = 8b + 0o, 19 = 0.5b + 5o] RET
"a S b, o RET" will solve the equation.
The answer will look like:
[b
5.375, o = 3.2625]=
Regexp
Regular expressions are nifty ways of searching/replacing regions of text.
Consider this example
#+BEGIN_SRC php
if (isadmin() || ismanager ()) {
//some code here
}
#+END_SRC
Suppose that you want to add a space between both "is" in the function names. The following will do just that.
M-x dired-do-query-replace-regexp is\(admin\|manager\) RET is \1 RET
But let's get a basic understanding of regexps.
some nice emacs add on modes/APIs that make elisp programming nice
Elisp macro add-function
debugging weird errors
wrong type argument errors
- ctable https://github.com/kiwanami/emacs-ctable
- s https://github.com/magnars/s.el
- f https://github.com/rejeep/f.el
This is the old way of doing it. The newer was is to say add-function
If you get a weird error like "wrong type argument : arrayp , nil", or "wrong type argument : stringp", it might be an issue with package.el. The following might fix the error:
- Change the address for the melpa archive to: "http://melpa.org/packages"
- Delete the ~/.emacs.d/elpa/archives/melpa/archive-contents file
#+BEGIN_SRC sh
rm ~/.emacs.d/elpa/archives/melpa/archive-contents
#+END_SRC
- Run package-initialize and package-refresh-contents.
#+BEGIN_SRC emacs-lisp
(package-initialize)
(package-refresh-contents)
#+END_SRC
debugging your init file
Variable binding depth exceeds max-specpdl-size
#+RESULTS:
https://github.com/Malabarba/elisp-bug-hunter. This is SOOO helpful!
M-x bug-hunter-init-file RET e
Check out this stackoverflow question:
http://stackoverflow.com/questions/1322591/tracking-down-max-specpdl-size-errors-in-emacs
#+BEGIN_SRC emacs-lisp
;; To track the problem down, you can try this:
(setq max-specpdl-size 5) ; default is 1000, reduce the backtrace level
(setq debug-on-error t) ; now you should get a backtrace
;; C-h a ; in speedbar
#+END_SRC
#+RESULTS:
t
to go back to the state that you wanted again, you can do this:
#+BEGIN_SRC emacs-lisp
(setq max-specpdl-size 2580) ; default is 1000, reduce the backtrace level
(setq debug-on-error nil) ; now you should get a backtrace
#+END_SRC
java development
#+RESULTS:
You will probably want to use eclim https://github.com/ervandew/eclim
building Emacs
It turns eclipse into a server. Emacs sends info to that server, the server responds back, and Emacs shows you code errors, code completion, etc. It's probably the best way to do java development in Emacs.
--with-cairo builds Emacs with the ability to render Emacs with Cairo. Cairo is a 2D drawing library. It is failing its own test suite. So it might not be a great thing for Emacs. And currently it doesn't work so well.
connecting to a database
sudo ../emacs-src/configure --with-modules --enable-link-time-optimization --with-x-toolkit=gtk3 --with-xwidgets
SELECT user_login, display_name FROM wpHonorsCollege_users WHERE ID = 1
user_login |
display_name |
joshua |
joshua |
console.log (6 + 2)
8
print (3 + 8)
11
(+ 2 4 )
6
ls
debugging emacs crashes
https://www.gnu.org/software/emacs/manual/html_node/emacs/Crashing.html
Essentially you do this:
cd /path/to/emacs/src
gdb ./emacs
...
(gdb) run
Now you use Emacs as usual when it crashes... you run
(gdb) thread apply all bt
tweaking the Gnus manual, because it is super confusing!
Make the info:gnus#Maildir section readable, because it is not!
get company ebdb to work.
https://emacs.stackexchange.com/questions/34274/gnus-gmail-and-mbsync-do-i-need-dovecot
I basically need to bind tab to company-ebdb in
:LOGBOOK:
- State "TODO" from [2020-02-25 Tue 10:26]
:END:
This is really weird!
It seems that helm-mode is causing a really slow performance on opening and saving remote files.
https://emacs.stackexchange.com/questions/5359/how-can-i-troubleshoot-a-very-slow-emacs
What can cause this problem? Is it caused by Emacs or it is due to my
pc's performance? Generally what are the variables that affect Emacs'
performance?
Emacs has around 50,000 internal variables and a few thousand external
packages averaging at a few dozen variables each, you can't expect
someone to answer this in a general sense. :-)
You can see that just by looking at the comments thread under your
question. There are half a dozen different suggestions in there, all
equally valid. What can you do to pinpoint the problem? Option 1:
Disable modes
Start disabling those minor-modes you've listed, and see which one
solves you performance issue. I would start with smartparens,
auto-complete, line-number and font-lock, and then follow down the
list.
"I didn't have this problem yesterday" means very little, don't rely
on it too heavily. Just start disabling minor-modes until something
solves it.
If none of the minor-modes fix your issue, then start commenting out
portions of your init file until you find out which snippet was
causing this. In any case, ask a new question when you have something
more specific. Option 2: The profiler
Invoke M-x profiler-start RET RET (the second RET is to confirm
cpu); Do some typing, preferably an entire paragraph or more;
Invoke M-x profiler-report.
That will give you a buffer describing the cpu time taken by each
function. Hitting TAB on a line expands it to display the functions
inside it. Navigate this buffer until you find out which function is
taking so much CPU time. What do I do afterwards?
Once you find the function or package or snippet causing lag you can
(in no particular order):
cool Emacs projects I should try
Set up Emacs to send html emails via org-mime and set up polymode
Ask a new question here regarding that specific minor-mode (or
function or snippet). Report a bug to the package maintainer. Check
the comments at the top of the package's source file. If it contains a
URL (specially on github), there's probably an issue tracker there.
Some packages offer a command like M-x PACKAGE-bug-report. His or her
email should be at the top of the package's source file. If it's a
built-in package, you can report it with M-x report-emacs-bug. Even
for packages that are not built-in, you can ask for help at the
help-gnu-emacs mailing list.
Set up part of the message-mode in org-mode.
polymode
org-mode message-mode
#+BEGIN_SRC scheme
(define (hello)
(let ([a "b"])
b))
#+END_SRC
Let C-c C-c send the email, when I am C-c C-c doesn't do anything for org-mode
#+RESULTS:
CLOSED: [2020-08-12 Wed 00:07]
:LOGBOOK:
- State "DONE" from "TODO" [2020-08-12 Wed 00:07]
- State "TODO" from [2020-08-11 Tue 23:41]
:END:
For instance
To: jbranso@dismail.de
--message follows this line--
when point is HERE and I press C-c C-c, then send the email
#BEGIN_SRC scheme
(define a "b")
;; when point is here, and I press C-c C-c, evaluate the code
#+END_SRC
--
lazily load poly-message-mode
set up poly-message-mode on message mode by default
Sent from Emacs and Gnus
This is slightly harder to do, because poly-mode tries to set up poly-mode via auto-mode-alist
(add-to-list 'auto-mode-alist '("\\.md" . poly-markdown-mode))
customize my html output to make it look SUPER COOL!
create an org html template for use in emails
migrating to emacs 27.1
or // -*- mode: poly-noweb -*-
Org-mode renders the HTML
I can now use .config/emacs/init.el
get flycheck-pos-tip working again
emacs-seq is breaking things. Specifically M-x bug-hunter does not work
#+BEGIN_SRC sh :results output :exports both
mv .emacs.d/ .config/emacs
#+END_SRC
;; mroh
;;11:45:29
;; joshuaBPMan: try to remove the emacs-seq pkg.
;;mroh
;;11:51:32
;;joshuaBPMan: it's included in emacs, so require works. Do you have any of those `guix refresh -l emacs-seq` installed?
;; cd .guix-profile; grep -r 'seq'
;; shows emacs-seq in my manifest
;; cat .guix-profile/manifest shows that emacs-seq is a dependency of bug-hunter...removing bug-hunter
Also it would be nice if org-babel-load-file would "log" in Messages, which heading it is currently loading, so that debugging
things I can do
file a bug report for Emacs org-mode...Somewhere in my org-mode gtd.org file, I am causing a syntax error.
;;Building the following 5 packages would ensure 9 dependent packages are rebuilt:
;;emacs-erc-status-sidebar@0.1-1.ea4189a
;;jmacs@27.1
;;emacs-flycheck-haskell@0.8-2.32ddff8
;;emacs-psc-ide@0.1.0-1.7fc2b84
;;emacs-org-roam@1.2.1
org-babel emacs files would be easier.
Where is it?