How can I explain this behaviour in Vim? - vim

Vim is so awesome. For example, you have a file, called 'test0.html', stored in a folder.
In the same folder, you store the folder 'test' with the files test1.html, and test2.html.
Then you put in test0.html the following content:
include('test/test1.html');
include('test/test2.html');
In vim, you put the cursor on the filenames. You open the files under the corsor with the keys gf. Thats why Vim is so awesome.
I would like to open in a new tab. That's possible with the keys gF.
But what if you want to stay in the same file, but open the file in a background tab, like Chrome does?
So I'm mapping the key.
noremap gf <c-w>gF<c-PageDown>
So, when my cursor is on test1.html, it open with the key gf in a background tab. Wonderful, now I'm a satisfied man.
Then I want to open test2.html under cursor.
Vim jumps to the tab of test1.html, instead stay on test0.html
When I tried to debug this weird behaviour, by only mapping gf to gF, and then do manual CTRL+pagedown, I get the expected behaviour.
My guess is that Vim is much faster with executing the command before he opens the new tab (gF), and so I get to the last tab from the first tab.
Am I correct in my guess explaination?

<c-PageDown> or more commonly used gT will got to the previous tab. <c-w>gF on the other hand will open the file under the cursor in a new tab. That tab will be last tab. So doing a gT will not always make you go back to the previous tab.
You can change your mapping to go back to the previous tab like so:
nnoremap gf :execute "normal! \<lt>c-w>gF" . tabpagenr() . "gt"<cr>
However I would personally suggest you avoid using tabs in such a manner and use buffers instead.

noremap gf :tabe<cfile><CR><c-PageUp>
This is even better. When the file doesn't exist, Vim will create a new one.

Related

Vim, how to make Netrw to replace current open file instead of split opening new window, when you press preview or create new file commands?

Recently I've been using netrw. I've put these four lines on my .vimrc:
let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_winsize = 25
map <C-n> :Lexplore <CR>
And I can easily toggle Lexplore and browse through the files to edit them. In this mode when I press Enter on any file on left hand side Netrw, it replaces the file in the right hand side window with new file; exactly what I want.
Problems start when I want to preview a file with "p" command on netrw or create a new file with "%" command. In former case (Preview) it split to the new window but I want it again to replace the file in right hand side window just like when I press Enter to to edit the file. And in latter case (creating a new file) it replaces the Lexplore (Netrw in the left hand side) instead of replacing the file on the right hand side window.
Is there any way that I can fix these issues? I've tried a lot of Netrw commands but nothing gives me what I want.
The "p" netrw command is mostly just pedit. So, assuming you don't already have a preview window, just press <cr> atop the file you want to preview and then :set pvw. That will make the window holding the file the preview window.
The second issue concerns "%" to create a new file. I'll think this over, but I think perhaps the better behavior is as you suggest -- keep the Lexplore window but open the new file in the editing window.
Please try version 171e of netrw which you can find at my website.
I believe this is related to a bug in vim. Once you use :Lexplore it apparently changes the g:netrw_chgwin option permanently, so whenever you try to pres <CR> in any netrw window, it will always open in a new window.

NERDTree live-preview (like sublime sidebar)

Sublime's sidebar has a cool feature, where I can just press the arrow keys and get a quick glance of what each file looks like in the editor pane. It doesn't actually open the file -- just shows it in the editor pane.
I want to do the same thing with NERDTree in Vim (or Vinegar/netrw, doesn't really matter). I know NERDTree lets me use go to open the file under the cursor while keeping the tree in focus, but (a) that requires two keystrokes, and (b) it creates a new buffer for every file I "preview" like this, so... not much of a preview really.
Is there a way to make NERDTree or Vim emulate this Sublime feature?
Yes, there is. Vim has a feature called "preview window". You can open a file in the preview window with :pedit <filename>. If you want to plug this into NERDTree, you could create a file in the ~/.vim/nerdtree_plugin/ directory, for example "live_preview_mapping.vim", with the following contents:
if exists("g:loaded_nerdree_live_preview_mapping")
finish
endif
let g:loaded_nerdree_live_preview_mapping = 1
call NERDTreeAddKeyMap({
\ 'key': '<up>',
\ 'callback': 'NERDTreeLivePreview',
\ 'quickhelpText': 'preview',
\ })
function! NERDTreeLivePreview()
" Get the path of the item under the cursor if possible:
let current_file = g:NERDTreeFileNode.GetSelected()
if current_file == {}
return
else
exe 'pedit '.current_file.path.str()
endif
endfunction
The first part is simply a load guard, so the file is sourced only once, just boilerplate. The second part adds a keymap using the NERDTree API for the <up> key that calls the given callback function.
The callback function is the meat of the code, but it should be fairly easy to understand -- it takes the node under the cursor, if there is one, and executes a :pedit with the filename.
You can even do this more easily with a simple filetype-specific mapping, something like this:
autocmd FileType nerdtree nnoremap <buffer> <up> :call NERDTreeLivePreview()<cr>
But the former is the approach recommended by the plugin (see :help NERDTreeAPI). If nothing else, this adds a help entry to the ? key for it, and it keeps nerdtree extensions in one place.
For more info on what you can do with the preview window, try :help preview-window. For instance, you can close it with <c-w>z, but you can map that to whatever you'd like, that's not really related to the NERDTree anymore. If you're unhappy with where the window shows up, consider changing the "pedit" to "botright pedit" or "leftabove pedit" or whatever you want. Check the help for :leftabove and take a look at the related commands below.
NERDTree doesn't offer anything automatic out of the box. I like a preview window that hijacks the last active window and allows for opening the buffer there, or to split with the original buffer. This extension does that, and its source code is pretty short.
https://github.com/numEricL/nerdtree-live-preview
With netrw, to preview a file: with the cursor atop a file, press "p".

Netrw open files into tabs in opposite vertical window

Imagine I have :Vex after starting vim. I want to be able to press t and have the tabs appended to the opposite window rather than the Netrw window. Is this possible?
If I press P I can open the file into the split window but I would like to be able to tab through the files in the vertical split whilst having my Netrw window visible - just like Sublime or Komodo.
Possible?
And yes, I've scoured :h netrw!
Almost got it with some .vimrc remaps and options.
It looks like this http://i.imgur.com/rUf19SF.png
Usage:
run vim.
hit shift enter. this will open netrw as a sidebar (a small split window to the right) and focus it.
browse as usual and hit enter to open a file. this will open it in the left window by default and focus it.
hit control-w control-w. this will focus netrw again.
browse as usual but this time hit control-enter to open a file. this will open it in a new tab that also contains the netrw sidebar.
The .vimrc config:
" netrw magic
" enable mouse usage. makes it easier to browse multiple tabs
set mouse=a
" hide netrw top message
let g:netrw_banner=0
" tree listing by default
let g:netrw_liststyle=3
" hide vim swap files
let g:netrw_list_hide='.*\.swp$'
" open files in left window by default
let g:netrw_chgwin=1
" remap shift-enter to fire up the sidebar
nnoremap <silent> <S-CR> :rightbelow 20vs<CR>:e .<CR>
" the same remap as above - may be necessary in some distros
nnoremap <silent> <C-M> :rightbelow 20vs<CR>:e .<CR>
" remap control-enter to open files in new tab
nmap <silent> <C-CR> t :rightbelow 20vs<CR>:e .<CR>:wincmd h<CR>
" the same remap as above - may be necessary in some distros
nmap <silent> <NL> t :rightbelow 20vs<CR>:e .<CR>:wincmd h<CR>
Caveats:
The netrw "sidebar" in each tab is independent, meaning the current directory in a tab may not be the same in another tab. Suggestions? Thought of using the netrw buffer in every "sidebar" window, but netrw uses a new buffer whenever changing directories.
You seem to be confusing Vim's "tabs" with the "tabs" you can find in virtually every other program.
Unlike other implementations, Vim's tabs are not tied to a buffer. In Vim, "tabs" behave like what you would call "workspaces": they are meant to keep together one or more windows. Those windows could display any buffer and you can very well end up with the same buffer displayed in multiple windows in multiple tabs!
With that in mind, it would be very wrong to use them like you want. "Tabs" don't represent files and jumping to another "tab" is not equivalent to jumping to another file at all.
The window created by :Vex is a normal window. Like all the other windows, it is contained in a "tab" and can't live outside of a "tab". And you can't have "tabs" inside of windows.
So, basically, what you ask is impossible.
If you are on a Mac and this "other-editor-like feature" is really important for you (more important than, say, embrace the Vim way), you could try this MacVim fork that adds a "regular" file explorer outside of the buffer/window/tab trio. You could also try PIDA which tries to build an IDE around Vim; including a separate file explorer.
As romainl said, tabs are not files (or buffers). So, if I re-interpret your question to mean: "I want to press t and have files appear in the opposite window...". Then I suggest reading :help netrw-C. If you really do mean "append" and not "appear", then that's more involved and before I expend the effort to figure out how to do so I'd like to know that that's what you really meant. The latest netrw (as of today, that's v153f) has additional options which are mentioned in that help reference I gave above.

