tags:

views:

198

answers:

3

I found an awesome tip here. You can "[r]apidly invoke an editor to write a long, complex, or tricky command". However, when I press the key combination above, I get Emacs open. I would like to switch it to Vim. How can I invoke Vim with C-X e?

[1. Problem SOLVED by Brian Cambell]

export EDITOR=vim

Add to your .bashrc or appropriate shell rc file

[2. Problem SOLVED thanks to Pax]

I was unable to get the tricky command back to Bash. The errors were:

 > Error detected while processing BufRead Auto commands for "*":
 > E117: Unknown function: JumpToLastPosition

Quotes in .vimrc solved the second problem. I am still unsure about the part in my .vimrc:

  "  augroup misc
  "      autocmd!
  "      autocmd BufReadPost * call JumpToLastPosition()
  "      autocmd FileChangedShell * call WarningMsg("File changed outside of vim")
  "  augroup end

[3. Problem]

What do the above part in .vimrc do?

+2  A: 
export EDITOR=vim

Add to your .bashrc or appropriate shell rc file

Brian Campbell
Thank you. A problem is that I am unable to get the command back to bash. How can I get it?
Masi
+1 for solving the first problem.
Masi
If you save the file opened in the editor and exit the editor, the command will be run.
Brian Campbell
I did ZZ, and my command "echo hello; touch {1..99};" did not do anything. How can I redirect the output to stdout? I cannot understand the side-effect.
Masi
Hmm. I'm not sure what's happening for you. Can you run "script editor-transcipt", do the steps that you're taking, exit, and then post the transcript somewhere online (not here, since it will have control characters in it). That way I can see exactly what you're doing.
Brian Campbell
@Brian: You can see it here: http://pastebin.com/d52c323b3
Masi
@UnixBasics Hmm. Hard to say what's going on there, though it looks like your Vim is giving you several errors, so you might want to check your configuration. Does vim work if you execute it manually, with "vim somefile"? If not, you should ask a separate question about that. Also, it would be more useful to upload your transcript somewhere as a binary file, rather than pasting it as text, because the control characters get screwed up otherwise.
Brian Campbell
A: 

The link that you provided contains the answer:

Next time you are using your shell, try typing ctrl-x e (that is holding control key press x and then e). The shell will take what you've written on the command line thus far and paste it into the editor specified by $EDITOR.

You need to set the EDITOR environment variable (like @Brian Campbell)

lothar
+2  A: 

On most Linux installs (all the ones I tested), bash recognizes both the Emacs and Vi command history keys (or you can use "set -o vi" to force it).

So, you can just use the vi-mode "<ESC>v" to to enter visual mode, this will start editing in a Vim session.

To run the command, you just save and exit from Vim ("ZZ" or ":wq"). To cancel the command, you need to delete the contents, save and exit ("1GdGZZ").

In addition to running it by exiting, you can also save it while in the editor to another location (":w /tmp/myscript").

Keep in mind that visual mode will work with the currently selected line so you don't have to start with a blank command ("<ESC>v"). You can use the normal vi-mode tools to select a line from the history first and then enter visual mode ("<ESC>kv" for last command, "<ESC>/grep<ENTER>nnv" for third-last grep command and so on).

Using this method has the advantage of not changing the "EDITOR" variable which may be used for other things (unless you want Vim for everything, which is a very sensible position to take IMNSHO).

Update:

Regarding your error, posted after the question:

JumpToLastPosition() is the function called by Vim for all files to put the cursor where it was when you last edited the file. I'm going to assume you're actually getting this error when the editing starts, not when you exit, since this is the auto function following a buffer read.

Can you start a "normal" vim session ("vim xx.txt" and then "vim xx") without this error occurring? You may find you get the same problem (and possibly only on the last one).

If you do have the same problem, you need to look at your startup files. It's possible the autocmd for BufRead is broken somehow. Have a look inside your vimrc and you filetype.vim files to see where that function is called and/or defined (I suspect it's called but not defined and that may be a mismatch between the two files or one of them has been damaged).

paxdiablo
@You are right: "I'm going to assume you're actually getting this error when the editing starts, not when you exit, since this is the auto function following a buffer read."
Masi
+1 for finding the crux move: "It's possible the autocmd for BufRead is broken somehow. Have a look inside your vimrc --"
Masi
Nice rhetoric tone! "'EDITOR' variable which may be used for other things (unless you want Vim for everything, which is a very sensible position to take IMNSHO).". Have to look more on Emacs :)
Masi