I'm making a plugin where the idea is that if the buffer is empty then you'll enter insert mode, otherwise you'll stay in normal mode.
Of course this is all nice and well but you come across some problems, what if you just opened Vim and you're in the default empty buffer and you don't want to do anything in that buffer? You gotta first go to normal mode and then do your :e magic. Which is undesirable as it takes more work.
So my question is, how does one detect if the current buffer is a real file whether it exists in the file system or not.
For example, I'd like to detect if the buffer is a JS file, whether new or pre-existing. Or if the buffer is a NERDTree buffer or a plugin buffer, that kind of stuff.
Thank you. Any help is appreciated. :)
P.S.: For those interested, this is the plugin: https://github.com/Greduan/vim-empty-insert
if expand('%:p') != ''
" do stuff
endif
See :h %:p and :h expand().
BTW, sorry if this sounds rude, but I'd guess many long-time Vim users would automatically press a/i when they enter a buffer, without thinking whether it exists on the drive or not. So you might want to consider keeping the functionality more consistent.
Related
When I use vim recently, I observe that the insert mode in vim doesn't work properly as I expect. As I switch the applications by using the shortcut Alt-Tab or Cmd-tab, vim writes [O in to buffer. Same thing happens when I switch back to the terminal(vim), vim writes [I into the buffer, I can't find a good keyword to find the right solution, so can someone give me a solution, a keyword or a hint please?
Thanks!!!!!
I want to:
Automaticaly close Lexpore after opening a file for better experience but I don't know vim script.
It shouldn't be hard, I suppose.
I will appreciate any comment!
I'm now learning vim script and trying to do it.
here is where I've got by now:
:autocmd <Lexplore> * :<Lexplore-exit>
or
if <command-mode> == "Lexplore *.*"
excute \\close last buffer. (because now I am in new buffer)
I just need to know how to say "RUN" / "RUNED" in script
then i use regex for anyfile and i need to say "CLOSE".
The truth is
I'm actually Hopeless! :)))))))))))))
There are other avenues to try before going full-on with VimScript. In this case, a simple mapping would probably be enough.
Basically, the right-hand side of a mapping is just a macro, a sequence of commands that you would type yourself, which makes mappings a very good and approchable way to discover Vim automation.
Assuming a simple layout, with only one window, how do you do what you describe manually?
You do :Lexplore and browse around.
In the Netrw window, you press <CR> to open the file.
Then you go back to the previous window (the Netrw window) with <C-w>p.
You close the window with <C-w>q.
Steps 2-4 above are easy to turn into a mapping (<key> is a placeholder, use what you want):
nmap <key> <CR><C-w>p<C-w>q
But that mapping will be available everywhere, which may not be a good idea. Enters the notion of filetype plugins:
Create these directories if they don't already exist:
~/.vim/after/ftplugin/ (Unix-like systems)
%userprofile%\vimfiles\after\ftplugin\ (Windows)
Create a netrw.vim file in the directory above:
~/.vim/after/ftplugin/netrw.vim (Unix-like systems)
%userprofile%\vimfiles\after\ftplugin\netrw.vim (Windows)
Put the mapping above in that file:
nmap <buffer> <key> <CR><C-w>p<C-w>q
The <buffer> tells Vim to only define that mapping in Netrw.
This is the simplest approach, which may or may not work for you.
A few notes regarding your comments…
Buffer numbers are implementation detail. They don't matter at all in your day-to-day work so you shouldn't think too much about them. Buffer names are far more useful and much easier to think about.
The "sidebar file explorer" UX pattern is a familiar one that newcomers like to transfer from their previous editor but it necessitates a lot of fighting against Vim itself to "work". Netrw's take on the pattern is different, yes, and it might take time to adjust to, but it fits much better within the overall experience. Hint: give up on :Lexplore. :Explore, :Sexplore, :Vexplore, and :Texplore are much more useful.
As you learn more, you will soon discover that file explorers, whatever UX pattern they follow, are not that useful to begin with.
Netrw already has a mapping for opening a file in a new tab page: :help netrw-t.
I often like to have a copy of the file that I am working on open on a second monitor, usually to assist with writing tests. I sometimes mindlessly attempt to edit it on this monitor, rather than the most up to date one. To prevent this, I have been setting read only mode with :set ro but that doesn't prevent me from making changes, it only prevents me from saving.
I would like a lock that turns off the ability to go into insert mode. I could create a toggle that remaps all the insert mode keys, but I was wondering if there is a better solution?
Yes, it's possible. :set nomodifiable in the buffer you want to mark read-only. This is used by plugins like taglist to create non-modifiable buffers.
You can open with the argument -M. This prevents edits to the buffer.
minibufexplorer is a persistent buffer manager for vim that lives in its own window. It shows all the buffers you have open, with color indicators as to which are currently visible in windows and which have unsaved changes.
It's a great plugin which I've been hooked on forever. And without the persistent buffer display I find that I now feel lost.
The problem is, minibufexpl tends to get in the way of other window controls. As it is a normal vim window, it behaves like one, causing issues if you wanted to say, rotate your other working windows. Minibufexplorer's window would rotate as well, which is obviously less than desirable.
What I really need is a plugin that persistently displays the open buffers, but doesn't behave like a window. I don't need file navigation or anything as I use other means for that. Perhaps even something like an extended status bar that was capable of displaying information on buffers like minibufexpl does?
There's a new fork of minibufexpl.vim on GitHub which aims to solve some of these problems. One of its new features is "Prevents resizing of MBE buffer by window resizing commands" and it is immune to commands such as C-WC-R (rotate windows).
https://github.com/fholgado/minibufexpl.vim
While this is not an exact answer to your question, I think you could find it useful.
FuzzyFinder plugin provides good visual representation of your open buffers,
and also offers great way to jump between them - fuzzy match.
Its list of buffers is not "persistent" meaning it's only shown on
demand, but I consider this an advantage: firstly the buffers list doesn't eat
up precious screen estate, and secondly with fuzzy match I don't feel a
need to see it at all.
Consider the below screenshot: in order to switch to ext_gcd.py I just need to press
,bg : ,b invokes buffers list and g fuzzy-matches the only buffer
whose name contains letter g.
P.S: it works with files too. My mappings are as follows:
map <leader>f :FufFileWithCurrentBufferDir **/<C-M>
map <leader>b :FufBuffer<C-M>
,f in this case recursively fuzzy-matches files starting from a directory where current buffer is placed.
Old thread, but for anyone else searching checkout the vim-airline plugin. It's very popular and has great compatibility with other plugins.
Use the g:airline#extensions#bufferline#enabled option to get what your looking for.
I really enjoy buftabs.vim because it keeps the information about which buffers I have open in the status line, which disappear when I need to type there in a nice clear manner. I find this quite preferable to minibufexplorer because my cursor never accidentally ends up in a read-only scratch buffer which takes up more space than necessary. It also does some magic-mapping of :bnext and :blast to allow hopping between buffers for a rather more "familiar" feeling.
You can use ctrlp, a plugin written in pure Vimscript and highly configurable.
:CtrlPBuffer open a window with the list of open buffers
you can begin to type, it is a fuzzy finder.
There is also Tagma Buffer Manager. I am not using it yet, but someday I will give it a try, for sure.
You can try Powerline's tabline!
I tried many plugins but they tend to take up the command line or the status line, or won't show up the whole tabs when there're too many of them.
Here is a screen shot (the top bar is the tabline!):
You need to add set showtabline=2 in .vimrc after install Powerline.
And if you want to go to the 8th buffer, :b 8 will jump to it!
Hope this helps.
In Vim, the :enew command has a really irritating behavior - after I create a new file, type something and save it, an empty buffer/file is still present. Why is that and is there any way to prevent it? I'm tired of having to close all these empty buffers all the time.
Thanks!
Your question doesn't make any sense, :enew only creates one new empty buffer and you say you are already writing text and saving it. It sounds like you are starting up vim without specifying a file, and using :enew as your first command, in which case you would then have two empty buffers. Why not use the new buffer that vim creates on startup?
Btw, :set confirm makes it easier to discard unused buffers.