Traversing directories with vim file name completion in insert mode (Ctrl-X Ctrl-F)

I’m trying to use vim’s compl-filename feature (Ctrl-XCtrl-F) to complete paths in INSERT mode, but I can’t work out how to traverse into directories without (temporarily) ending the completion mode:
Let’s say I want to complete the path /etc/sysconfig/network-scripts/ifup.
I would like to be able to do something like:
/eCtrl-XCtrl-F
/etc/
/etc/sysCtrl-F
/etc/sysconfig/
/etc/sysconfig/netCtrl-F
/etc/sysconfig/netconsoleCtrl-N
/etc/sysconfig/networkCtrl-N
/etc/sysconfig/network-scripts/
/etc/sysconfig/network-scripts/ifupCtrl-Y
/etc/sysconfig/network-scripts/ifup
The issue is, as soon as I start typing* after a path match (like /etc/), it ends file name completion. I would like it to stay in file name completion, so that I can still use Ctrl-F, Ctrl-N, etc. Since it ends completion, I have to type Ctrl-XCtrl-F again to restart it, and the helpful completion popup menu disappears in the meantime.
Is there an option I can set to change this?
* By ‘typing’ here, I am referring to characters in 'isfname' -- of course, typing other characters (like space or punctuation) should not continue file name completion.
I'm not sure exactly what you're saying, but you can just press Ctrl-XCtrl-F again on a directory while you're in the completion menu to expand it. You don't have to close out of the menu first. I just keep Ctrl held down and tap xf to traverse a directory, n and p to move up and down and w to go back up.
If you don't use :h i_CTRL-F then you could remap it. For example,
inoremap <C-f> <C-x><C-f>
Simple remap would be
inoremap / /<C-x><C-f>
So when you type slash(/) in insert mode you will get that auto completion popup :)
Place it in your .vimrc file (for vim) or in init.vim (for neovim)
Vim doesn't do auto-completion.
For that, you'll need a dedicated plugin like AutoComplPop or NeoComplCache
Please use insert "i" first before using cntr+x+f. I was in similar situation. :)

How to effectively work with multiple files in Vim

I've started using Vim to develop Perl scripts and am starting to find it very powerful.
One thing I like is to be able to open multiple files at once with:
vi main.pl maintenance.pl
and then hop between them with:
:n
:prev
and see which file are open with:
:args
And to add a file, I can say:
:n test.pl
which I expect would then be added to my list of files, but instead it wipes out my current file list and when I type :args I only have test.pl open.
So how can I add and remove files in my args list?
Why not use tabs (introduced in Vim 7)?
You can switch between tabs with :tabn and :tabp,
With :tabe <filepath> you can add a new tab; and with a regular :q or :wq you close a tab.
If you map :tabn and :tabp to your F7/F8 keys you can easily switch between files.
If there are not that many files or you don't have Vim 7 you can also split your screen in multiple files: :sp <filepath>. Then you can switch between splitscreens with Ctrl+W and then an arrow key in the direction you want to move (or instead of arrow keys, w for next and W for previous splitscreen)
Listing
To see a list of current buffers, I use:
:ls
Opening
To open a new file, I use
:e ../myFile.pl
with enhanced tab completion (put set wildmenu in your .vimrc).
Note: you can also use :find which will search a set of paths for you, but you need to customize those paths first.
Switching
To switch between all open files, I use
:b myfile
with enhanced tab completion (still set wildmenu).
Note: :b# chooses the last visited file, so you can use it to switch quickly between two files.
Using windows
Ctrl-W s and Ctrl-W v to split the current window horizontally and vertically. You can also use :split and :vertical split (:sp and :vs)
Ctrl-W w to switch between open windows, and Ctrl-W h (or j or k or l) to navigate through open windows.
Ctrl-W c to close the current window, and Ctrl-W o to close all windows except the current one.
Starting vim with a -o or -O flag opens each file in its own split.
With all these I don't need tabs in Vim, and my fingers find my buffers, not my eyes.
Note: if you want all files to go to the same instance of Vim, start Vim with the --remote-silent option.
:ls
for list of open buffers
:bp previous buffer
:bn next buffer
:bn (n a number) move to n'th buffer
:b <filename-part> with tab-key providing auto-completion (awesome !!)
In some versions of vim, bn and bp are actually bnext and bprevious respectively. Tab auto-complete is helpful in this case.
Or when you are in normal mode, use ^ to switch to the last file you were working on.
Plus, you can save sessions of vim
:mksession! ~/today.ses
The above command saves the current open file buffers and settings to ~/today.ses. You can load that session by using
vim -S ~/today.ses
No hassle remembering where you left off yesterday. ;)
To add to the args list:
:argadd
To delete from the args list:
:argdelete
In your example, you could use :argedit test.pl to add test.pl to the args list and edit the file in one step.
:help args gives much more detail and advanced usage
I use buffer commands - :bn (next buffer), :bp (previous buffer) :buffers (list open buffers) :b<n> (open buffer n) :bd (delete buffer). :e <filename> will just open into a new buffer.
I think you may be using the wrong command for looking at the list of files that you have open.
Try doing an :ls to see the list of files that you have open and you'll see:
1 %a "./checkin.pl" line 1
2 # "./grabakamailogs.pl" line 1
3 "./grabwmlogs.pl" line 0
etc.
You can then bounce through the files by referring to them by the numbers listed, e.g.
:3b
or you can split your screen by entering the number but using sb instead of just b.
As an aside % refers to the file currently visible and # refers to the alternate file.
You can easily toggle between these two files by pressing Ctrl Shift 6
Edit: like :ls you can use :reg to see the current contents of your registers including the 0-9 registers that contain what you've deleted. This is especially useful if you want to reuse some text that you've previously deleted.
Vim (but not the original Vi!) has tabs which I find (in many contexts) superior to buffers. You can say :tabe [filename] to open a file in a new tab. Cycling between tabs is done by clicking on the tab or by the key combinations [n]gt and gT. Graphical Vim even has graphical tabs.
Things like :e and :badd will only accept ONE argument, therefore the following will fail
:e foo.txt bar.txt
:e /foo/bar/*.txt
:badd /foo/bar/*
If you want to add multiple files from within vim, use arga[dd]
:arga foo.txt bar.txt
:arga /foo/bar/*.txt
:argadd /foo/bar/*
Many answers here! What I use without reinventing the wheel - the most famous plugins (that are not going to die any time soon and are used by many people) to be ultra fast and geeky.
ctrlpvim/ctrlp.vim - to find file by name fuzzy search by its location or just its name
jlanzarotta/bufexplorer - to browse opened buffers (when you do not remember how many files you opened and modified recently and you do not remember where they are, probably because you searched for them with Ag)
rking/ag.vim to search the files with respect to gitignore
scrooloose/nerdtree to see the directory structure, lookaround, add/delete/modify files
EDIT: Recently I have been using dyng/ctrlsf.vim to search with contextual view (like Sublime search) and I switched the engine from ag to ripgrep. The performance is outstanding.
EDIT2: Along with CtrlSF you can use mg979/vim-visual-multi, make changes to multiple files at once and then at the end save them in one go.
Some answers in this thread suggest using tabs and others suggest using buffer to accomplish the same thing. Tabs and Buffers are different. I strongly suggest you read this article "Vim Tab madness - Buffers vs Tabs".
Here's a nice summary I pulled from the article:
Summary:
A buffer is the in-memory text of a file.
A window is a viewport on a buffer.
A tab page is a collection of windows.
To change all buffers to tab view.
:tab sball
will open all the buffers to tab view. Then we can use any tab related commands
gt or :tabn " go to next tab
gT or :tabp or :tabN " go to previous tab
details at :help tab-page-commands.
We can instruct vim to open ,as tab view, multiple files by vim -p file1 file2.
alias vim='vim -p' will be useful.
The same thing can also be achieved by having following autocommand in ~/.vimrc
au VimEnter * if !&diff | tab all | tabfirst | endif
Anyway to answer the question:
To add to arg list: arga file,
To delete from arg list: argd pattern
More at :help arglist
When using multiple files in vim, I use these commands mostly (with ~350 files open):
:b <partial filename><tab> (jump to a buffer)
:bw (buffer wipe, remove a buffer)
:e <file path> (edit, open a new buffer>
pltags - enable jumping to subroutine/method definitions
You may want to use Vim global marks.
This way you can quickly bounce between files, and even to the marked location in the file. Also, the key commands are short:
'C takes me to the code I'm working with,
'T takes me to the unit test I'm working with.
When you change places, resetting the marks is quick too:
mC marks the new code spot,
mT marks the new test spot.
If using only vim built-in commands, the best one that I ever saw to switch among multiple buffers is this:
nnoremap <Leader>f :set nomore<Bar>:ls<Bar>:set more<CR>:b<Space>
It perfectly combines both :ls and :b commands -- listing all opened buffers and waiting for you to input the command to switch buffer.
Given above mapping in vimrc, once you type <Leader>f,
All opened buffers are displayed
You can:
Type 23 to go to buffer 23,
Type # to go to the alternative/MRU buffer,
Type partial name of file, then type <Tab>, or <C-i> to autocomplete,
Or just <CR> or <Esc> to stay on current buffer
A snapshot of output for the above key mapping is:
:set nomore|:ls|:set more
1 h "script.py" line 1
2 #h + "file1.txt" line 6 -- '#' for alternative buffer
3 %a "README.md" line 17 -- '%' for current buffer
4 "file3.txt" line 0 -- line 0 for hasn't switched to
5 + "/etc/passwd" line 42 -- '+' for modified
:b '<Cursor> here'
In the above snapshot:
Second column: %a for current, h for hidden, # for previous, empty for hasn't been switched to.
Third column: + for modified.
Also, I strongly suggest set hidden. See :help 'hidden'.
I use the same .vimrc file for gVim and the command line Vim. I tend to use tabs in gVim and buffers in the command line Vim, so I have my .vimrc set up to make working with both of them easier:
" Movement between tabs OR buffers
nnoremap L :call MyNext()<CR>
nnoremap H :call MyPrev()<CR>
" MyNext() and MyPrev(): Movement between tabs OR buffers
function! MyNext()
if exists( '*tabpagenr' ) && tabpagenr('$') != 1
" Tab support && tabs open
normal gt
else
" No tab support, or no tabs open
execute ":bnext"
endif
endfunction
function! MyPrev()
if exists( '*tabpagenr' ) && tabpagenr('$') != '1'
" Tab support && tabs open
normal gT
else
" No tab support, or no tabs open
execute ":bprev"
endif
endfunction
This clobbers the existing mappings for H and L, but it makes switching between files extremely fast and easy. Just hit H for next and L for previous; whether you're using tabs or buffers, you'll get the intended results.
If you are going to use multiple buffers, I think the most important thing is to
set hidden
so that it will let you switch buffers even if you have unsaved changes in the one you are leaving.
I use the following, this gives you lots of features that you'd expect to have in other editors such as Sublime Text / Textmate
Use buffers not 'tab pages'. Buffers are the same concept as tabs in almost all other editors.
If you want the same look of having tabs you can use the vim-airline plugin with the following setting in your .vimrc: let g:airline#extensions#tabline#enabled = 1. This automatically displays all the buffers as tab headers when you have no tab pages opened
Use Tim Pope's vim-unimpaired which gives [b and ]b for moving to previous/next buffers respectively (plus a whole host of other goodies)
Have set wildmenu in your .vimrc then when you type :b <file part> + Tab for a buffer you will get a list of possible buffers that you can use left/right arrows to scroll through
Use Tim Pope's vim-obsession plugin to store sessions that play nicely with airline (I had lots of pain with sessions and plugins)
Use Tim Pope's vim-vinegar plugin. This works with the native :Explore but makes it much easier to work with. You just type - to open the explorer, which is the same key as to go up a directory in the explorer. Makes navigating faster (however with fzf I rarely use this)
fzf (which can be installed as a vim plugin) is also a really powerful fuzzy finder that you can use for searching for files (and buffers too). fzf also plays very nicely with fd (a faster version of find)
Use Ripgrep with vim-ripgrep to search through your code base and then you can use :cdo on the results to do search and replace
My way to effectively work with multiple files is to use tmux.
It allows you to split windows vertically and horizontally, as in:
I have it working this way on both my mac and linux machines and I find it better than the native window pane switching mechanism that's provided (on Macs). I find the switching easier and only with tmux have I been able to get the 'new page at the same current directory' working on my mac (despite the fact that there seems to be options to open new panes in the same directory) which is a surprisingly critical piece. An instant new pane at the current location is amazingly useful. A method that does new panes with the same key combos for both OS's is critical for me and a bonus for all for future personal compatibility.
Aside from multiple tmux panes, I've also tried using multiple tabs, e.g. and multiple new windows, e.g. and ultimately I've found that multiple tmux panes to be the most useful for me. I am very 'visual' and like to keep my various contexts right in front of me, connected together as panes.
tmux also support horizontal and vertical panes which the older screen didn't (though mac's iterm2 seems to support it, but again, the current directory setting didn't work for me). tmux 1.8
In my and other many vim users, the best option is to,
Open the file using,
:e file_name.extension
And then just Ctrl + 6 to change to the last buffer. Or, you can always press
:ls to list the buffer and then change the buffer using b followed by the buffer number.
We make a vertical or horizontal split using
:vsp for vertical split
:sp for horizantal split
And then <C-W><C-H/K/L/j> to change the working split.
You can ofcourse edit any file in any number of splits.
I use the command line and git a lot, so I have this alias in my bashrc:
alias gvim="gvim --servername \$(git rev-parse --show-toplevel || echo 'default') --remote-tab"
This will open each new file in a new tab on an existing window and will create one window for each git repository.
So if you open two files from repo A, and 3 files from repo B, you will end up with two windows, one for repo A with two tabs and one for repo B with three tabs.
If the file you are opening is not contained in a git repo it will go to a default window.
To jump between tabs I use these mappings:
nmap <C-p> :tabprevious<CR>
nmap <C-n> :tabnext<CR>
To open multiple files at once you should combine this with one of the other solutions.
I use multiple buffers that are set hidden in my ~/.vimrc file.
The mini-buffer explorer script is nice too to get a nice compact listing of your buffers. Then :b1 or :b2... to go to the appropriate buffer or use the mini-buffer explorer and tab through the buffers.
have a try following maps for convenience editing multiple files
" split windows
nmap <leader>sh :leftabove vnew<CR>
nmap <leader>sl :rightbelow vnew<CR>
nmap <leader>sk :leftabove new<CR>
nmap <leader>sj :rightbelow new<CR>
" moving around
nmap <C-j> <C-w>j
nmap <C-k> <C-w>k
nmap <C-l> <C-w>l
nmap <C-h> <C-w>h
I made a very simple video showing the workflow that I use. Basically I use the Ctrl-P Vim plugin, and I mapped the buffer navigation to the Enter key.
In this way I can press Enter in normal mode, look at the list of open files (that shows up in a small new window at the bottom of the screen), select the file I want to edit and press Enter again. To quickly search through multiple open files, just type part of the file name, select the file and press Enter.
I don't have many files open in the video, but it becomes incredibly helpful when you start having a lot of them.
Since the plugin sorts the buffers using a MRU ordering, you can just press Enter twice and jump to the most recent file you were editing.
After the plugin is installed, the only configuration you need is:
nmap <CR> :CtrlPBuffer<CR>
Of course you can map it to a different key, but I find the mapping to enter to be very handy.
I would suggest using the plugin
NERDtree
Here is the github link with instructions.
Nerdtree
I use vim-plug as a plugin manager, but you can use Vundle as well.
vim-plug
Vundle
When I started using VIM I didn't realize that tabs were supposed to be used as different window layouts, and buffer serves the role for multiple file editing / switching between each other. Actually in the beginning tabs are not even there before v7.0 and I just opened one VIM inside a terminal tab (I was using gnome-terminal at the moment), and switch between tabs using alt+numbers, since I thought using commands like :buffers, :bn and :bp were too much for me. When VIM 7.0 was released I find it's easier to manager a lot of files and switched to it, but recently I just realized that buffers should always be the way to go, unless one thing: you need to configure it to make it works right.
So I tried vim-airline and enabled the visual on-top tab-like buffer bar, but graphic was having problem with my iTerm2, so I tried a couple of others and it seems that MBE works the best for me. I also set shift+h/l as shortcuts, since the original ones (moving to the head/tail of the current page) is not very useful to me.
map <S-h> :bprev<Return>
map <S-l> :bnext<Return>
It seems to be even easier than gt and gT, and :e is easier than :tabnew too. I find :bd is not as convenient as :q though (MBE is having some problem with it) but I can live with all files in buffer I think.
Most of the answers in this thread are using plain vim commands which is of course fine but I thought I would provide an extensive answer using a combination of plugins and functions that I find particularly useful (at least some of these tips came from Gary Bernhardt's file navigation tips):
To toggle between the last two file just press <leader> twice. I recommend assigning <leader> to the spacebar:
nnoremap <leader><leader> <c-^>
For quickly moving around a project the answer is a fuzzy matching solution such as CtrlP. I bind it to <leader>a for quick access.
In the case I want to see a visual representation of the currently open buffers I use the BufExplorer plugin. Simple but effective.
If I want to browse around the file system I would use the command line or an external utility (Quicklsilver, Afred etc.) but to look at the current project structure NERD Tree is a classic. Do not use this though in the place of 2 as your main file finding method. It will really slow you down. I use the binding <leader>ff.
These should be enough for finding and opening files. From there of course use horizontal and vertical splits. Concerning splits I find these functions particularly useful:
Open new splits in smaller areas when there is not enough room and expand them on navigation. Refer here for comments on what these do exactly:
set winwidth=84
set winheight=5
set winminheight=5
set winheight=999
nnoremap <C-w>v :111vs<CR>
nnoremap <C-w>s :rightbelow split<CR>
set splitright
Move from split to split easily:
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
if you're on osx and want to be able to click on your tabs, use MouseTerm and SIMBL (taken from here). Also, check out this related discussion.
You can be an absolute madman and alias vim to vim -p by adding in your .bashrc:
alias vim="vim -p"
This will result in opening multiple files from the shell in tabs, without having to invoke :tab ball from within vim afterwards.
To open 2 or more files with vim type: vim -p file1 file2
After that command to go threw that files you can use CTRL+Shift+↑ or ↓ , it will change your files in vim.
If u want to add one more file vim and work on it use: :tabnew file3
Also u can use which will not create a new tab and will open file on screen slicing your screen: :new file3
If u want to use a plugin that will help u work with directories
and files i suggest u NERDTree.
To download it u need to have vim-plug so to download other plugins also NERDTree to type this commands in your ~/.vimrc.
let data_dir = has('nvim') ? stdpath('data') . '/site' : '~/.vim'
if empty(glob(data_dir . '/autoload/plug.vim'))
silent execute '!curl -fLo '.data_dir.'/autoload/plug.vim --create-dirs
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
call plug#begin('~/.vim/plugged')
Plug 'scrooloose/nerdtree'
call plug#end()
Then save .vimrc via command :wq , get back to it and type: :PlugInstall
After that the plugins will be installed and u could use your NERDTree with other plugins.

Resources