I'm completely new to Emacs and I'm looking for a way to make Emacs automatically write these lines of code for C files
#include <stdio.h>
int main()
{
return 0;
}
I'm sorry for messing up these lines, but I think you've got the point.
Yasnippet is perfect for re-usable snippets .
Have a look at the emacs wiki
Install using: M-x package-install yasnippet, or look on project on github.
Once installed, enable yasnippet-minor-mode with M-x yas-minor-mode-on (when your C file is open, the buffer's major mode is on c-mode and yas-minor-mode adds yas functionality.)
C-c&C-n will open a buffer for a snippet.
Give it a name and copy your code into the body, result should look like:
# key: c
# name: c_include
# --
#include <stdio.h>
int main()
{
return 0;
}
C-cC-c to close and save the buffer.
It will now appear with the name you gave it in the yasnippet menu when you call
C-c&C-s
Yasnippet has a lot more functionality. Read the doc.
Find C templates in AndreaCrotti yasnippet snippets Github repository
Related
In Emacs, I want to achieve an IDE-like behaviour with parenthesis block and cursor position. That is, when I type, for example, int main() { RET, it should expand to
int main() {
I <- cursor position
}
I've installed smartparens plugin to automatically insert pairs, but it doesn't deal with the cursor:
int main() {
I <- cursor position}
Consider using Yasnippets, a template system for Emacs which comes with a lot of templates preinstalled. In your case:
mainTAB
will expand to:
int main(int argc, char *argv[])
{
CURSOR
return 0;
}
What you request is already the case in Emacs, starting with release 24.4.
You can enable the behavior by turning on electric-indent-mode, if it is not already enabled by default (Emacs 24.4 or later). Put this in your init file to enable the mode:
(when (fboundp 'electric-indent-mode) (electric-indent-mode 1))
However, you need at least Emacs release 24.1 to use electric-indent-mode. It is not available in older releases.
As #Drew pointed out electric-indent-mode, I've looked up different electric-modes in Emacs and figured out that electric-pair-mode fits exactly my needs.
I need to edit source code in org-mode.
#+BEGIN_SRC cpp
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
#+END_SRC
In code blocks, there is no code completion.
Is it possible to use code completion in code blocks in org-mode?
You can do C-c ' to open a new window with the corresponding major mode.
Then you have code completion.
I really like the org-babel that enables me to organize my scripts in org-mode, however, i found there are some issues with the org src buffer when i edit the source code in a separate buffer (using keybinding C-c ').
first thing is, even i explicitly run write-file, and then specifies the file path and name to save, the buffer is not saved to that file, but the source code block in the .org file gets updated and the .org -file is saved.
second thing is, whenever i run save-buffer in org src buffer, the buffer screen will automatically scroll down till current mouse position is the last line in the buffer. this is annoying because sometimes i lose tracking my scripts.
i am not so familiar with elisp, and can only do simple work like define-key or add-hook, i hope i can get help from here. thanks
Org-babel is not meant for organizing scripts, but for including source code as part of your document.
You may be interested in tangling, which allows to join and extract source code blocks from your orgmode document into separate files.
The following example will merge 2 source code fragments into the file test.m when tangling (org-babel-tangle, bound to C-c C-v t):
* Tangling example
Set up a vector:
#+begin_src octave :tangle test.m
a = 1:10;
#+end_src
Then find out squares
#+begin_src octave :tangle test.m
b = a.^2
#+end_src
How can I view source code for a builtin Emacs package?
For instance given lisp code (require 'color), I would be interested in which functions are provided by that package. I googled emacs "color.el" and found the source code. But I wonder if this file can be viewed directly from within Emacs itself? By the way, find . -name 'color*' gives
./share/emacs/24.3/lisp/color.el.gz
./share/emacs/24.3/lisp/color.elc
You can get to the source code in a single command with M-x find-library.
You can either open the file directly in Emacs. I think .gz files are by default decompressed. Usually, I pick out one function from the package I'm interested in (e.g. org-mode), look up the documentation for that using C-hf org-mode RET and then click on the file name in the documentation buffer. It will take you the source file.
So ./share/emacs/24.3/lisp/color.el.gz is the gzipped source code, and you can open that in emacs by first doing M-x auto-compression-mode, and then visiting that file.
How can I make Emacs to complete words that are in C include files ?
#include <stdio.h>
int main(){
print//<-- this is where I want it to complete printf
What's the simplest way? (something simpler than Cedet)
First generate tags for the source and include files you'd like to be able to autocomplete for. See my blogpost for tips on using tags if you didn't use tag tables before.
Now if you have a TAGS table that includes the stdio.h, then you can autocomplete 'printf' using the command `complete-tag'.
Perhaps bind `complete-tag' to a key:
(global-set-key [f3] 'complete-tag)
Unlike complete-tag, dabbrev-expand, or hippie-expand (which does dabbrev-expand like things), the CEDET suite does exactly what the question describes. When asked to perform a completion, it looks and sees that you have included stdio.h, and then looks there for possible completions.
CEDET does a lot of other things related to completion as well which will provide very focused and correct suggestions, not just vaguely similar suggestions. A side affect is that CEDET takes more effort to setup. You need to teach it where you include files are, for example, and sometimes how to deal with macros, and what the project you are working on is like.
There is more detail on this here:
link text
You might want to try out M-/ (dabbrev-expand). This command attempts to complete the identifier immediately preceding the point (ie, where your cursor is) using the contents of the current buffer and then the contents of other buffers of the same mode. If the first completion offered isn't the one you want, just keep typing M-/. If you have the habit of keeping a single emacs session open continuously (which, if you don't have, you should really acquire), and have a handful of files from the current project open, you're quite likely to be able to find an the expansion you want for any particular prefix.
So, to answer you're original question, M-/ will find the printf completion you're looking for if (a) you've used printf anywhere else in the buffer you're editing, or (b) it appears in any other .c or .h file you have open in emacs.
You might also try hippie-expand, which has additional options regarding where it looks for completion information. I bind M-/ to hippie-expand, and then modified the order of the elements in hippie-expand-try-functions-list as follows:
(global-set-key (kbd "M-/") 'hippie-expand)
(setq hippie-expand-try-functions-list '(try-expand-dabbrev try-expand-dabbrev-all-buffers try-expand-dabbrev-from-kill try-complete-file-name-partially try-complete-file-name try-expand-all-abbrevs try-expand-list try-expand-line try-complete-lisp-symbol-partially try-complete-lisp-symbol))
This makes hippie-expand act like the normal M-/ at first, but repeated presses will yield more possible expansions.