I am trying to customize the behaviour of Enter key in Normal mode in Vim.
Here is my .vimrc:
nmap <CR> o<Esc>
nmap <S-CR> i<CR><Esc>
I am trying to make Enter simply append a newline after the current line when pressed. However, when the Shift-Enter combination is pressed, I want to break the current line at the cursor.
The latter does not work. Whenever I press Shift-Enter it just appends a line without breaking it at the cursor.
I managed to correct my terminal key-code for Shift+Enter
by sending the key-code Vim apparently expects. Depending on your terminal,
(Adding Ctrl+Enter as a bonus!)
iTerm2
For a single Profile open Preferences → Profiles → Keys → [+] (Add)
For all profiles open Preferences →
Keys → [+] (Add)
Keyboard shortcut: (Hit Shift+Enter)
Action: Send Escape Sequence
Esc+ [13;2u
Repeat for Ctrl+Enter, with sequence: [13;5u
urxvt, append to your .Xresources file:
URxvt.keysym.S-Return: \033[13;2u
URxvt.keysym.C-Return: \033[13;5u
Alacritty, under key_bindings, add following to your ~/.config/alacritty/alacritty.yml:
- { key: Return, mods: Shift, chars: "\x1b[13;2u" }
- { key: Return, mods: Control, chars: "\x1b[13;5u" }
Kitty, in ~/.config/kitty/kitty.conf:
map shift+enter send_text all \x1b[13;2u
map ctrl+enter send_text all \x1b[13;5u
Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today in the terminal version of Vim (<S-CR> should work in GVIM on all platforms, and in the Windows console Vim). This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.
Note on mapping <CR>:
If you map <CR> in normal mode, it'll interfere with selection of history items in the command-line window and with jumping to error under cursor in quickfix/location list windows! (Unless you add the following:)
:autocmd CmdwinEnter * nnoremap <CR> <CR>
:autocmd BufReadPost quickfix nnoremap <CR> <CR>
Note on :nmap:
You should use :noremap; it makes the mapping immune to remapping and recursion.
I also wanted to map <S-CR> and found that I couldn't get it to work in CLI mode until I added a second mapping using Ctrl+V then <Shift+Enter> for the mapped keystroke. The <S-CR> mapping is still needed for GVIm to work as expected, however. This would render the your .vimrc snippet as follows:
nnoremap <CR> o<Esc>
nnoremap <S-CR> i<CR><Esc> " Needed for GVIm
nnoremap ^[0M i<CR><Esc> " Needed for CLI VIm (Note: ^[0M was created with Ctrl+V Shift+Enter, don't type it directly)
I tested this on Ubuntu 12.04. Happy Vimming!
You can't map <S-CR> in CLI Vim, no matter how hard you try, because Vim can't distinguish <S-CR> from <CR>.
You must find another mapping or stick with GVim/MacVim.
edit
Some terminal emulators, like iTerm.app or Terminal.app on Mac OS X, allow you to set up shortcuts to send specific characters sequences to the shell. If you have that possibility it may be worth a try but you'll quickly get used to a platform-specific gyzmo that can't be ported so, well… I don't really recommend it.
Ingo Karkat and romainl are 100% correct. However what you are asking is common so I want to give you some options.
I personally recommend using Tim Pope's Unimpaired plugin. It provides many mappings but the ones you will looking for are [<space> and ]<space> which create blank lines above and below the current line respectively. Unimpaired also provides nice mappings for moving through the quickfix list, buffer list, option toggling, and many others. See :h unimpaired for more.
If you do not want to use unimpaired plugin but like the mappings below are some quick mappings to put in your ~/.vimrc file:
nnoremap <silent> [<space> :<c-u>put!=repeat([''],v:count)<bar>']+1<cr>
nnoremap <silent> ]<space> :<c-u>put =repeat([''],v:count)<bar>'[-1<cr>
Related
When typing <Enter> in normal mode in Vim with the Tagbar plugin installed, the Tagbar window is opened automatically. I want to disable this functionality. What must I do?
put the flowing code in you .vimrc
unmap <cr>
Your mapping for <C-m> is actually the cause of the Enter key opening Tagbar. If you remove that map from your vimrc, the enter key will no longer trigger :TagbarToggle.
Mappings for <C-m> and <CR> (Enter) are synonymous in Vim:
The following table shows the mapping between some of the keys on the keyboard and the equivalent Ctrl-key combination:
Ctrl-I Tab
Ctrl-[ Esc
Ctrl-M Enter
Ctrl-H Backspace
If you use one of the Ctrl-key combination in the above table in a map, the map also applies to the corresponding key. Both the keys produce the same key scan code. For example, if you create a map for CTRL-I, then you can invoke the map by pressing Ctrl-I or the Tab key.
This means when you set nmap <C-m> :TagbarToggle<CR>, it is the same as
also setting nmap <CR> :TagbarToggle<CR>.
You'll probably want to choose a new key instead of M. The alternative is to
change the key code sent by <C-m> at the operating system level with some
remapping program.
The terminal key bindings come from readline, the program that processes input text in
your terminal prompt. A full list of key bindings is in the readline
manual.
You can find more info about Vim key codes by typing :help keycodes in Vim, or reading the help docs here.
Try :help tagbar to open the documentation. It sounds like you might have a mapping in your vimrc file that says something like
nnoremap <silent> <CR> :TagbarToggle<CR>
or
nnoremap <silent> <CR> :TagbarOpen<CR>
if you find and remove that mapping will no longer open Tagbar
I have added set term=xterm to my vimrc to be able to use 256-color vim schemes in terminal, but it comes at a price (at least for me). It also replaces (sort of) the BackSpace with Delete (i.e. BackSpace starts to delete forward just like Delete does) in insert mode and makes it "BackSpace" instead of moving left (or h) in normal mode. I have nothing against Ctrl-H as a way "to Backspace", but I simply don't want to have two delete buttons and ability "to BackSpace" (delete backward) in normal mode.
How can I reverse that change while retaining the setting I need?
PS I've read :h CTRL-h and a bit of :h xterm, but I couldn't find the solution.
Vim's inoremap and nnoremap commands can be used to adjust how keys are interpreted in Vim.
A solution is documented here for your specific context: https://conemu.github.io/en/VimXterm.html
The relevant quote:
"If you have problems with BS in Vim (BS acts like Delete key) under ConEmu when term=xterm, you may try to remap BS key:
inoremap <Char-0x07F> <BS>
nnoremap <Char-0x07F> <BS>
"
In general, when a key does not do what you want, the trick is to find out what it actually sends to Vim. Sometimes hitting Ctrl-V followed by that key in insert mode might help figure it out. Then inoremap and nnoremap as shown above can be used to reassign it to the behaviour you want in insert and normal modes, respectively.
NOTE: I am using Terminator instead of terminal. But as all other mappings are working fine why does these are not working.
I have tried to use these mappings in my vimrc file to be able to use Tab Navigation. But its not working at all.
nnoremap <C-S-tab> :tabprevious<CR>
"nnoremap <C-tab> :tabnext<CR>
nnoremap <C-tab> :tabn<CR> "I also tried this
nnoremap <C-t> :tabnew<CR>
inoremap <C-S-tab> <Esc>:tabprevious<CR>i
inoremap <C-tab> <Esc>:tabnext<CR>i
inoremap <C-t> <Esc>:tabnew<CR>
inoremap <C-S-w> <Esc>:tabclose<CR>
"Also to go to the nth tabpage Use <A-Fn>
nnoremap <A-F1> 1gt
nnoremap <A-F2> 2gt
nnoremap <A-F3> 3gt
nnoremap <A-F4> 4gt
nnoremap <A-F5> 5gt
nnoremap <A-F6> 6gt
nnoremap <A-F7> 7gt
nnoremap <A-F8> 8gt
nnoremap <A-F9> 9gt
nnoremap <A-F10> 10gt
NOTE: I have ctags and cscope installed. So I think there might be some confliction as ctrl-t is to jump back from a certain tag. And only this mapping is working for new tab.
Also I have checked ctrl-PageDown is working fine for the same purpose.
2nd Question:
How does this key notation works in vimrc.
Is it something like this:
All modifier keys should be used in Caps like
C for Ctrl.
A for Alt.
S for Shift.
And other keys are all in small.
But what about keys like:
Home
End
Backspace
Escape
PageUp
PageDown
Tab
Function keys etc.
How to use them?
Here
I read that how these should be used in mapping but even they have used tab instead of Tab in mappings.
Brace yourself for disappointment.
The terminal keycodes
Vim accepts terminal keycodes, so not all key combinations are possible. The best way to figure out which keys are recognizable is to open insert mode and press ctrl+v followed by your key combination. This will show you the raw codes. Do this for another key combination. If the raw codes are the same then Vim can not distinguish between them. e.g. ctrl+v ctrl+shift+tab.
Your mappings
You should probably avoid doing insert mode mappings to switch tabs. It simply isn't the Vim Way as insert mode should only be used in short bursts.
Your :tabprev and :tabnext mappings can simplified into gT and gt mappings. Personally I do not mind the default gt or gT mappings.
<key> notation
As far as I know the case does not matter. All my mappings are lowercase. For a list of <> notation please see :h key-notation.
You are correct on the <c-..> for control, <a-...> for alt, and <s-...> for shift. Example combination would be <c-s-space>. Note: most <c-s-...> mappings are going to fail.
Using Tabs
Vim's tabs are not like most text editors tab. They are more like viewports into a group of windows/splits. Additionally, Vim is buffer centric, not tab centric like most editors. For example using features like Vim's quickfix list is often easier without tabs (See :h 'switchbuf if you must use tabs). Vim's tabs often get in the way of using a splits as there are better window and buffer navigation commands available. I personally have many files open (sometimes 100+) using no tabs and use on average 1-2 splits without any issue.
Bottom line: Learn to use buffers effectively.
Conclusion
I would suggest you break this tab workflow quickly and learn to love buffers. You won't really need your mappings and you will not be working against Vim's nature.
Read :help key-notation for an explanation of… Vim's key notation.
It is generally a good idea to play it safe so I recommend to follow these conventions when mapping combos:
always use an uppercase letter for the modifier key, C for Control, S for Shift, A for Alt, D for Command (MacVim GUI only), M for Meta,
always use a lowercase letter for the alphabetical keys, abc…xyz,
always capitalize the first letter of "special" keys, Tab, Space, Up, etc.
Examples:
<S-Up>
<C-r>
<A-LeftMouse>
However, the following notation works just as well so… whatever notation you choose, try to be consistent:
<s-UP>
Using multiple modifiers in a single mapping doesn't work reliably so you will be better in the long run if you completely avoid them.
nnoremap <C-S-j> :echo "csj"<CR>
nnoremap <C-j> :echo "cj"<CR>
now press <C-j> and <C-S-j> in normal mode.
<C-S-w> is indistinguishable from <C-w>.
:verbose map <C-t>
shows you what is mapped to <C-t> and where the mapping occurred. You can use it to debug your mappings.
And I agree with Peter, you are using both tab pages and insert mode wrongly.
I am trying to customize the behaviour of Enter key in Normal mode in Vim.
Here is my .vimrc:
nmap <CR> o<Esc>
nmap <S-CR> i<CR><Esc>
I am trying to make Enter simply append a newline after the current line when pressed. However, when the Shift-Enter combination is pressed, I want to break the current line at the cursor.
The latter does not work. Whenever I press Shift-Enter it just appends a line without breaking it at the cursor.
I managed to correct my terminal key-code for Shift+Enter
by sending the key-code Vim apparently expects. Depending on your terminal,
(Adding Ctrl+Enter as a bonus!)
iTerm2
For a single Profile open Preferences → Profiles → Keys → [+] (Add)
For all profiles open Preferences →
Keys → [+] (Add)
Keyboard shortcut: (Hit Shift+Enter)
Action: Send Escape Sequence
Esc+ [13;2u
Repeat for Ctrl+Enter, with sequence: [13;5u
urxvt, append to your .Xresources file:
URxvt.keysym.S-Return: \033[13;2u
URxvt.keysym.C-Return: \033[13;5u
Alacritty, under key_bindings, add following to your ~/.config/alacritty/alacritty.yml:
- { key: Return, mods: Shift, chars: "\x1b[13;2u" }
- { key: Return, mods: Control, chars: "\x1b[13;5u" }
Kitty, in ~/.config/kitty/kitty.conf:
map shift+enter send_text all \x1b[13;2u
map ctrl+enter send_text all \x1b[13;5u
Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today in the terminal version of Vim (<S-CR> should work in GVIM on all platforms, and in the Windows console Vim). This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.
Note on mapping <CR>:
If you map <CR> in normal mode, it'll interfere with selection of history items in the command-line window and with jumping to error under cursor in quickfix/location list windows! (Unless you add the following:)
:autocmd CmdwinEnter * nnoremap <CR> <CR>
:autocmd BufReadPost quickfix nnoremap <CR> <CR>
Note on :nmap:
You should use :noremap; it makes the mapping immune to remapping and recursion.
I also wanted to map <S-CR> and found that I couldn't get it to work in CLI mode until I added a second mapping using Ctrl+V then <Shift+Enter> for the mapped keystroke. The <S-CR> mapping is still needed for GVIm to work as expected, however. This would render the your .vimrc snippet as follows:
nnoremap <CR> o<Esc>
nnoremap <S-CR> i<CR><Esc> " Needed for GVIm
nnoremap ^[0M i<CR><Esc> " Needed for CLI VIm (Note: ^[0M was created with Ctrl+V Shift+Enter, don't type it directly)
I tested this on Ubuntu 12.04. Happy Vimming!
You can't map <S-CR> in CLI Vim, no matter how hard you try, because Vim can't distinguish <S-CR> from <CR>.
You must find another mapping or stick with GVim/MacVim.
edit
Some terminal emulators, like iTerm.app or Terminal.app on Mac OS X, allow you to set up shortcuts to send specific characters sequences to the shell. If you have that possibility it may be worth a try but you'll quickly get used to a platform-specific gyzmo that can't be ported so, well… I don't really recommend it.
Ingo Karkat and romainl are 100% correct. However what you are asking is common so I want to give you some options.
I personally recommend using Tim Pope's Unimpaired plugin. It provides many mappings but the ones you will looking for are [<space> and ]<space> which create blank lines above and below the current line respectively. Unimpaired also provides nice mappings for moving through the quickfix list, buffer list, option toggling, and many others. See :h unimpaired for more.
If you do not want to use unimpaired plugin but like the mappings below are some quick mappings to put in your ~/.vimrc file:
nnoremap <silent> [<space> :<c-u>put!=repeat([''],v:count)<bar>']+1<cr>
nnoremap <silent> ]<space> :<c-u>put =repeat([''],v:count)<bar>'[-1<cr>
I've tried MiniBufExplorer, but I usually end up with several windows showing it or close it altogether. What I'd like is something like LustyJuggler with incremental search, the way I switch between buffers in Emacs. Surely there is a script like this?
I used to use a combination of tabs and multiple gvim instances, keeping groups of related files as tabs in each instance. So long as I didn't end up with too many tabs in one instance, the tab bar shows you the name of each file you're editing at a glance.
Then I read a post by Jamis Buck on how he switched from TextMate back to vim, and learned some great tricks:
Ctrl+w s and Ctrl+w v to split the current window
Ctrl+6 to switch back and forth between two buffers in the same window.
the awesome fuzzyfinder.vim which gives you auto-completing search of files in your current directory or of buffers you currently have open
Jamis' own fuzzy_file_finder and fuzzyfinder_textmate, which slightly modify how fuzzyfinder works to behave more like a similar feature in TextMate (as far as I can tell, the difference is that it matches anywhere in the filename instead of only from the start). Watch this video to see it in action.
Now I just have one gvim instance, maximised, and split it into multiple windows so I can see several files at once. I bound Ctrl+F to fuzzyfinder\_textmate, so now if I type (say) Ctrl+F mod/usob it opens up app/models/user\_observer.rb. I almost never bother with tabs any more.
Update 2010/08/07
While fuzzyfinder\_textmate remains awesome, as Casey points out in the comments, it's no longer maintained. Also, it (and/or fuzzyfinder.vim) gets a bit slow and unstable when working with large projects (lots of directories or files), so I've been looking for an alternative.
Fortunately, there seems to be a very nice alternative in the form of Wincent Colaiuta's Command-T plugin. This has very similar (if not slightly better) behaviour to fuzzyfinder\_textmate, but is noticeably faster; it also has nice features like being able to open the found file in a split or vertical split. Thanks (and upvotes!) to David Rivers for pointing to it.
I use the basics - ':ls' + ':bn'/':bp' + ':b <part-of-name>'
I like "ctrl-w s" and "ctlr-w v" to split the window. Then I map the movement keys (h, j, k, l) with ctrl held down to move between the split windows:
" Map ctrl-movement keys to window switching
map <C-k> <C-w><Up>
map <C-j> <C-w><Down>
map <C-l> <C-w><Right>
map <C-h> <C-w><Left>
Having to move my hand over to the arrow keys is annoying.
Next, I set up ctlr-tab to switch between buffers in the current window (like a lot of other environments):
" Switch to alternate file
map <C-Tab> :bnext<cr>
map <C-S-Tab> :bprevious<cr>
These have worked pretty well for me over the last several years although vim always has more secrets than you can know.
I have been using Wincent Colaiuta's Command-T vim plugin for a couple months now. Wincent wrote the parts of it that need to be fast in C, and I must say that it is! And, I think its file pattern matching logic is even better than Textmate's Command-T. Check out the screencast.
The Command-T plug-in for VIM provides
an extremely fast, intuitive mechanism
for opening files with a minimal
number of keystrokes. It's named
"Command-T" because it is inspired by
the "Go to File" window bound to
Command-T in TextMate.
Files are selected by typing
characters that appear in their paths,
and are ordered by an algorithm which
knows that characters that appear in
certain locations (for example,
immediately after a path separator)
should be given more weight.
Easier buffer switching contains many useful tips. I have adapted the following to my .vimrc, which does buffer-name auto-completion, maps the most useful buffer-switching commands to my <Leader> and left-side home row keys, and shows the current buffer number in the status line:
"" Tab triggers buffer-name auto-completion
set wildchar=<Tab> wildmenu wildmode=full
let mapleader = ","
map <Leader>t :CommandT<Return>
map <Leader>a :bprev<Return>
map <Leader>s :bnext<Return>
map <Leader>d :bd<Return>
map <Leader>f :b
"" Show the buffer number in the status line.
set laststatus=2 statusline=%02n:%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P
I also use MiniBufExplorer, which provides a compact listing of each listed buffer in its own horizontal split up top.
nmap <Leader>bb :ls<CR>:buffer<Space>
and nobody mentioned nice plugin ctrlp.vim. Using this plugin you can search buffer by name.
I use
CTRL-J for next buffer
CTRL-K for previous buffer
CTRL-L for next tab
CTRL-H for previous tab
Here is the .vimrc configuration:
map <C-J> :bnext<CR>
map <C-K> :bprev<CR>
map <C-L> :tabn<CR>
map <C-H> :tabp<CR>
See http://syskall.com/my-biggest-vim-productivity-boost/
imap <A-1> <Esc>:tabn 1<CR>i
imap <A-2> <Esc>:tabn 2<CR>i
imap <A-3> <Esc>:tabn 3<CR>i
imap <A-4> <Esc>:tabn 4<CR>i
imap <A-5> <Esc>:tabn 5<CR>i
imap <A-6> <Esc>:tabn 6<CR>i
imap <A-7> <Esc>:tabn 7<CR>i
imap <A-8> <Esc>:tabn 8<CR>i
imap <A-9> <Esc>:tabn 9<CR>i
map <A-1> :tabn 1<CR>
map <A-2> :tabn 2<CR>
map <A-3> :tabn 3<CR>
map <A-4> :tabn 4<CR>
map <A-5> :tabn 5<CR>
map <A-6> :tabn 6<CR>
map <A-7> :tabn 7<CR>
map <A-8> :tabn 8<CR>
map <A-9> :tabn 9<CR>
I've recently gone more minimalistic.
To cycle buffers I use ]b and [b from unimpaired: https://github.com/tpope/vim-unimpaired
To jump straight to an open buffer just use Vim's tab completion with :b. A few letters is enough to get to any open buffer with a tab or two.
Similarly to open buffers I use :e with relative paths and tab complete.
I also use :ls occasionally to see what buffers I have open (and to check their modified status).
To get rid of a buffer I use :bw to wipe the buffer. I usually make a temporary split and change buffers to preserve my layout though since :bw also closes the active window.
All the minibuf things I tried just ended up annoying me, and I don't want some smart-matching thing opening random files for me. If I really need to browse for something I use NERDtree (:e .).
IDK, Lately I also dropped Yankring (because it screws up xp) and started using registers, and I recently decided the f/t movements are the greatest thing ever...
To list and switch between buffers I use:
nnoremap <Leader>l :ls<CR>:b<space>
To switch between buffers:
map <Leader>n :bn<CR>
map <Leader>p :bp<CR>
The excellent Buffer Explorer, the be has gotten to be such strong muscle memory that I find myself wishing I could use it in other applications. I find it to be extremely fast when actively editing more than two files.
I've spent quite a while building my .vimrc to work with this HTML::Mason project I've been on for four years, so I have an odd mix of tabs and split windows. For your viewing enjoyment:
map ;o :Sex <CR>
map <C-J> <C-W>j
map <C-K> <C-W>k
map <C-l> <C-W>l
map <C-h> <C-W>h
map ;] :tabnext<CR>
map ;[ :tabprev<CR>
map <C-t> :tabe +"browse ."<CR>
map <C-O> :NERDTreeToggle ~/curr/trunk/<CR>
I use tselectbuffer. It's really fast and unlike bufexplorer doesn't take space in your window. It also has a incremental search.I tried minibufexplorer and I found the navigation in the buffer a bit difficult.
I have mapped <S-J> and <S-K> to :bp and :bn, although I admit I don't use it as the number of files is greater than 10. I have then mapped <C-J> and <C-K> to Gnome Terminal's previous and next tabs, and I usually run 4 instances of vim for each of the 4 different projects I work on. I still really wish next and previous buffer would go to the history of buffers I have been working on, and not the order int he buffer list.
I use tselectbuffer. It's really fast and unlike bufexplorer doesn't take space in your window. It also has a incremental search.I tried minibufexplorer and I found the navigation in the buffer a bit difficult.
i use simple :vsplit with ^W+w/^W+r and :tabnew with Ctrl+Alt+PgUp/PgDown key combinations.
When there are several buffers open in a Vim session, it can become difficult to keep track of the buffers and their respective buffer numbers. If this is the case, switching to a different file can be made easier using a simple map:
:nnoremap (F5) :buffers(CR):buffer(Space)
vim.wikia.com