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).