Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm currently in the middle of attempting to gain proficiency in Emacs. I have to admit that it's difficult being someone that's spent 10+ years using Vim. I'm used to being able to just hit HJKL as a nervous habit, which doesn't go over so well in Emacs. I feel like there are many things in Vim that seem superior at first glance.

For example, if I want to do a regexp search/replace in Vim, I can do this from Normal Mode (which will replace throughout the whole file):

  :%s/regxp/replacement/g<enter>
Emacs (by default) has `replace-regexp`:

  M-x replace-regexp<enter>regexp<enter>replacement<enter>
My only problem? This only replaces from the cursor to the end of the file/buffer, so to replace over the whole file I would need to do:

  M-< M-x replace-regexp<enter>regexp<enter>replacement<enter>
It just seems that much more complicated, and now I've lost my place, so I'm at the top of the buffer rather than wherever I was in the file.

On the other hand, I find Emacs Lisp to be much easier to deal with then VimScript/VimL even as someone that has written my own Vim plugins, and created plugins to patch issues with files in the standard Vim dist (too lazy to submit a patch and wait for the next Vim release).



This obviously isn't a solution to your valid complaint, but you can use C-u C-SPC to navigate to your previous cursor position. It can band-aid your issue, but it's also quite useful in general.

https://www.gnu.org/software/emacs/manual/html_node/emacs/Ma...


I find that you need to be willing to write your own little elisp routines to get the best out of Emacs. If whole-file replacements is how you like your search and replace, then write a routine for that. My bugbear with regex replace - apart from rebinding it to M-r - was that, when applied to a region, it didn't keep the region. So I wrote a macro that "advised" the function (and any other function I choose) with code that restored the region after the command was finished.

Be aware, however, of iedit-mode. Select a region of text and enable iedit-mode, and any change to the selected region will be propagated to every other instance of the region's text throughout the document.

Using iedit-mode in combination with rgrep (recursive grep) and wgrep (editable grep buffer that propagates changes to the underlying documents) is a decent way of doing project-wide refactorings.

And of course, neither iedit nor wgrep are part of the base emacs install. Using the package manager is essential.


You can check https://github.com/syl20bnr/spacemacs if you want to use Evil mode not just for editing but for everything else in Emacs. Just clone to your ~/.emacs.d and start Emacs then everything will be automatically installed for you. Most key bindings outside of editing are Vim-like, created by Vim users. You already stick with Vim for 10 years and learned many things about, so I suggest you to keep that knowledge. But then, aside of editing text, if some Emacs utility does not have Evil key bindings, you should also learn the default, because most of the utilities are always in normal mode, so you only need to press a single key to execute most commands.

spacemacs also has extensive documentation, so you can learn key bindings easily and be productive with Emacs immediately without any extra configuration.


Here's some elisp I use to do this (assuming I understand your issue correctly and it's the same as mine):

  (defun replace-regexp-g ()
    (interactive)
    (save-excursion
      (goto-char 0)
      (call-interactively 'replace-regexp)))


Are you aware of evil-mode? One of the many things it implements is the :%s/stuff/otherstuff/g syntax.


I'm aware of evil-mode. I'm attempting to try 'the Emacs way' first. I'm not attempting to just throw myself in. When I need to be proficient I'll fallback to Vim, but I want to gain access to things like org-mode, for instance.


"The Emacs way" is very much "learn some elisp and write whatever you want".

I switched from Vim to Emacs and in the beginning there were some things I really missed. One that I remember was "*", which in Vim/my config highlights current word in whole buffer and let's you n/N between matches. In Emacs, doing the same with M-left C-s C-w felt just wrong.

Fixing this was a matter of 2-3 lines of very straightforward elisp. I since started relying more on Occur mode and IEdit mode for this, but the experience of lacking a feature and adding it absolutely painlessly remained with me.

Writing simple elisp and simple editing-related features is a joy under Emacs. It's much easier than in Vim, for a number of different reasons. This makes me think that "the Emacs way" is about scripting and programming it to do whatever you need it to do.


You may also be interested in https://github.com/nschum/highlight-symbol.el which provides highlighting of the current symbol, and provides highlight-symbol-{next,prev} commands to move between occurrences of a symbol.


Have you taken a look at NeoVim (https://github.com/neovim/neovim)? They plan on replacing VimScript with Lua.


I'm watching NeoVim, but I'm skeptical about their ambitious goals (i.e. refactoring internal Vim code). I do wish them luck though.


M-< C-M-% regexp RET replacement RET C-u C-space

If you are just replacing plain text (no regexps) then take a look at iedit. Then C-; will edit all occurrences of the word at cursor.


Iedit mode works beautifully with wgrep + rgrep/ack/ag results.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: