How to navigate results (candidates) in Unite.vim? - vim

After a search in unite.vim, you open a candidate. Is there an easy way to jump to the next one without having to run the search command again? Similar plugins (ack.vim, git-grep) use the quickfix window, so you can type :cn and :cp to jump to next/previous results. Is there anything similar in unite.vim?
Thanks!

I haven't managed to find a way to put results into the quickfix or location lists, though I'm sure you could extend Unite to include that action.
Meanwhile, this little exerpt from the Unite documentation can help you re-open the Unite buffer you just recently had open:
:UniteResume [{options}] [{buffer-name}] *:UniteResume*
Reuses the unite buffer named {buffer-name} that you opened
previously. Narrowing texts or candidates are
as-is. If {options} are given, context information gets
overridden.
Note: Reuses the last unite buffer you used in current tab if
you skip specifying {buffer-name}.
I use this in the following way in my vimrc:
" Press <leader>ll to re-open last Unite buffer
nnoremap <silent><leader>ll :<C-u>UniteResume<CR>
Another useful snippet from the docs:
-no-quit
Doesn't close unite buffer after firing an action. Unless you
specify it, a unite buffer gets closed when you selected an
action which is "is_quit".
-keep-focus
Keep the focus on a unite buffer after firing an action.
Note: This option is used with "-no-quit" option.
For example, you could bind
nnoremap <silent><leader>lg :<C-u>Unite -no-quit -keep-focus grep<CR>
To be able to press <leader>lg to grep through your source files, without closing or de-focusing the Unite buffer when you select an item.

Good news, everyone! I created an issue-724 with your request in Unite repo and it has been implemented in the blink of an eye! Glory to Shougo!
From the docs:
:UniteNext [{buffer-name}] *:UniteNext*
Do the default action with next candidates in the unite buffer
with {buffer-name}.
You can use it like |:cnext|.
:UnitePrevious [{buffer-name}] *:UnitePrevious*
Do the default action with previous candidates in the unite
buffer with {buffer-name}.
You can use it like |:cprevious|.

Related

Vim and NERDTree - Clear [No name] from buffers

