I would like to know what are all the programmer-useful shortcuts that exists in emacs.
I come from a netbeans background and I am trying to make myself comfortable with emacs -text only environment. So I am looking at shortcuts for "refactoring" the code, "auto-completion", "go to definition" etc.
How can all these be achieved in emacs ? What are other programmer-useful shortcuts ?
I'll be using emacs basically for LAMP, javascript, C, C++.
ps - you can safely assume that I know how to open a file, save a file, navigate and whatever is in the tutorial in emacs.
For auto-completion, use etags with M-xtags-search or M-xetags-select-find-tag. I use macros often to do repetitive tasks. C-x(<string of useful tasks>C-x). Also, M-xalign-regexp to beautify the code and make it more readable.
You should find most of the most used features by Emacs users in this question's answers here at Stackoverflow.
Check this site
Some the important keybindings that are not there in the tutorial are:
Previous matching bracket: C-M-b (if it doesn't work, try ESC followed by C-b)
Next matching bracket: C-M-f (or ESC C-f)
Go to start of block: C-M-u
Go to end of block: C-M-d
Start of function: C-M-a
End of function: C-M-e
Outline mode: C-u 1 C-x $ (C-x $ to revert)
Newspaper mode: C-x 3 M-x follow-mode (especially useful with today's wide-screen monitors!)
Vertical Copy
Sometimes you will need to copy a vertical patch of data, e.g. one column in a table. First press C- where you want to start copying. Then go to the end of the column and press C-x r k. To paste the column press C-x r y. (If you don't want to delete original column, just press C-_ there once to restore it and then press C-x r y at target.)
To start, here is one :
Meta - / -> does code completion
M-x diff-buffer-with-file
M-x revert-buffer
When working with versioning (I use git), M-x diff-buffer-with-file is really useful. When you have a file open in a buffer in emacs, then you do a git checkout or some other action that touches that file, emacs will complain at you when you try to edit the buffer. M-x diff-buffer-with-file is helpful to see if you will break anything by keeping what's in the buffer. If something has changed and you want to grab the file from disc and put it in the buffer, do M-x revert-buffer.
Related
I am new to Emacs on Windows XP and have multiple queries which I could not find/understand after a lot of search.
I understand that speedbar will support showing of C functions list. As mentioned in http://stackoverflow.com/questions/259364/how-do-i-configure-emacs-speedbar-for-c-sharp-mode, I added the code in .emacs as
(speedbar 1)
(add-to-list 'speedbar-fetch-etags-parse-list
'("\\.c" . speedbar-parse-c-or-c++tag))
I see the speedbar startup but it never shows the functions. Maybe I need to install etags but cannot find it. I also found gtags but could never find a binary
I copied sr-speedbar.el in ~/emacs.d/ directory. I added (require 'sr-speedbar) in .emacs, commented out speedbar related code, reloaded M-x ~/.emacs but did not find any sr-speedbar. If I uncomment speedbar code and comment sr-speedbar, it shows the speedbar on loading. Emacs is able to find sr-speedbar.el in ~/emacs.d/ directory since if I change filename, it shows an error: unable to find sr-speedbar.
I wanted to make a keyboard macro for logging using tramp and ssh. However, when I record a macro, I have to delete multiple characters to provide the path from the beginnig i.e. emacs automatically shows the last path and so I have to delete all the characeters and then start again with ssh://. The keyboard macro records all backspaces and returns error if the backspaces are more than the length of the current path. Is there a way to avoid it
I configured linum package. I want it to start at the beginning of emacs session automatically with typing M-x linum. How can I do that?
Is there way to retrieve command history (not shell commands) and then replay some command
Thanks again for patience on reading until the point :-)
For C, C++, and other languages, supported by CEDET/Semantic, the Speedbar is able to show functions & other objects. But you need to setup Semantic correctly, for example, you can use my article in CEDET.
As I understand, to enable linum-mode globally, you need to put (global-linum-mode 1) into your ~/.emacs
For 3, there are two options:
just type "/ssh:blabla" at the end of the pre-inserted directory (this directory will be ignored as witnessed by it becoming grey).
do C-a C-k to erase the content of the minibuffer.
For 5, there is repeat-complex-command bound to C-x ESC ESC and there is repeat bound to C-x z.
I have been looking into packages that would enable me to have tabs in my emacs editor. Would like to open multiple files in a single emacs window. Any help/link would be appreciated
Emacs has this functionality built in. They are called buffers C-x C-f opens a new file of a name you provide. C-x b lets you change between buffers. C-x C-b lists all open buffers. C-x k closes the current buffer.
If you want multiple buffers in one window then C-x 2 splits the windows horizontally. C-x 3 will split it vertically. C-x 1 will close all the windows that are not the current one. C-x o cycles between open panes. &c.
GNU the organization that manages the code base for GNU Emacs has a wonderful tutorial
Note: the the above segment C- means while holding the <CTRL> key
post script: the ido library makes working with emacs in generally much easier.
You can already open multiple files in buffers. However, if you want a visual guide, then this is what you might want: http://emacswiki.org/emacs/TabBarMode
Caveat emptor: You might have to configure it a bit.
or you can use elscreen
and use Gnu/Linux too, so i remap the Winkey to manage my tabs.
emacs doesn't contain "tabs" like an internet browser its best to understand the concept of buffers like the others have said but if you want to have a tab bar at the top anyway just to give a quick reminder of what buffers are open I suggest looking at the TabBarMode in the emacs wiki.
When using buffers i like to use m-x ibuffer since you can filter out which buffers your interested in. say you only want to see the buffers which are associated with files in a /home/flood/coding/epicgame/ directory, i simple go to ibuffer, type / f then type epicgame and a filter will be applied, then to remove the filter simply press / twice and remember that C-h m is your friend.
This technique has helped me alot, you can even OR filters together and merge them into groups. I love emacs XD
I've recently started using emacs and I'm enjoying using it for the most part. The only thing I'm not enjoying, is switching between buffers. I often have a few buffers open and I've grown tired of using C-x b and C-x C-b, are there any packages that make switching between buffers easier? I've looked into emacs wiki on switching buffers and I'd appreciate insight/feedback on what are are using/enjoying. Thanks.
UPDATE: iswitchb-mode is obsolete in Emacs >= 24.4, replaced by ido.
All of the features of iswitchdb are now provided by ido. Ross provided a link to the documentation in his answer. You can activate with the following in your .emacs (or use the customization interface as Ross suggests):
(require 'ido)
(ido-mode 'buffers) ;; only use this line to turn off ido for file names!
(setq ido-ignore-buffers '("^ " "*Completions*" "*Shell Command Output*"
"*Messages*" "Async Shell Command"))
By default, ido provides completions for buffer names and file names. If you only want to replace the features of iswitchb, the second line turns off this feature for file names. ido will ignore any buffers that match the regexps listed in ido-ignore-buffers.
The behaviour described below for iswitchb-mode applies equally to ido for switching buffers.
iswitchb-mode (Emacs < 24.4)
iswitchb-mode replaces the default C-x b behaviour with a very intuitive buffer-switching-with-completion system. There are more sophisticated options, but I've never needed more than this.
After you hit C-x b, you are presented with a list of all buffers. Start typing the name of the buffer you want (or part of its name), and the list is narrowed until only one buffer matches. You don't need to complete the name, though, as soon as the buffer you want is highlighted hitting enter will move you to it. You can also use C-s and C-r to move through the list in order.
You can turn it on by default with this in your .emacs:
(iswitchb-mode 1)
You can also tell it to ignore certain buffers that you never (or very rarely) need to switch to:
(setq iswitchb-buffer-ignore '("^ " "*Completions*" "*Shell Command Output*"
"*Messages*" "Async Shell Command"))
You can use C-x <right> (next-buffer) and C-x <left> (previous-buffer) to cycle around in the buffer ring. You could bind S-<right> and S-<left> to these functions. (S is the "super-key" or windows-key). This way you can save some keystrokes.
Moreover, note that C-x b has a default entry, i.e. it displays a standard value (most of the time this is the previously viewed buffer), so that you don't always need to enter the buffer name explicitly.
Another nice trick is to open separate windows using C-x 2 and C-x 3. This displays several buffers simultaneously. Then you can bind C-<tab> to other-window and get something similar to tabbed browsing.
M-x customize-group ido then set Ido Mode to Turn on both buffer and file and set Ido Everywhere to on. Then click the Save for future sessions button at the top and enjoy ido magic for both files and buffers. Read the docs to get a sense of how to use ido.
Also, take a look at smex.
ido-mode provides an efficient way to switch buffers.
ibuffer is best for managing all opened buffers.
anything is good for selecting an interested thing from different
sources. (for eg: a single key can be used to switch to another
buffer or to open recently closed file or to open a file residing
in the same directory or ... anything you want ... )
If you've looked at the Emacs Wiki, you probably have all this information already, but here are a few other relevant Q&As:
Emacs: help me understand file/buffer management
Buffer switching in Emacs
How to invoke the buffer list in Emacs
My toolkit consists of ibuffer, windmove+framemove, winner-mode, and a custom binding to make C-xleft/right and C-cleft/right less of a hassle to use.
I have mapped the "ยง"-key to 'buffer-list and I find it to be very efficient.
I've started using anything for a couple of days and I'm really liking it: http://www.emacswiki.org/emacs/Anything .
Emacs-fu has an good intro to anything: http://emacs-fu.blogspot.com/2011/09/finding-just-about-anything.html
My favourite function for this is helm-mini which is part of helm.
As other helm functions, it allows incremental narrowing of the selection. It also searches your recently visited buffers, which is a really nice way to re-open a buffer. Helm can be a little surprising at first and as a new Emacs user, I found it visually overwhelming and I preferred ido or ibuffer which have been suggested in other replies. But now I absolutely love it and use it all the time for countless things.
Something that I realized by accident and that can be useful:
mouse-buffer-menu is by default bound to <C-mouse-1> (Control key + mouse left click) and opens a popup with a list of the current buffers.
Sorry if these are obvious answers but I've googled around and can't seem to find what I'm looking for.
When I have multiple files open in split screen mode using C-x C-3 how do I close JUST the window I'm currently in and no other?
I'm using Ruby and it doesn't seem to auto indent my def end correctly.. is there a Ruby plugin that will fix this?
Is there a command to go to a specific line?
How do I interpret ruby from within emacs?
1. When I have multiple files open in split screen mode using C-x C-3 how do I close JUST the window I'm currently in and no other?
To close current view C-x 0. (Btw, I think you're referring to C-x 3.)
2. I'm using Ruby and it doesn't seem to auto indent my def end correctly.. is there a Ruby plugin that will fix this?
Have a look at
ruby-mode for emacs
RubyMode
3. Is there a command to go to a specific line?
Sure, M-g g (or M-x goto-line)
4. How do I interpret ruby from within emacs?
Don't know. You could try rubydebug.
You can also see C-h b which will give you key binding for the current-buffer to give you a lot of additional things that you might need. Once you install any Ruby Mode, you can also use C-h m to see key binding for that specific modes in your current buffer, including ruby mode.
for Q3) in addition you might want to know
C-h w COMMAND
will always show you the key binding for a funcion
C-h w goto-line RET
goto-line is on M-g g, M-g M-g, <menu-bar> <edit> <goto> <go-to-line>
for Q4) you could start a shell in a buffer, either M-x eshell or M-x shell and the simply run your script there.
Using the eshell configuration in [1] you can easily switch between the shell and your script source (pressing C-z).
[1] http://www.emacswiki.org/alex/2008-08-19_Emacs_on_Windows
I am working on an emacs mode for a review tool, and want to use ediff for viewing diffs... the problem is that the review tool only yields a diff, rather than old and new files. I'd like to put the old file in a buffer and apply a diff rather than copying the file, calling out to diff as a subprocess and loading it into a separate buffer.
Can this be done without writing it myself, does anyone know?
ediff-patch-buffer can be used to patch a buffer in ediff-mode
Depending on what your workflow is this may or may not apply.
Try
M-x diff-mode
From which you can do commands like:
C-c C-a diff-apply-hunk
C-c C-b diff-refine-hunk
C-c C-c diff-goto-source
C-c C-d diff-unified->context
C-c C-e diff-ediff-patch
The most intuitive flow for me is the 'diff-ediff-patch.
When you're looking at a patch, run C-c C-e, enter the file name to patch (say file.to.patch). Emacs will then set you up with an ediff of two buffers file.to.patch.orig and file.to.patch containing the original file and the patched version. You then use ediff as you would regularly. At the end of the flow, you have the patched version in the filename you want.
Check out the documentation for diff-mode.
There is also M-x epatch that asks for a buffer containing a patch (Gnus mail for example) and then asks for a file/directory to patch. Very handy.