changing the path on the emacs eshell

The emacs eshell is an interactive shell for entering commands written in elisp. I like it because it feels like a normal emacs buffer and I can easily move around and copy and paste, without switching to a copy mode. It loses many of the features of bash, but when I need them I can use bash, inside or outside of emacs.

I had a hard time figuring out how to set the path it uses when I type commands, though. I wound up finding a way that worked for me by reading the source of the which function and then the code of eshell-search-path, which the which function uses to find executables in the path.

The variable is eshell-path-env and it’s a colon-separated list of directories. I needed to add my nodebrew directory to it, and I did so by adding the following to ~/.emacs.d/init.el:

(setq eshell-path-env (concat "/Users/bat/.nodebrew/current/bin" ":" eshell-path-env))

I’m not sure that this is the best answer, but it took me a while to find and answer that worked so I thought I’d share it.

vim: change current directory

I like using the excellent FuzzyFinder plugin for vim. It’s usefulness is heavily dependent on keeping the current directory what I want, though, because otherwise I could do a lot of drilling. I found that I needed a better way of changing directories. What I wanted most of the time was a way to change to the directory of the current file.

I found this while searching for a way to switch to the current directory. I’m not sure whether I picked the same mapping as the author.

map ,C :cd %:p:h<CR>

Admittedly, I’m not sure where the :p:h comes from, but it works well. A couple of months after I found this, I made a second mapping for lcd. Additionally, because I want to lcd to the current directory more often, I made lcd the more convenient remapping.

map ,c :lcd %:p:h<CR>
map ,C :cd %:p:h<CR>

A word about lcd: it changes the directory for the current window, not the current buffer. This sounded obvious to me after I figured it out, but prior to then I thought it was tied to the buffer it’s invoked on and buffers spawned from that buffer.

Another thing I’d like to learn is how to change to the global directory when a local directory is set (or remove the current local directory).

A brief journey back into to emacs-land

In an earlier post, I said that I am no longer going to deny myself the pleasure of dabbling in different editors. It was my interpretation of the “use one editor; use it well” mantra I got from the pragmatic programmers. It was probably a misinterpretation of it. They also would recommend using the best tool for the job, and how can you know what the best tool for the job is without learning about all the alternatives? And, in the case of text editors, it’s practically impossible to know every one of them well enough to judge which is best.

One day I was feeling frustrated with vim, so I fired up emacs. I wanted to remind myself of one thing it has that vim lacks—the ability to view/edit a single buffer in multiple frames. Pretty nifty, really. If you’re unfamiliar with the terminology, in Emacs a frame is a top-level window (think JFrame), and a window is a split within a frame, or the whole frame if it isn’t split. In MacVim, each frame has its own vim instance with its own set of buffers, so you can’t view the same buffer in two different frames, which would be nice for having a big frame and a frame that can sit alongside other windows. In emacs it works without a hitch.

While I was in emacs, I noticed something else I like about it—the scratch buffer. In vim there’s a blank buffer that shows up when you start it, but it seems like vim would rather pretend it’s not there. If you delete a buffer, it closes any window (i. e. split view) that is associated with it, unless there is only one window in the frame, in which case it grudgingly shows the scratch buffer (or the last buffer if there’s another buffer still open).

I can think a couple of things that I gained from this experience. First, I can see that there are other developers who can sympathize with my experiences. If I ever get frustrated with vim, I can take refuge in the emacs community. Same for when I got frustrated with emacs and became a vimmer. Second, I was able to take the scratch concept and apply it to another part of my computing experience. I created a directory in my home directory called “scratch”. It’s going to be my new temporary working directory. I had one called sandbox but the name never quite felt right and I never got in the habit of using it.

I don’t know which editor I’ll be using a year from now. But for now, it’s vim, for the most part. I do want to try out SLIME and see how viable Viper-mode (vim emulation in emacs) is, though.

vim and the enter key

This is just a quick update on how I’m using the enter key in vim. The sticky shift-enter key configuration didn’t work out for me. After pressing shift-enter once, I found that I would press shift-enter again, expecting it to create a newline. I wound up just remapping <CR≫ to <Esc>, and using shift-enter to insert newlines, or pressing enter and then “o” or “O” to create a new line above or below the current line. I use the latter option most. The only time it doesn’t work is when I need to split a line into two. For that I use shift-enter.

Shift-enter doesn’t work when using vim in a terminal, so the remapping doesn’t work quite as well in the terminal. I use it anyway, though. I should remap something to split a line into two, but so far I haven’t. I use vim in the gui (MacVim) most of the time. The couple of times I’ve had to split a line in the command line, I’ve hit:

  • D to delete the rest of the line and yank it
  • o to open a new line
  • Enter to get out of insert mode (since it’s remapped to Esc)
  • p to paste the rest of the line, that I deleted earlier

…which is terrible. If I start using command-line vim more often I’ll add a mapping for it.

This might seem like a pain, but for me it beats reaching for the Esc key. YMMV.

Update: zenzike pointed out in the comments that if you remap CR (the enter key) in the terminal, Control-CR still inserts a newline. So you could use Control-CR to insert a newline if CR was remapped to Esc. Unfortunately, this doesn’t work for me in OS X’s terminal or in iTerm by default. It does, however, work in gnome terminal running under Ubuntu on VMWare Fusion. I’m not sure why it doesn’t work in either of the two OS X terminal programs. I would be curious to learn why it doesn’t work and how to fix it, though, as this otherwise a very nice solution.

