Vim-like */# - (next/previous word at point) in Emacs? - emacs

I switched from Vim to Emacs, and I am so crazy for Emacs now.
But there is a very, very useful trick in Vim and I can't use conveniently in Emacs, that's find next/previous word at point. Those are very handy with */# in Vim.
The simplest way is to move to the beginning of the word at point and then C-s and C-w, use C-s/C-r to find next/previous word.
Then I found another trick from Mastering Emacs, but still some minor bug.
Today I find a plugin - vimpulse which simulate vim in Emacs. And I can use */# there just like Vim!!
But seems the vimpulse will automatically enables Viper.
So, are there any other methods to implement this requirement? Or, Can I automatically disable Viper if I use vimpulse?

First, the answer
Well, as is often the case, we can do better in Emacs. The package I use for this is highlight-symbols
Specifically, I bind a series of its commands with variations of the F3 key:
;;;;;;;;;;;;;;;;;;;;;;
;; highlight-symbol ;;
;;;;;;;;;;;;;;;;;;;;;;
(require 'highlight-symbol)
(global-set-key [f3] 'highlight-symbol-next)
(global-set-key [(shift f3)] 'highlight-symbol-prev)
(global-set-key [(control f3)] 'highlight-symbol-at-point)
(global-set-key [(control meta f3)] 'highlight-symbol-query-replace)
Next/previous symbol is nice. But I find it most helpful when reading an
algorithm to highlight a few key variables.
On symbols vs words
The distinction between words and symbols is very nice for
programming. I'm not sure if other editors offer this distinction.
An alternative
Another related tool I find very useful for programming is iedit.
Here is how I load it:
(autoload 'iedit-mode "iedit")
(global-set-key [(control \;)] 'iedit-mode)
(define-key isearch-mode-map [(control \;)] 'iedit-mode)
To do what you want, go to a symbol and press C-;, now
tab and shift tab will move amongst matching symbols.
C-' (single-quote) will show you an adhoc occur view of the
buffer.
For a bonus refactoring tool, mark a region (maybe a class) where you want to
rename the symbol (variable), and press C-; again and only matches
within that region will be edited.

Try Evil: https://gitorious.org/evil/pages/Home
*/# works nice with it.

Related

Emacs, org-mode, evil-mode - TAB key not working

I've been working with VIM for decades, and I've become quite proficient in it. I was however sort-of... seduced by Emacs's org-mode, and in order to try it, I installed Emacs and Evil.
Evil satisfies most of my VIM-related muscle memory, so I proceeded with my testing of org-mode - and met my first issue: when I spawn Emacs in its own window (i.e. emacs plan.org) then the TAB key works, opening and closing my plan's sections just fine. However, TAB does nothing when I use Emacs in text mode (i.e inside my XTerms, via "emacs -nw plan.org"). And that's the state that I am mostly interested in, since I usually work from inside screen/tmux over SSH connections.
If it's a conflict with Evil-mode, I don't understand why - I am unaware of any TAB functionality in VIM's normal mode (which is what we're in when opening/closing org-mode sections).
Any Emacs-guru out there with a suggestion on why this happens?
Try
(setq evil-want-C-i-jump nil)
in your ~/.emacs before
(require 'evil)
Evil has, in evil-maps.el
(when evil-want-C-i-jump
(define-key evil-motion-state-map (kbd "C-i") 'evil-jump-forward))
That should give you org-mode Tab functionality back
I have almost no experience with terminals. However, I know that TAB is equivalent to C-i. Maybe that one would go through the terminal? If that works, you could add some key bindings for every TAB operation?
Try maybe C-h k TAB as well to see if TAB if sent on the wire.
(define-key evil-normal-state-map (kbd "M-i") 'evil-jump-forward)
(define-key evil-normal-state-map (kbd "M-o") 'evil-jump-backward)
I bind the function to other keys, so it's also work.

Better defaults for emacs

I've been using Vim for a several years. And now I want to give a try to Emacs.
For Vim I have a general config file (here) where I'm overriding defaults (e.g. hey, Vim, show me the line numbers; save more history, don't create these stupid backup files, etc...)
I want the same thing for Emacs. While searching, the best thing I've found is better-defaults.el from technomancy. I'm still digging in Prelude and Emacs-Starter-Kit sources, but there are too many overrides and plugins.
So, what I want:
ability to see a list of variables, which I can customize (e.g. indent-tabs-mode or newline-and-indent). I know about C-h v variable-name but this command requires me to know a name of variable, but I want a list of them
sample config file for Emacs which sets helpful defaults with comments for each command
For your first question: M-x customize-option.
C-h v TAB is not what you want, as it shows you also non-option variables (e.g., internal variables).
However, if you load library help-fns+.el then C-u C-h v TAB shows you only the user options (in buffer *Completions*).
My advice would be to not look for an existing "sample config file", if you intend to start with it, as opposed to just seeing how another user redefines things. And for help with the latter, I would still recommend the Emacs manual over looking at someone elses init file. Especially to start with.
However, if you really want to look at init files from other users then this is the place to start. (And this is a good place to start, other than the manual (which is the best place), to learn about customizing Emacs.)
Finally, my (unsolicited) advice wrt learning Emacs, including customizing, is to start by not customizing it at all. I say that without irony as one who has heavily customized Emacs.
If you want to "get it", i.e., to get a feel for the Emacs design and what makes it different, then let yourself get used to Emacs as it is out of the box -- for maybe a month or so. At that point you can think about customizing, and your customizations are likely to be much wiser (in your own terms, i.e., for whatever it is that you want).
Another way of putting this is that until you know Emacs a bit, you really do not know what it is that you want or need in terms of customization. In particular, it would be a mistake, IMO, to start out by trying to think of Emacs in terms of Vim or trying to make Emacs do what you've done in Vim. There is plenty of time for that later, if, based on understanding Emacs, you really do want to do that.
Welcome to Emacs. Enjoy.
I'm going to take a reasonable dissent from Drew's excellent answer, there are some things you really ought to set in your emacs-file immediately, that aren't set out of the box that you really ought to set.
Issue number 1: THAT $(generate-swearing) BELL!
The bell will ding like a madman. That's annoying. You can turn it off.
In your init-file, do this:
(setq visible-bell 1)
Issue number 2: Emacs has an interesting view of backup files.
If you edit a file, say "foo.txt", emacs will create little backups of the file with the name "foo.txt~" in the same directory.
This is annoying as all hell, and you can fix it by doing this:
(setq backup-directory-alist '(("" . "~/.emacs.d/emacs-backup")))
Issue number 3: Emacs uses C-w differently than bash does, and that's a bit annoying.
C-w usually deletes a word backwards. By standard in emacs, it deletes the marked region. That's a bit silly.
It is better to do something like this:
;; This is my preference, your mileage may vary.
(global-set-key (kbd "C-x C-k") 'kill-region)
(global-set-key (kbd "C-x k") 'kill-buffer)
(global-set-key (kbd "C-w") 'backward-kill-word)
Issue number 4: Alt-X is a clunky way of running an interactive command.
It is better to do something like this instead, avoid your hand cramping up all the time.
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
You also may want to check out Steve Yegge's Effective Emacs: https://sites.google.com/site/steveyegge2/effective-emacs
It's pretty amazing. One thing to note though is that the caps-lock to ctrl thing is also available through a microsoft tool here:
https://learn.microsoft.com/en-us/sysinternals/downloads/ctrl2cap
This is better than the manual hack Yegge suggests, and you can turn it off if you don't like it.

emacs get rid of C-x

Is there a way I can stop needing C-X before any shortcut and, for example, just use C-c to leave?
Also, if possible, what disadvantages could this have?
I'm looking for a simpler way of using GNU Emacs, but not sure if I can find one.
(this is too long for a comment)
Not really an answer but I know that some people hate the "C-x anything" in Emacs not just because you have to "type a lot of keys" but also because from a touch-typing point of view C-x doesn't make that much sense.
But of course in Emacs everything is configurable. I'm using "C-," instead of C-x and I honestly find it much easier this way.
I use my left pinky to hit CTRL, which is the key physically located at the left of 'a' on a QWERTY keyboard: i.e. the key that used to be CTRL but that now often is labelled CAPS-Lock [and you can remap it to CTRL].
I then touch-type ',' with my right hand.
To this end I added this to my config:
(define-key global-map [(control ,)] ctl-x-map)
You won't have less keys to type to do, say, a C-x C-c (which you can now do both by doing C-x C-c or by doing C-, C-c), but at least you won't be distorting your fingers as much ; )
Also note that as explained here (see user "scottfrazer"'s +40+ upvoted and accepted answer) it may be better to create a minor mode for all your key mappings instead of directly define a global mapping:
Globally override key binding in Emacs
My recommendation is to first learn the standard Emacs key bindings, then, after a while change whichever ones you think will do you the most good to change.
See also CUA mode, in the Emacs docs -- I don't use or recommend CUA mode, but a lot of people (esp. newbies?) use it to keep their cut, copy, paste key habits.
After looking at the source code of boon.el, I found out about ctl-x-map. C-x is a prefix key, prefix keys often have their own maps.
To use M-m for C-x shortcuts and unbind C-x, use this
(global-set-key (kbd "M-m") 'ctl-x-map)
(global-set-key (kbd "C-x") nil)
I think it is a very bad idea, because the C-x prefix can handle such situations like C-x s vs C-x C-s. And: you are really f*cked when you read emacs tutorials and have your own keybindings
If you want some other keybindings read ErgoEmacs Keybinding or How to Set Emacs's User Interface to Modern Conventions

How to switch between visible buffers in emacs?

If I am in split-screen viewing 2 different buffers on Emacs and the cursor is on the top buffer, what's a quick way to move the cursor to the bottom buffer?
Bonus question: if I know a command, is there an easy way to identify what key-combo it's bound to, if any?
To switch to other buffer use: C-x o.
Describe key: C-h k.
Here is a better solution when you open more than two windows(buffers) in one frame:
(global-set-key (kbd "C-x <up>") 'windmove-up)
(global-set-key (kbd "C-x <down>") 'windmove-down)
(global-set-key (kbd "C-x <left>") 'windmove-left)
(global-set-key (kbd "C-x <right>") 'windmove-right)
Now, you can use C-x UP/DOWN/LEFT/RIGHT to go to the above/nether/left/right buffer when you have three or more in one frame, they are more precise than 'other-window and you don't need to install any package.
You even CAN make it to cycle the buffers in the direction(vertically/horizontally) with one of the above shortkeys with configuration in .emacs/init.el file, but I don't recommend it(besides I don't remember it anymore, you can google it if you want).
Of course, you can use other shortkeys other than the ones I use in my .emacs.
You may also be interested in WindMove, which enables "directional" window navigation with <S-up>, <S-right> etc.
With respect to the bonus question, if you know the command (other-window), and you invoke it with M-x other-window, Emacs will show a brief message in the minibuffer stating: "You can run the command `other-window' with C-x n".
There is also M-x where-is which prompts for a command and gives you the current bindings that result in that command (if any).
There is a tutorial that's shipped with Emacs. It actually has the answer to your question (see the section MULTIPLE WINDOWS about 80% into the tutorial). The tutorial can be accessed via C-h t, or M-x help-with-tutorial. There's also a link to the tutorial on the initial splash screen of Emacs. Right below the link to the tutorial is a link to the on-line Emacs Guided Tour. The tutorial walks you through basic editing/movement commands, the guided tour is more of an introduction to what Emacs has to offer.
If you want to navigate among only buffers that are currently displayed, then you really want to navigate among the windows they are displayed in. This gives you a way to do that, using window/frame names that are the same as the buffers:
See Better window navigation in Emacs?

How to make Emacs (without GUI) differentiate between Ctrl+Shift+S and Ctrl+S?

I'm trying to teach Emacs standard shortcut keys. I use emacs23-nox version from Debian, often through SSH connection. One of those is CTRL+SHIFT+S for SaveAs. The relevant part of configuration looks like this:
(global-set-key (kbd "C-S") 'write-file)
(global-set-key (kbd "C-s") 'save-buffer)
but causes either both CTRL+SHIFT+S and CTRL+S invoke 'save-buffer (or 'write-file, depending on which line comes last). I also tried "C-S-s" instead of "C-S".
No Use of Shift Key section of Why Emacs's Keyboard Shortcuts Are Painful says it's not possible. I read this answer, but the accepted solution doesn't work for me (C-h k CTRL+SHIFT+S prints C-s).
Is there a way to make Emacs (the no GUI version) differentiate between CTRL+SHIFT+S and CTRL+S?
You can't, there isn't any provision in the character sets for uppercase control characters. The only way you can get shift and control is by going direct to the windowing system, so you need the GUI version.
To clarify a bit: If you are using Emacs with a window manager, then you can bind the key [(control shift ?s)], which is C-S-s (aka C-S, but not written this way in Emacs). This key is definitely different from the ASCII control character C-s, which has no lowercase/uppercase versions.
So the answer is (a) you have to be using a graphics terminal; (b) bind [(control shift ?s)] to get the uppercase version.