I found this code the forum to make my life easier when switching between tabs and I placed it in my ~/.vimrc. Happily, it works well. Unfortunately, whenever I ctrl+tab to change tab, the cursor moves back one character. Can anyone help me keep the cursor in one place?
set showtabline=2
:nmap <C-S-tab> :tabprevious<cr>
:nmap <C-tab> :tabnext<cr>
:nmap <C-t> :tabnew<cr>
:map <C-t> :tabnew<cr>
:map <C-S-tab> :tabprevious<cr>
:map <C-tab> :tabnext<cr>
:map <C-w> :tabclose<cr>
:imap <C-S-tab> <ESC>:tabprevious<cr>i
:imap <C-tab> <ESC>:tabnext<cr>i
:imap <C-t> <ESC>:tabnew<cr>
The cursor moves back one character because that's how i works. Technically, you can change
:imap <C-tab> <ESC>:tabnext<cr>i
to
:imap <C-tab> <ESC>:tabnext<cr>li
but staying in insert mode is not a very good idea. If you can't help it, I'd suggest you remove the three imap from your ~/.vimrc to force you to use Vim correctly.
By the way…
You don't need that leading : in your ~/.vimrc:
map <C-t> :tabnew<cr>
You don't really need those :map because you already have nmap (and imap, but it should go).
<C-t> is very useful, both in insert mode (indents the current line) and in normal mode (goes back in the tagstack). You shouldn't override it like that.
<C-S-Tab> will probabbly not work in a terminal.
Tabs are not designed as 1-to-1 proxies for files and thus should not be used as such. You should use buffers instead.
Everything romainl said is good advice, in particular that tabs are not supposed to work as one-to-one proxies for files, like in other editors. Learn to use buffers, and your life will be easier. They're really very simple: all you really need is :set hidden, :ls, :bn, :bp, maybe some mappings like the ones you're setting up for tabs, and a bit of the Vim help.
That said, romainl's mapping will fail at the end of a line. A better solution uses <C-o>, which takes you out of insert mode temporarily for one command, and takes into account stuff like end-of-line behavior. You could use:
:imap <C-tab> <C-o>:tabnext<cr>
Related
I currently map my F2 and F3 as following:
map <F2> :tabn <CR>
map <F3> :tabp <CR>
imap <Esc> :tabn <CR>
imap <Esc> :tabp <CR>
I try to figure out how to map the normal/insert mode at the same time.
I spent some times on googling around without any luck.
Any suggestion would be appreciated.
The best I've found so far is:
nnoremap <F2> :tabn<CR>
imap <F2> <C-O><F2>
nnoremap <F3> :tabp<CR>
imap <F3> <C-O><F3>
Still an extra line for each key, but at least the actual command (:tabn, :tabp) is only mentioned once (and only needs to be changed in one place if you want to change it).
Honestly in my opinion, these mappings are probably not worth it.
gt/gT already exist to move between tabs. See :h gt
Normal mode is crucial to Vim and it makes sense to use this mode and not insert mode for movement between tabs. It is called normal mode for a reason as it is the mode you should normally be in.
Aside about tabs and buffers
Your mappings suggest a heavy tab centric workflow. I know it might sound weird but maybe try and use less tab panes together with a more buffers centric workflow. Here are some nice posts about it:
Why do Vim experts prefer buffers over tabs?
Use buffers effectively!
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.
When I make a .vimrc entry,
nnoremap <silent> <leader>c :colorscheme <tab>
The tab is applied if I understand the terminalogy, as a literal, that is, upon typing ,c, I get in Vim command line,
:colorscheme ^I
I tried to internet the search terms, but mostly I get results about remapping Vim Tabs; the closest I found was somebody putting quotes around their <tab>, but I think that is for a different desired outcome.
I also have this, which is why I want the tab in my shortcut,
set wildmenu
set wildmode=longest:list,full
You'll need the 'wildcharm' option:
set wildcharm=<C-z>
nnoremap <silent> <leader>c :colorscheme <C-z>
See :help 'wildcharm'.
As a side note, I use that option with great effect for switching buffers:
nnoremap <leader>b :buffer <C-z><S-Tab>
and a file-opening variant would be just as easy and just as useful:
nnoremap <leader>e :edit <C-z><S-Tab>
I'm used to using cmd-option + arrows to switch between tabs in Chrome. I'd like to do the same in Vim without having to learn vimscript right way. Any suggestions?
Something like
:nnoremap <D-A-Down> <C-PageDown>
:nnoremap <D-A-Up> <C-PageUp>
should do it. The <D-> modifier is Mac-specific. For other options, see
:help keycodes
You can use some alternative mappings. The terminal may not play nice with some of these because <Tab> is a control key already.
Something like:
nnoremap <C-t> :tabe<CR>
nnoremap <C-tab> :tabn<CR>
nnoremap <C-S-tab> :tabp<CR>
This will not necessarily work depending on your terminal and terminal settings, so you would just have to learn to use other navigation keys.
I want to remap <PageUp> to <C-u> and PageDown to <C-d> per the Vim scrolling documentation.
As it stands right now, my /etc/vim/vimrc looks like this:
nnoremap <PageUp> <C-u>
nnoremap <PageDown> <C-d>
I've tried a lot of different combinations and nothing I've done has worked.
My goal is to make the cursor move to the Start Of File or EOF when holding down PageUp/PageDown. As it is right now, the cursor stops before it gets all the way to the top (and PageDown scrolls past the EOF). Just annoyances I'm trying to fix.
EDIT: The above settings work fine. I was placing my mappings too early in the file.
What about the following mappings?
nnoremap <PageUp> gg
nnoremap <PageDown> G
Or simply using gg and G?
Instead of placing the mappings into the system-wide /etc/vim/vimrc, you should put user customizations into the ~/.vimrc file. Nonetheless, the global configuration (if that's what you want) should work, too. That it doesn't means that the mappings get cleared or redefined. You can check with
:verbose nmap <PageDown>
If it didn't get redefined, you have to hunt for :nunmap commands in all loaded scripts (:scriptnames), or capture a log with vim -V20vimlog.
You can do this with
map <silent> <PageUp> 1000<C-U>
map <silent> <PageDown> 1000<C-D>
imap <silent> <PageUp> <C-O>1000<C-U>
imap <silent> <PageDown> <C-O>1000<C-D>
from fixing-pageup-and-pagedown