I have been using vim for awhile but I have not been able to figure out how to map the escape globally - every time I open a file, I have to map the escape key to jj like this:
:imap jj <Esc>
Is there a way to change the runtime defaults so that Esc is already mapped to jj?
I found the default.vim file, but you cannot edit it on a mac (with standard mac software). Should I download something to edit this file? Is there another easier way?
Any help is much appreciated!
The place for your local changes is called .vimrc and you typically create one in your home directory with the commands you want to execute on startup.
Run vimtutor on the (system) command line and spend a few minutes learning how to use this editor. Then dig into the facilities provided by :help in Vim. E.g. run :help vimrc in Vim.
I'm reading this book and I really do not understand what this author is talking about. It appears that you can program your mac to go forward one character by either hitting l or ^F. I do not understand the difference between emac key stroke and vim keystroke. He also says run this command and 'place it in your $HOME/.bash_profile but I cannot figure out how to place the command in the bas_profile.
The Author is talking about make you terminal console behaves like vi, this means typing set -o vi in your terminal, the console will work similar vi. So you will be able to navigate using the motion keys of vi, use INSERT mode, x to delete, etc.
You can set that permanently if you include this command in your ~/.bash_profile file.
If you are not sure what it does, I don't recommend so.
I tried to map <Alt+D> to <Ctrl+D> by adding the below line to .vimrc, but it doesn't work. I checked the .vimrc is loaded by Vim.
map <Alt-D> <C-D>
Is there any error in this mapping?
To Mac users out there: for mapping ALT+hjkl, use instead the real character generated (find out which character using the combination while in INSERT mode), for example with my keyboard I get:
<ALT+j> ==> ª
<ALT+k> ==> º
and so on.
Solution found here on StackOverflow.
I used this to move lines up and down with ALT+k\j, using this on my .vimrc:
nnoremap ª :m .+1<CR>==
nnoremap º :m .-2<CR>==
inoremap ª <Esc>:m .+1<CR>==gi
inoremap º <Esc>:m .-2<CR>==gi
vnoremap ª :m '>+1<CR>gv=gv
vnoremap º :m '<-2<CR>gv=gv
as explained here.
Hope it's useful, enjoy Vim :)
ADDENDUM BY Dylan_Larkin (2019): For this to work on a Mac, "Use Option as Meta Key" must be turned OFF in Terminal->Preferences->Keyboard
UPDATE 09/2021
I recently switched from a "British" keyboard to "ABC - Extended" and noticed this configuration doesn't work as expected.
As an alternative, I mapped the <up> and <down> keys to do the same operation (which, I guess, also solves most of the complexity explained in other answers of this very question):
nnoremap <down> :m .+1<CR>==
nnoremap <up> :m .-2<CR>==
inoremap <down> <Esc>:m .+1<CR>==gi
inoremap <up> <Esc>:m .-2<CR>==gi
vnoremap <down> :m '>+1<CR>gv=gv
vnoremap <up> :m '<-2<CR>gv=gv
This is also a great way for beginners to rewire the habit of using the arrows and instead learn the much more efficient Vim motion way to move around the code. ;)
You can complete your transition mapping <left> and <right> to quickly move between tabs with:
nnoremap <left> gT
nnoremap <right> gt
Or whatever you fancy (even a brutal <NOP>, like I did at the beginning of my journey).
:help key-notation describes what format needs to be used to map different keys. In the case of alt, you can use either <A- or <M-. So your mapping would be
map <M-d> <C-d>
I'd also recommend using the nore variant of :map (e.g., noremap) unless you explicitly want to allow the right-hand side to be re-evaluated for mappings.
I'm not sure is "possible" anymore. Please read the update below.
Yes, you can even in terminal vim, but there's no real catch all answer. You basically have to follow two steps:
Make sure the <M-d> notation exists, and map exactly what your terminal inputs (^[ is the escape character):
$ cat
^[d
$
" in your .vimrc
execute "set <M-d>=\ed"
" you have to use double quotes!
Map something to your newly "created" combination:
noremap <M-d> :echo "m-d works!"<cr>
Understanding how it works, you can expand this "trick" to other "strange" combinations, for instance, I'm using termite, and vim doesn't recognize <S-F1>, using cat I get ^[[1;2P. Then, in my vimrc I do: execute "set <S-F1>=\e[1;2P", and then I can map it to anything.
Note: I don't know why, but for some people using \<Esc> works instead of \e.
Update (february 2016)
Depending on the terminfo your terminal runs, maybe you could... in most terminals, "alt + h", for example, is mapped to ^[h, which is: "escape + h". So it could overwrite keys. I've just tried (again) and it seems to work, but I believe it's a very buggy and error prone implementation.
Nevertheless, for the brave enough, here's an experimental plugin:
https://github.com/vim-utils/vim-alt-mappings
https://github.com/drmikehenry/vim-fixkey
Map Alt Key in Vim on Mac OSx:
Start by viewing the key code your terminal is sending to vim:
$ sed -n l
^[[1;9D
In the above example, I ran the command and pressed Alt + Left.
The ^[[1;9D is the escaped sequence being sent to vim, so we can user that for our mapping.
map <Esc>[1;9D
Use:
map <A-D> <C-D>
See :help key-notation.
My Terminal would produce ^[x commands (e.g. for alt-x). What got it to work inside Vim was this small script from vim.wikia.com:
for i in range(97,122)
let c = nr2char(i)
exec "map \e".c." <M-".c.">"
exec "map! \e".c." <M-".c.">"
endfor
Add to .vimrc to fix all alt key mappings.
as a follow up to Bruno's answer for Mac users, try making sure your option key is mapped to Esc+.
This will give you the "normal" behavior of the option (A) key in Vim.
For example, in iterm2, this option can be found under Preferences > Profiles > Keys:
Your terminal might not transmit "properly" the Alt-D. You can use C-V to actually get the actual escape sequence send to Vim and use it to create your mapping. Ie, edit your .vimrc
and replace the actual by typing the following sequence "C-V Alt-D" so you'll have the correct escape sequence in your vimrc. That won't work if your terminal doesn't send anything to vim.
Find out key mapping by putting following command in your vim editor
:help key-notation
It will display all the key mapping.
In my ubuntu system for Alt it is <M-...>. It is possible for your version mapping might be different. If you too have same mapping then following should work.
map <M-D> <C-D>
Hello after no good solution after years of testing all the above on mac, I kept searching.
Here is my solution:
To create a combination keystroke including Alt you have to declare the combination in the preference > keyboard and use that combination in the vim setup file (check use option as meta key).
The output must be an unusual character (no a for example) so that you're not overriding a regular character.
In the example below you should be able to quite vim with ALT-Up.
vim setting:
mac setting:
Like control key is represented by a '^' in the terminal, what is the equivalent for the command key (mac)?
I am trying to remap my bash shortcuts using stty
For eg
stty eof ^D
But instead of control, I want to use the command key.
EDIT:
Okay so the issue I was trying to solve was that I wanted to interchange command and control keys because I work on osx and linux and the different key combinations cause me a lot of pain.
So I interchanged the modifier keys using osx preferences. But now all the bash shortcuts like Ctrl+C etc had become equivalent of using the key sequences 'cmd+c' - which is not acceptable.
Thankfully iTerm2, supports remapping of modifier keys as well, so for iterm2 I reversed them again which means iTerm2 recognizes command as command and control as control.
So problem solved for now.
The command-key shortcuts do not generate actual input for your terminal, so they are not represented in any way. Terminal allows you to bind certain key combinations to produce actual input (in Preferences > Settings > Keybaord), but you don't get the choice of a Command modifier for them.
Type this in your bash shell :
stty ctlecho
then hit Command
That will display what you need.
To go back to normal
stty -ctlecho
If it doesn't work, try a combo.
Example with Ctrl+C
$ stty ctlecho
$ ^C
$ stty -ctlecho
$
I am new to OSX and have downloaded iTerm 2 and my name for the command line is superfluously long.
It looks like my-name-Macbook-pro:~ myname$
and I want it to look like ~myname$
Is there an easy way to shorten this name, I have googled as much as I can but can't find an easy solution.
This has nothing to do with OSX or with iterm2. OSX by default uses the bash shell and you just need to set the prompt correctly.
The prompt is set by the variable PS1. See this article on how to set your bash prompt. To achieve what you want, you need the \w and the \u fields. So something like
PS1=\\w:\\u\$
will give you ~:myname$. Play around with the other options and see what you like best. Once you find a setting that you like, enter it in your .bashrc file so that it is loaded every time you login. Also see this question for cool stuff to put in your .bashrc.