vim and vimperator, a few weeks later

A few weeks ago, I started using vim as my main text editor. Soon after that, I installed the vimperator Firefox extension on my main computer. A few days into the use of these two tools, I was really excited about both of them. It takes more than a few days for me to know whether I’m comfortable with using a particular piece of software regularly, though.

I’m still using vim every day, and I like it. It’s powerful, ergonomic, and customizable. There are a few annoyances. I still haven’t figured out how to properly configure Firefox to use MacVim as an external editor. Vim doesn’t support having multiple frames for a single instance of vim (emacs does). The official wiki, hosted by Wikia, has horrible ads, and I refuse to install an ad blocker, because I don’t want to turn a blind eye to virtual blight like the maintainers of vim have. Nevertheless, I’ve become quite comfortable with vim and will continue using it. While I’d like it much more if it didn’t have these annoyances, it has relatively few annoyances compared to some other editors I’ve tried, and the good things about it outweigh the bad.

My experience with Vimperator was different. As with vim, I really enjoyed using it at first. Over time, though, I found myself growing tired of it, even though it enabled me to browse faster. The biggest problem for me was that the commands only work if I’m in the normal mode, and it’s very easy to get thrown out of the normal mode. The two things that most often put me in the wrong mode were full-page Flash files and JavaScript that placed the focus in text boxes. Another issue was that I couldn’t easily use the keyboard to scroll in a div. I can’t easily do that with normal Firefox either, but Vimperator is supposed to make browsing with the keyboard easy, and it does it for the most part.

I uninstalled vimperator, but I miss its functionality. I’d like a lightweight extension that makes it easy to click links or jump between form fields with the keyboard. I don’t think that the vim input model is suitable for keyboard navigation within a browser, because the browser environment is too unpredictable. I’d like to see a couple of multi-key combinations that activate keyboard input, though. These would work inside of text fields. Flash would probably still break them some of the time, but to deal with that I could install FlashBlock.

I really enjoy trying out new development tools. I think the next thing I’ll try out is a visual CSS editor. Any suggestions?

Idea: Sticky Shift-Enter in vim

I set up vim to use enter to get out of insert mode. This prevents frequently having to reach for the escape key or use a multi-key combination. To insert a new line while in insert mode, I now use shift-enter.

An idea I had this morning is to make shift-enter swap the shift-enter and enter bindings. This way, if I’m in the mode of writing something new without manipulating text, I can push shift-enter once to enter a newline, then push enter for each additional newline, and push shift-enter when I’m done to get out of insert mode.
shift-enter

To implement it, I created a function that toggles the bindings, and added a call to the function to my bindings for <S-Enter>.

function ToggleEnterMappings()
  if mapcheck("<CR>", "i") == "<Esc>"
    iunmap <CR>
    inoremap <S-Enter> <C-o>:call ToggleEnterMappings()<CR><Esc>
  else
    inoremap <CR> <Esc>
    inoremap <S-Enter> <C-o>:call ToggleEnterMappings()<CR><CR>
  endif
endfunction
call ToggleEnterMappings()

Time will tell whether this is a good setup for me. I think if I do single-line inserts much more often than I do multi-line inserts it might.

Using emacs again

After watching the Meet Emacs screencast on Peepcode and talking to a few local developers who use emacs, I’ve started using emacs again. Since I started playing around with it several weeks ago, I’ve learned about a number of useful features I either hadn’t used before or had just barely scratched the surface of. These include the following:

  • ido-mode – A tool for quickly finding files that uses a combination of history and intelligent name searching.
  • hippie-expand – Intelligently completes part of a word to match another word in the current buffer.
  • text macros – I’d seen this before but didn’t realize how often it can be done in place of a regex search-and-replace. Much more intuitive, too.
  • C-x C-v – Unlike the more commonly used C-x C-f, which loads a file in a new buffer, visit-file (C-x C-v) loads a file in the current buffer. This solves a common annoyance for me of opening the wrong file in the right directory, and having to choose between leaving the wrong file open or navigating back to the right directory after closing the wrong file.

These are things I wouldn’t have noticed if I had still been blindly following the Pragmatic Programmer tip, Use a Single Editor Well. The problem with picking an editor and sticking to it is that there really isn’t a way to learn how good an editor is without actually using it.

I’m also starting to think that maybe, even after I’ve gained a pretty good understanding of most editors, I still won’t want to pick a single editor. Maybe I’ll use emacs for some things and TextMate for others. Using more than one editor might not be for everybody, but for people who take a serious interest in editors, it might be a good idea.

I put my emacs dotfiles directory up on GitHub, if anyone is curious about the customizations I’ve made to it.

So far I like my new setup, and I’m using Emacs most of the time. I’m getting to be quite comfortable in it. I’ve dealt with a couple of things that used to annoy me most about emacs, which are confusing indentation and inaccurate syntax highlighting. I dealt with the former by setting the tab key to go to the next tab key, rather than use the current mode’s indentation code. To deal with the latter, I just turned off syntax highlighting. I’m actually quite comfortable without it most of the time, and if I really want pretty syntax highlighting, I can always fire up TextMate.

In the future, I may continue to use emacs as my primary editor, or I may not. If not, I don’t think my time learning Emacs will have gone to waste, as I’ll have learned new editor features and I’ll know what to look for when I explore the advanced features of whichever editor I’m using.