I am new to Vim and NERDTree, and I am trying to understand why a buffer appears as [No name] after I delete it using :bd. I want to keep my NERDTree clean so I wonder if there is a way to remove the [No name].
For example,
The l.py file is the one I am working on, and the [No name] ones are the files that I already closed (with :bd). I don't want them to show up at all because it looks messy.
Thanks!
I doubt NerdTree is putting buffers into your status line. I imagine some status line plugin like vim-airline. It would probably be best to look at your status line plugin's documentation for how to make customizations or submit an issue on the plugin's bug tracker.
We need to talk...
The biggest problem I see is that you are trying to use the status line as a makeshift "tab bar". Most other editors use a tab bar as a way to manage documents, but not Vim. This makeshift "tab bar" is probably a bad idea once you start using more files or more complicated workflows with splits or tabs (Vim's tabs are different).
Oh yeah? What about Buftabline, Airline, BufTabs, MiniBufExpl, ...?
All these plugins do is show you your currently listed buffers. Maybe with some kind of positioning information so that you feel comfortable cycling via :bnext and :bprevious (or whatever mappings you might be using) through your buffer list.
Now that is great and all, these plugins have recreated other editor's version of tabs, but Vim already let you cycle through buffers without these plugins. The only thing missing was a menu which :ls will gladly do for you without wasting any screen real-estate.
Imagine having 10, 25, 50, or 100+ buffers open. How is your Buftabline going to handle that? My bet is not well. You need to stop reaching for simple tools like :bnext / :bprev and start reaching for a power tool like :b.
Behold the power of :b
The :b command can take a buffer number to switch directly to a buffer. Far more interesting, :b can take a partial filename.
:b partial-name
Need more power in your :b?
Uses tab completion
Uses <c-d> to list out completions
Accepts globs. e.g. :b *foo
Use ** to descend into directories. e.g. :b foo/**/bar
Don't forget to add set hidden to your vimrc. See :h 'hidden'
Why ride a bike when you can fly?
taken from Bairui's collection of Vim infographics.
You can use a simple mapping to leverage both :ls and :b:
nnoremap <leader>b :ls<cr>:b<space>
Now you can travel directly to you buffer you want. No more cycling.
But I like plugins
Who doesn't like a good plugin. As a matter a fact if you are looking for a nice buffer switching plugin then I would recommend you look into a nice fuzzy finder like CtrlP to aid you in switching between buffers. A fuzzy finder actually adds value to switching between buffers by getting you there faster with less typing.
Conclusion
Eventually you workflow will require you to use more than 3 buffers at a time. At that moment I would suggest you take another look at the :b command or at the very least get a nice fuzzy finder. It will save you time and effort.
So stop riding your bike when you can fly.

Manage "window-local" buffers in Vim

Clarification: A "window-local buffer" in this context just means a buffer once loaded into a specific window, any other buffer not ever loaded into that window is not a "window-local buffer".
I had some ruminations on ways managing buffers earlier and I think having a "window-local" buffer list would provide an extra way managing buffers. By saying that, I'm not interested in ways managing buffers already provided by Vim.
And it seems the solution is straightforward by recording "window-local" buffers manually like autocmd BufWinEnter * call add(w:buffers, expand('%')) and providing corresponding interfaces to the "window-local" buffer list w:buffers.
Do you find this useful? And any suggestion would be appreciated.
To list all open buffers use this:
:buffers
To open buffer #5 use this:
:buffer 5
You can use the following mapping to combine the two commands:
:nnoremap <F5> :buffers<CR>:buffer<Space>
How to use this convenient mapping:
press F5
enter a buffer number
press <Enter>
More reading here. (search for "switching by number")
The buffer list is global. You can have a window-local argument list (with :arglocal), and then use commands like :next to navigate through them.

Can Vim NerdTree be used as a buffer explorer

I have been using vim NerdTree for sometime now. One thought came to my mind and i am not sure if it is possible so seeking the help of experts.
Can Nerdtree show the list of open buffers say something like :NerdtreeBuffers
I know there is BufferExplorer but i dont like it for the simple fact that i have to pre-define if i want the buffer in a tab/split/vsplit
Nerdtree is awesome ... i just need one command and then from the list i can open it however i want.
No, NERDTree doesn't have any buffer list/exploration properties. I like Buffergator (GitHub) for quick buffer listing & selection. By default, it operates similarly to NERDTree, opening a left vertical split and allowing you to navigate to a buffer and hit enter to jump to it. It also supports opening buffers in vertical/horizontal splits, and other basic functionality. CtrlP is also handy. It does fuzzy find on files, but can also limit itself to open buffers.

How do I close a single buffer (out of many) in Vim?

I open several files in Vim by, for example, running
vim a/*.php
which opens 23 files.
I then make my edit and run the following twice
:q
which closes all my buffers.
How can you close only one buffer in Vim?
A word of caution: “the w in bw does not stand for write but for wipeout!”
More from manuals:
:bd
Unload buffer [N] (default: current
buffer) and delete it from
the buffer list. If the buffer was changed, this fails,
unless when [!] is specified, in which case changes are
lost.
The file remains unaffected.
If you know what you’re doing, you can also use :bw
:bw
Like |:bdelete|, but really delete the
buffer.
If this isn't made obvious by the the previous answers:
:bd will close the current buffer. If you don't want to grab the buffer list.
Check your buffer id using
:buffers
you will see list of buffers there like
1 a.php
2 b.php
3 c.php
if you want to remove b.php from buffer
:2bw
if you want to remove/close all from buffers
:1,3bw
Rather than browse the ouput of the :ls command and delete (unload, wipe..) a buffer by specifying its number, I find that using file names is often more effective.
For instance, after I opened a couple of .txt file to refresh my memories of some fine point.. copy and paste a few lines of text to use as a template of sorts.. etc. I would type the following:
:bd txt <Tab>
Note that the matching string does not have to be at the start of the file name.
The above displays the list of file names that match 'txt' at the bottom of the screen and keeps the :bd command I initially typed untouched, ready to be completed.
Here's an example:
doc1.txt doc2.txt
:bd txt
I could backspace over the 'txt' bit and type in the file name I wish to delete, but where this becomes really convenient is that I don't have to: if I hit the Tab key a second time, Vim automatically completes my command with the first match:
:bd doc1.txt
If I want to get rid of this particular buffer I just need to hit Enter.
And if the buffer I want to delete happens to be the second (third.. etc.) match, I only need to keep hitting the Tab key to make my :bd command cycle through the list of matches.
Naturally, this method can also be used to switch to a given buffer via such commands as :b.. :sb.. etc.
This approach is particularly useful when the 'hidden' Vim option is set, because the buffer list can quickly become quite large, covering several screens, and making it difficult to spot the particular buffer I am looking for.
To make the most of this feature, it's probably best to read the following Vim help file and tweak the behavior of Tab command-line completion accordingly so that it best suits your workflow:
:help wildmode
The behavior I described above results from the following setting, which I chose for consistency's sake in order to emulate bash completion:
:set wildmode=list:longest,full
As opposed to using buffer numbers, the merit of this approach is that I usually remember at least part of a given file name letting me target the buffer directly rather than having to first look up its number via the :ls command.
Use:
:ls - to list buffers
:bd#n - to close buffer where #n is the buffer number (use ls to get it)
Examples:
to delete buffer 2:
:bd2
You can map next and previous to function keys too, making cycling through buffers a breeze
map <F2> :bprevious<CR>
map <F3> :bnext<CR>
from my vimrc
Close buffer without closing the window
If you want to close a buffer without destroying your window layout (current layout based on splits), you can use a Plugin like bbye. Based on this, you can just use
:Bdelete (instead of :bdelete)
:Bwipeout (instead of :bwipeout)
Or just create a mapping in your .vimrc for easier access like
:nnoremap <Leader>q :Bdelete<CR>
Advantage over vim's :bdelete and :bwipeout
From the plugin's documentation:
Close and remove the buffer.
Show another file in that window.
Show an empty file if you've got no other files open.
Do not leave useless [no file] buffers if you decide to edit another file in that window.
Work even if a file's open in multiple windows.
Work a-okay with various buffer explorers and tabbars.
:bdelete vs :bwipeout
From the plugin's documentation:
Vim has two commands for closing a buffer: :bdelete and :bwipeout. The former removes the file from the buffer list, clears its options, variables and mappings. However, it remains in the jumplist, so Ctrl-o takes you back and reopens the file. If that's not what you want, use :bwipeout or Bbye's equivalent :Bwipeout where you would've used :bdelete.
How about
vim -O a a
That way you can edit a single file on your left and navigate the whole dir on your right...
Just a thought, not the solution...
[EDIT: this was a stupid suggestion from a time I did not know Vim well enough. Please don't use tabs instead of buffers; tabs are Vim's "window layouts"]
Maybe switch to using tabs?
vim -p a/*.php opens the same files in tabs
gt and gT switch tabs back and forth
:q closes only the current tab
:qa closes everything and exits
:tabo closes everything but the current tab
Those using a buffer or tree navigation plugin, like Buffergator or NERDTree, will need to toggle these splits before destroying the current buffer - else you'll send your splits into wonkyville
I use:
"" Buffer Navigation
" Toggle left sidebar: NERDTree and BufferGator
fu! UiToggle()
let b = bufnr("%")
execute "NERDTreeToggle | BuffergatorToggle"
execute ( bufwinnr(b) . "wincmd w" )
execute ":set number!"
endf
map <silent> <Leader>w <esc>:call UiToggle()<cr>
Where "NERDTreeToggle" in that list is the same as typing :NERDTreeToggle. You can modify this function to integrate with your own configuration.

How do you use vim's quickfix feature?

I'm a pretty new Vim user and I've found that its learning curve is quite steep (at least for me). I just installed this vim script for JavaScriptLint error checking, which shows errors in vim's quickfix window once I save a buffer.
However, I don't know what to do next.. How do I 'scroll' through all the errors? How do I close the quickfix 'window'? How do I get it to check for errors after I've made changes to my code?
I've looked at the vim quickfix docs but the amount of commands are overwhelming and I can't seem to find what I want. Any help would be appreciated.
A side question: is there any way to have javascriptlint check for js errors for code residing in a .html file?
There are a lot of commands for quickfix as you have said, but I tend to find I only use a small subset of them:
:copen " Open the quickfix window
:ccl " Close it
:cw " Open it if there are "errors", close it otherwise (some people prefer this)
:cn " Go to the next error in the window
:cp " Go to the previous error in the window
:cnf " Go to the first error in the next file
:.cc " Go to error under cursor (if cursor is in quickfix window)
I tend to use this with :make and :vimgrep, so I can't comment on the Javascript lint checker, but this should give you something to get started.
Regarding the general use of JavascriptLint, I'm not a javascript programmer, but it looks like the script exposes a function called "JavascriptLint", so if you want to call it manually, you can use :call JavascriptLint(). However, it works on the disk copy of the file, so it'll have to be saved first. If (and only if) the command line jsl works on html files, you should be able to use :call JavascriptLint() on an html file to check the internal javascript. You could also do:
autocmd BufWritePost,FileWritePost *.html call JavascriptLint()
to automate it. If jsl doesn't support html files, then (short of patching the application or asking the author to change it), it's probably a lost cause...
The easiest way to navigate the quickfix list (or the location list, for that matter) is the unimpaired plugin.
Once the quickfix window is populated, [q and ]q go forward and back (respectively) in the quickfix list. [Q and ]Q go to the beginning and end (which is especially handy if you only have one item in the list; this makes vim complain about [q and ]q). So the workflow is:
Run whatever command populates the quickfix list
Type [Q to go to the first item
Scroll through subsequent items (if any) with [q and ]q
If you're using Syntastic, you'll get the location list instead of the quickfix list. No problem; just use [L, ]L, [l, and ]l in the same way.
unimpaired has loads of other handy mappings too -- [e and ]e "bubble" lines up and down, [<Space> and ]<Space> insert blank lines above and below, etc. I was surprised nobody mentioned it here before; that's probably because it didn't exist until January 2010, though the question was asked in 2009.
Put the following two lines in your .vimrc file:
map <C-j> :cn<CR>
map <C-k> :cp<CR>
Now you can navigate through the errors using ctrl-j and ctrl-k, which mimics the standard down and up motion commands j and k.
You can also use :cc 2 (or any other number) to jump to, in this case, the second error in the quickfix window. Navigating with :cn, :cc 4, etc will put the cursor on the line in question.
In addition to #DrAl's great answer about how to open and close the quick window and navigate between entries, I made an image to show some of the other quick fix navigation commands.
Each group of 3 files below represents a set of quickfix results, e.g. from a vimgrep. cnewer and colder are for going through historic result sets.
Maybe this option didn't exist when this question was written (or maybe I'm embarrassing myself because there's something in my .vimrc that makes this happen) but when I get a Quickfix List, I just navigate it with j and k then hit <CR> (i.e. the Enter key) to jump to that place in the file.
Then, to get back to the Quickfix List I type Ctrl+W j for "move down a window" and I'm back.
Finally, when I'm done, I just type :q, like I would to close any normal window.
The best-practice way of integrating JavaScript syntax-checking is using the Syntastic Vim plugin, which is using Vim's location-list (which is parallel to the quickfix) window.
I've written answers for this question and this question explaining how to do it, plus also how to get source-code browsing / tag-list for Vim using the community-driven jshint.com (which is way better than JSLint IMO) and Mozilla's DoctorJS (formerly jsctags).
the quickfix window is operated mostly like any other vim window: j down a line, k up a line, :cn to jump to the next error/warning, etc.
experiment!
Although this requires > Vim 7.4.858, the cdo (or ldo for location lists) command allows updating a non-contiguous set of lines in a way you could once only do with sed:
:vimgrep /re/ %:p
:cdo! norm #a
# or
:cdo! s/re/repl/
The above shows running a recorded macro or a simple search and replace. Missing seems to a be a way to pipe through and external command as you can with :range! command

Resources