I use vim as a viewer in midnight commandder and I want to quickly view and close a file. How can I map the escape key to :q
in normal mode so that vim exits only if nothing is changed?
-
Why not use an actual "viewer" instead of an editor?– romainlCommented May 3, 2015 at 7:27
-
Because it gives me much more capability such as configable code highlight. And I can edit the file in the case I like and exit with no pain if it is not modified.– OxyCommented May 4, 2015 at 17:32
1 Answer
To do it for just the current vim
session,
type the following commands (in vim
). (Spaces are added for clarity.
Do not type a space except where I have indicated Space.)
: m a p Space Ctrl+V Esc Space : q Ctrl+V Enter Enter
To do it for just the rest of the current Terminal session, type the following commands (in Terminal; i.e., the shell). (Line break(s) are added for clarity. Type the following all on one line; do not type Enter except where I have indicated Enter.)
E X I N I T =
" : m a p Space Ctrl+V Esc Space : q Ctrl+V Enter " Enter
and then
export EXINIT
Enter
To make the changes “permanent” (i.e., until you change the files back),
put the above commands (the EXINIT="…"
and export EXINIT
)
into your .bash_profile
and/or .bashrc
.
(You can put them together on the same line, separating them with ;
.
You may be able to shorten this by combining them into export EXINIT="…"
.)
This won’t take effect until you login again,
or possibly until you start a new shell,
unless you just type the EXINIT
commands
or source
the file into which you put the EXINIT
commands.
Note: if you aren't using bash as your primary shell, you may need to modify the above for your shell.
Alternatively, try putting the :map …
command into your .exrc
file.
In case this isn’t obvious,
:map string1 string2
(in vi
/ vim
) creates a mapping (translation) so that,
when you type string1
(in command mode),
the editor acts like you typed string2
.
And Ctrl+V is the super-escape character
that tells both vi
/ vim
and the shell
(in fact, probably all programs, or at least most of them)
that the following character is meant to be interpreted
as a literal textual character, and not as a control character.
Note that Ctrl+V followed by Enter
gives you a carriage return character, which is often displayed as ^M
;
but, in a :map
string2
,
that is interpreted as an Enter.
-
Thanks G-Man. The first approach works perfectly well (but is useless as you know). The second one does not work. I am on Mac OS. Do you have any idea why it's not working?– OxyCommented May 1, 2015 at 21:56
-
I followed the same procedure with
~/.exrc
file and it does not work as well.– OxyCommented May 1, 2015 at 21:59 -
@Oxy: I edited the answer to clarify possible misunderstanding. I don’t know why it isn’t working. Can you get other commands through into
vim
by any of those techniques? For example, do you already have a~/.exrc
whose contents are working? If you typeexport EXINIT=":map q jjjj"; vim some_file
, does (q
) make your cursor go down four lines? If you typeexport EXINIT=":set list"; vim some_file
, does it show$
at the ends of lines (and does it show tabs as^I
)? Commented May 1, 2015 at 22:25 -
-
Well,
vi
should bevi
the world around, but OS X isn't Linux. Try researchingvi
on OS X (start withman vi
, and then search Super User, and then Google) to see whether it has some different way of letting you pass in initialization commands. Commented May 1, 2015 at 22:35