Ctrl + w is a shortcut to closing an open window on VM instance of Google Compute Engine. Hence it will ask to perform that action before letting me type one more w, to enact switching of tabs between the directory tree and the script to work on in vim.
I've tried the following, as mentioned here
map <C-l> :tabn<CR>
map <C-h> :tabp<CR>
map <C-n> :tabnew<CR>
While I'm not sure what tabn and tabp indicate, I tried the first two line and (C as Ctrl) neither of these respond to anything new. I used source ~/.vimrc command before expecting a change is reflected. What is wrong here?
I think you're mixing up Vim tabs and Vim windows.
Vim windows will split the screen vertically or horizontally into separate panes, which are all visible at the same time. That's typically used by directory tree plug-ins which want to display a navigator on a sidebar.
Vim tabs group a set of windows, so that you can switch between whole sets of windows at once and still easily go back to where you were before. (People often use tabs to work on different projects and switch between them, though opening one file per tab, fullscreen, is also a somewhat common workflow.)
The mappings that use Control-W are window mappings, not tab mappings. (You can switch to next tab with gt and previous tab with gT).
The normal commands to cycle windows are Ctrl-W w (to move right/down) and Ctrl-W W (to move left/up), so you can use these two mappings:
nnoremap <C-l> <C-w>w
nnoremap <C-h> <C-w>W
If you want a mapping to open a new window with a new blank file, you can use:
nnoremap <C-n> <C-w>n
If your problem is with typing Control-W in specific, perhaps a better option is to map a key sequence you're not using to replace Control-W, but keep it to just the prefix, so that all other commands that follow are still available?
Perhaps use Control-Q, which is just next to W in the keyboard:
nmap <C-Q> <C-W>
And you might want to remap the commands that use the same key twice, so in your case use Q twice where there's two W's:
nmap <C-Q> <C-W>
nnoremap <C-Q>q <C-W>w
nnoremap <C-Q>Q <C-W>W
nnoremap <C-Q><C-Q> <C-W><C-W>
Those four there, that would be my recommendation, if Control-W is an inconvenient sequence for you.
I have a NERDTree setup nicely on my Rails project:
Sometimes, when I do fuzzy finding (Ctrl + P), I would accidentally still be on left tab; when I went to the file, it would replace the nerdtree's left tab. See screenshot below:
Super simple question, but how can I bring up the Nerdtree display back up like the first screnshot?
You should just be able to run :NERDTree again.
Additionally, I have this setting in my .vimrc, to make it less likely I'll delete the NERDTree buffer by accident with I'm compulsively typing :bd
autocmd FileType nerdtree cnoreabbrev <buffer> bd :echo "No you don't"<cr>
Deleting the buffer will permanently put NERDTree in hell (Vim plugins are usually brittle). If you do that, you have to restart Vim.
Also, if you use tabs in Vim, you should get NERDTreeTabs (dead but it works) which keeps NERDTree open / closed / in the same state across all tabs you have open, which is a standard design practice in all editors except Vim. I have a mapping set up to toggle NERDTree:
nnoremap <Leader>nt :NERDTreeTabsToggle<cr>
steal from my my vimrc!
nnoremap <F2> :NERDTreeToggle<CR>
Press F2 twice. The first one will close the NERDTree panel and second one will bring it up again.
F2 can be any key of your choice.
I am using vimdiff for the first time. Online I found written that to move from the left pane you use CTRL + w + Left or right arrow
This does not work for me. But I see that if I press just CTRL + w and press w for a sec and let it go, it switches pane after ~500ms.
Is this how it is supposed to work? Am I doing something wrong?
Ctrl+w and right and left arrow can be used to move between any split windows on vim, not only vimdiff splits.
These keys do work here on cygwin; also, Ctrl+w w also moves to the next window, but without the delay you mentioned.
It is possible that you have mapped these keys in your .vimrc or via some vim plugin. You can check this with :map w, :map <left> and :map <right>.
As moving between windows is something that you use often, you may consider using the following mappings:
nnoremap <C-J> <C-W>j
nnoremap <C-K> <C-W>k
nnoremap <C-H> <C-W>h
nnoremap <C-L> <C-W>l
Then you can use Ctrl+h and Ctrl+l to move left and right, without moving your hands from the home row. And the nnoremap will ensure that these works despite of any other mappings that you may have.
Press Ctrl + W and then (after releasing Ctrl + W) press the arrow keys to change the pane.
It is very useful to use set mouse=a in your .vimrc file. It gives you possibility to switch between windows using mouse. Additionally you can resize windows using it.
If you prefer to use keyboard I have also mapped arrow keys in .vimrc in this way:
map <C-Left> <C-W>j
map <C-Down> <C-W>k
map <C-Up> <C-W>h
map <C-Right> <C-W>l
To move among left and right pane, Press ctrl+w and then ctrl+r. This is both left and right vice-versa.
You can also use :wincmd w for next window, and :wincmd W for previous window.
The :wincmd is especially useful when ctrl+w is captured by the environment. For example see: https://stackoverflow.com/a/73749587/811335
I have a lot of buffers open and to switch between then I Ctrl+w and then Ctrl+[movement key]. Is there a better way? Because this way when I've to do more than one movement I end up having to press 4 keys.
If you want to move between windows, you always can map Ctrl-W, H/J/K/L to anything you want:
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l
If you want to move between buffers, you can use :buffer command, print a part of the file name you want to go to: pressing Tab will cycle through all files that contains this part in the names. E.g you want to show buffer with filename 'some_long_c_file.c' and you know you don't have any other opened file whose name ends with 'file.c': just type :buffer file.c and press Tab (or even 'ile.c' or shorter), this will complete it to 'some_long_c_file.c'.
For quick switching between the two last buffers, use Ctrl-^
The best way I've found to do this is to install bufexplorer.vim. That let's you quickly switch to a screen in which you can select any buffer. You can also close buffers etc..
Here's a screenshot of my vim with bufexplorer open showing it's help screen:
For me the solution was two-fold:
1) Use unimpaired - so ]b and [b
2) map :b# to switch to previous buffer. I used map <cr> :b#<cr> . (so enter key would toggle back and forth between two open buffers).
Recently I found out that I'm "using tabs incorrectly" in Vim. I've been trying to just use buffers in Vim since, assisted through MiniBufExplorer, but I find it painful because of how many keystrokes it takes to change buffers from normal mode. With tabs, I can just do gt or gT to hop back and forth between tabs in normal mode, and I can also do NUMBERgt to go to a specific tab.
With buffers, I either have to enter command mode with :bn, :bp, or with MiniBufExplorer, use Ctrl + k or Ctrl + Up to hop up to the buffer window, scroll left or right with h and l and then hit Enter to select the buffer I want. Or I can do something involving a leader sequence, but it always requires removing multiple fingers away from home row. That's a real pain.
How can I get something equivalent switching tabs in normal mode to switch buffers in normal mode, so I can do something like gn/gp for :bn/:bp and NUMBERgn for :buf NUMBER?
Add this to your .vimrc
map gn :bnext<cr>
map gp :bprevious<cr>
map gd :bdelete<cr>
" I personally use <leader>
map <leader>n :bnext<cr>
map <leader>p :bprevious<cr>
map <leader>d :bdelete<cr>
Note that you are remapping gp and gd, but maybe you don't care about that (:help gp, :help gd).
For more information on how to map key strokes see :help map-overview and :help map.txt.
Btw, I personally use <leader> instead of g in the mapping. My <leader> is set to ;. This puts my <leader> key on the home row which makes me willing to map all kinds of stuff using <leader>. :help mapleader if you want to change your <leader> key.
The way I usually switch between buffers is to use the :buffer command with the built-in autocompletion, e.g. :b prof<Tab> to switch to folder/path/LoginProfileFactory.php.
You can just start typing any part of the file name of the buffer you need, which is nice.
Less often, I actually remember the numbers of the buffers I want and I use something like :b 3 or :3b.
I see you mention you don't like :buf 3 though, so Rumple Stiltskin has an alternative to the :3b style that you may prefer.
{count}CTRL-^ switches to the count numbered buffer.
I have the following lines in .vimrc:
nnoremap <silent> <tab> :if &modifiable && !&readonly && &modified <CR> :write<CR> :endif<CR>:bnext<CR>
nnoremap <silent> <s-tab> :if &modifiable && !&readonly && &modified <CR> :write<CR> :endif<CR>:bprevious<CR>
Now a Tab let you go to the next buffer and a Shift-Tab to the previous.
This is based on Nick Knowlson's answer, but I wanted to expand on my comment there ...
Type :b <Tab> (note the space), then cycle through the open buffers with Tab or ← / →.
... which gets us closer to the Ctrl + Tab in all the other editors and browsers I use.
It's actually even better in some ways, you can then go backwards and forwards with ← / → arrows. It avoids the thumb + finger fu to type Ctrl + Shift + Tab to go backwards through the tabs in editors and browsers.
N.B. Shift + Tab just does the same as Tab
This is then actually something like Win + Tab in Windows 10, where once you first open up the window and you can then move around using the arrow keys.
Edit: I have two further tricks that I picked up for using buffers:
From this answer I have this in my .vimrc:
nnoremap <leader>bb :buffers<cr>:b<space>
it opens the :ls / :buffers command and pre-types the :b so that you just have to type the buffer number as you'll see a list with all the buffers and their numbers.
I also have
nnoremap <leader><tab> :b#<cr>
which toggles between the current and most recently used buffers, it's a bit like doing cd - when switching back and forth between directories
I use the plugin unimpaired.vim
it defines mappings [b and ]b that jump to the previous and next buffer in the list.
For jumping for a specific buffer the best option I know is the one you mentioned: :b<number>
If you go into another buffer you can came back quickly by typing <c-^>
Expanding on Rumple Stiltskin's answer, if you know that the file you want to get to is in buffer 4, for example, you can get there quickly with
4Ctrl-^
On my UK keyboard, I can actually do 4Ctrl-6, as explained in
:help CTRL-^
By the way, you can see the buffer numbers with
:buffers
or
:ls
I use LustyExplorer: I hit <leader>b to open a list of buffers then a couple of letters from the name of the buffer I want to open then enter. Easy.
But, you are not "using tabs incorrectly", you are using tabs the way you want. If it worked for you why go through the pain of unlearning your way to learn "the right way"?
I use F9 and F10 to move between the previous/next buffer with this mapping:
map <F9> :bprevious<CR>
map <F10> :bnext<CR>
For me this is the fastest way to switch buffers.
fzf.vim is another fast way to changes buffers using fuzzy matching. This plug-in ships with the default command:
:Buffers
which opens the list of all open buffers similar to :ls but a buffer can be (fuzzy) searched and selected.
Opening the buffer in the current window is through enter, but can can also be opened in a new split (h or v) or tab using ^X ^V or ^T respectively.
Noteworthy is also:
:Lines
Which allows to search through the content of all open buffers. This can be handy if you forget the name of a buffer but you know what it should contain.
Here is my solution:
" `<leader><Tab>` - next buffer;
nnoremap <silent> <leader><Tab> :bnext<CR>
" `<leader><S-Tab>` - previous buffer;
nnoremap <silent> <leader><S-Tab> :bprevious<CR>
" `_bufferNumber_ + <Tab>` - go exact the buffer number;
nnoremap <silent> <Tab> <C-^>
By the way, I use 'buftabline' plugin and set let g:buftabline_numbers = 1 to spread my buffer on the tabline.
I make it easier for myself:
In .vimrc :
nnoremap <leader>bf :buffers<CR>:buffer "<- Last spaces is necessary
For example, in normal mode, say your leader key is \(default it is), type \bf, then you have a list of opened buffers, type number of buffer you want and hit enter key.
NOTE: remember that last spaces not necessary at all if you wich type it after :D
Jut like that ;)
More detail:
<C-O> Navigate backward
<C-I> Navigate forward
So there is no need extra remapping, otherwise you remapped them.
I think bufexplorer is a nice plugin to use. <leader>be brings up an interactive buffer explorer that lists all open buffers. You could quickly move through the list and Enter puts you in the selected buffer. Unlike LustyExplorer It has no dependency to ruby.
I prefer navigating between buffers similarly to how I'm navigating between window panes: <alt-h> and <alt-l>. This is to straightforward to set on Mac because <alt>/<option> key binds are bounded to specific characters.
" Buffer navigation
map ˙ :bp<cr>
map ¬ :bn<cr>
map § <c-^>
Here is a good answer that shows how you can see characters maped to <alt-..> combinations