How to open vim command output in separate tab/window? - vim

I'm using vim-rubytest to execute tests from within MacVim.
This prints output in vim's command output window.
The problems are that this output is not scrollable or disappears after i switch to editor.
Is it possible to send this output to separate tab/window in Vim?

I don't use vim-rubytest plugin but looking at the documentation it seems to me that by default vim-rubytest will put the contents into the quickfix list. To open the quickfix window issue the following command:
:copen
You can navigate the quickfix list via :cnext and :cprevious.
I belive the quickfix approach to be the preferred way, but to answer you question you can redirect the output into a register and then paste into a new buffer.
:redir #"
Then execute <leader>T. watch it all go by. Then end the redirection and create a new buffer with the contents inside.
:redir END
:new|pu|0d_
For more help see
:h quickfix
:h :cnext
:h :redir
:h :new
:h :put
:h :delete

The default on vim-rubytest is for let g:rubytest_in_quickfix = 0 this will execute everything on the terminal and echo the results.
In order to "open in a new window" you should set
let g:rubytest_in_quickfix = 1 and then the output will be run and applied to the quickfix. This will open up a separate quickfix window that you can use see all the failures and allow you to jump to the file.
There is an issue with the usage of quickfix that I have filed on github (Reference) that doesn't allow you to open the first error. In the issue I posted a possible fix and have my own fork with it applied + some other minor adjustments to make using quickfix more ideal in my own TDD workflow.

Related

why cscope does not show complete result list?

I run a cscope command cs f s free and get one of the 21 results but I can not see the whole results list, how to fix it?
I faced the similar issues while running :cs (vim cscope) commands from vim. The entire cscope list is actually in quickfix window.
To see the equickfix window (containing list), run :cope or :cw command inside from vim.
Remove option in cscopequickfix
:set cscopequickfix=
or comment the line in .vimrc containing cscopequickfix

How to open a terminal in new tab from VIM?

When I'm working with lets say 4 files, all are open in tabs(VIM). I want to save the changes and compile it without having to close the tabs, i.e I want to open a terminal in new tab along with the existing 4?
How should I do this in VIM?
:tab ter
opens a terminal in a new tab instead of opening it in a new window as :ter does. You can also use the equivalent, longer :tab terminal form.
Credits to user wolloda in this Reddit post.
Extra information
Terminating the shell with Ctrl-d or exit closes the terminal buffer.
Ctrl-w N or Ctrl-\ Ctrl-n put the buffer in the terminal-normal mode:
Keystrokes are not forwarded to the shell, but are used by Vim as in a normal buffer
(although the shell job is still running). Then you can use gt to change tabs,
type Ex commands such as :ls, etc. To bring the terminal buffer back to life,
i, a, ...
If you are going to map this, I recommend using :tab ter++kill=hup, so that when you :qa the terminal job does not prevent Vim from quitting.
And this is the signal normal terminal emulators send to its jobs when closed anyway.
For terminal mode mappings, use tnoremap, for example
tnoremap <S-Tab> <C-W>:tabprevious<CR>
tnoremap <C-N> <C-W>N
More information on :help :terminal and :help :tab.
In 2019, vim now has a Terminal mode.
:help terminal
For example, you can use it like this.
# go to terminal-job mode
:terminal
# go to terminal-normal mode
ctrl-w N
# go back to terminal-job mode
i
A more vim like way of doing this would be to use :make
:make
:make will execute the 'makeprg'. It defaults to make which is great of C projects
After running :make the quickfix list will be contain any errors.
Set your compiler via the :compiler command.
Extra parameter can be passed like so :make foo-command
Current filename can be represented by %. e.g. :make %
quickfix list
Use :cnext and :cprev to move between your errors.
:copen to open up the quickfix list in a window (:cclose to close)
:cwindow to open quickfix list window only if there are errors
May want to use better mappings for :cnext and friends. I suggest Tim Pope's unimpaired plugin
Alternatives and Plugins
Just use <c-z> to suspend vim and run your build system. (Cons: loose out on the quickfix list)
Use :! to compile. (Same cons as suspending) e.g. :!make
Syntastic is a syntax checking system that checks files on save
Dispatch can be used to run things in the background. Great for test suites
As #brettanomyces mentioned you may want to consider terminal multiplexers like tmux or screen.
SingleComplile tries and takes some of the work out of using :make
Conclusion
If you are just starting out I would suggest you learn how to use :make and the quickfix list. There is a nice Vimcast episode that intros the quickfix list: Search multiple files with :vimgrep. Additionally Syntastic is a great way to get up and running with linters quickly.
Aside about 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. Therefore using features like the 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+) use no tabs and use on average 1-2 splits without any issue. Bottom line: Learn to use buffers effectively.
For more help see the following:
:h :make
:h 'makeprg
:h quickfix
:h :cnext
:h :cope
Vim 8.1 now has a built in terminal that can be opened with the :term command. This provides much more complete integration with the rest of the Vim features.
Original Answer:
I would suggest looking at tmux or screen. I use tmux myself and along with vim-tmux-navigator moving between the terminal and vim is very easy.
For anyone using NeoVim:
The highest voted answer uses :tab ter. This doesn't work on NeoVim (at least for me). However, it's still fairly simple:
:tabe term://bash
tabe is open a new tab and edit file.
term:// is a NeoVim way of opening a terminal
bash is the kind of shell you want to use (e.g. I use zsh, so my command is actually :tabe term://zsh)
Some helpful commands that I created:
" open terminal
if has('nvim')
command Terminal vsplit term://zsh
command TerminalTab tabe term://zsh
else
command Terminal vert term
command TerminalTab tab ter
endif
Another way
Ctrl-w :
That gets me to the command line, then one can enter tablast tabnext or tabprevious
or the short versions tabl, tabn, tabp
Or this way:
Ctrl-w gt and Ctrl-w gT
(Next tab and Previous Tab)
Or Ctrl-w Number gt (for a specific tab)
That works too.

Cscope result handling with Quickfix window

I am using cscope with quickfix support to display the result by setting set cscopequickfix=s-,c-,d-,i-,t-,e- ~/.vimrc file
I can move between results with :cnext and :cprevious commands
What I want is to display the result in quickfix window first and do a preview before jumping to the file.
I tried using set switchbuf+=usetab,newtab but that gives a different result and not helping.
Is there a plugin or command available to get this behaviour.
I ran into the same issue and fixed it by closing the buffer it opens (1st result) and then opening the results in a quickfix window by calling :cwindow.
Here's what my key remap looks like:
nnoremap <leader>s yiw:cs find s <C-R>=expand("<cword>")<CR><CR>:bd<CR>:cwindow<CR>/<C-R>0<CR>
Explanation:
yiw- Yank the word under cursor for highlighting later
:cs find s <C-R>=expand("<cword>")<CR><CR>- Standard cscope command to find a symbol
:bd<CR>- Delete the buffer, so this closes the first search result
:cwindow<CR>- Open search results in a new quickfix window
/<C-R>0<CR>- Search for the symbol, so I get the symbol I looked up for highlighted in quickfix window and I can move within quickfix window using n and N.
I ended up writing a small plugin for this. You can check it out here - https://github.com/ronakg/quickr-cscope.vim

How can I open a help file in Vim on a new buffer in an existing window?

I often take a look at help files in Vim, but sometimes I want to read one in full screen. Since the :help command opens it in a new window, and closing the old window, if it was the only one besides of the help file, for some reason closes Vim, the only way I found of doing this was opening the help file, and then reopening it in a new tab.
I wondered, is there any way to make the :help command (or another command) to open a help file in the same window, but a new buffer?
You might be looking for :only or CTRL-W o (the same command). This makes the current window the only one on the screen. All other windows are closed.
You can also vertically split the help window with:
:vert help {subject}
BTW, :help actually does open in a new buffer, it's just "unlisted". To list all buffers, including the unlisted ones:
:buffers!
If I understand the question correctly, all you need to do is to chain the help command call with the only command:
:help <subject> | only
The :help will usually open a new window unless the active window's buffer buftype is already help. So to truly reuse a window you must open a new empty buffer in that window with :enew, change the buftype with :set buftype=help and then issue the :help <whatever>.
For convenience you could define a command to do that in your .vimrc:
command! -nargs=1 -complete=help H :enew | :set buftype=help | :h <args>
And then use :H {subject} from any window.
Using this method you truly reuse the window and that allows you to use C-^ to go to the alternate for example. It will also respect your window layout (split windows, etc) unlike the other answers.
You can open a new tab for help with :tab help. This will give you a full screen help. Also look at :help :tab.
You can use :help to open the help window, then Ctrl+W_ to make that window full screen (mostly, see the winminheight option).
To open full size new tab with your desired topic:
:tab help {subject}
:tab h {subject}
Subject is any valid :help argument.
To split the current window:
:vert help {subject}
:vert h {subject}
A more general variant of #Shamaoke's answer is to open the main help menu in a full window.
:help | only
I wrote a custom command using capital H like so (works exactly like :h except that it uses the whole window):
command! -nargs=1 -complete=help H call HelpFullScreen( <f-args> )
function! HelpFullScreen( topic )
exe "h " . a:topic
wincmd j
try
clo
catch /^Vim(\a\+):E444:/ " can't close last window
endtry
endfunction
Works like a charm!

Selecting resulting files from grep in vim

After I run a grep search in vim with :grep, I get a list of files. Is there a way to select one of those files and open it in a new tab at that particular line?
Just for completeness, as well as the :copen command, there's also :cw, which only opens the "quickfix" window if there are entries (so if your grep has no results, it won't appear).
I think the easiest way (without defining a mapping) of making the files open in a new tab would be to do:
:cw " Open the quickfix window
Ctrl-W T " Switch the window into a new tab
<ENTER> " Open the file/line
Alternatively, you could do:
:cw " Open the quick fix window
Ctrl-W <ENTER> " Open the file/line in a new window
Ctrl-W T " Move the new window to a new tab
If you want to do it by default, you could probably use the BufEnter and BufLeave autocmds to create and remove a mapping when entering and leaving the quickfix window; however, this is probably not trivial.
:help :cw
:help :copen
:help quickfix
For achieving what you want you have to open the quickfix/error window after calling grep:
:copen
I have a script that makes it for me every time i use grep.
I came upon this thread looking for an answer to a very similar question. The answer presented above, though correct, failed to describe a convenient way to open ALL the files in the QuickFix window at once ... into either buffers or tabs.
There doesn't seem to be a built in command to do it, but it's trivial as a VIM plugin ... somebody has done it here
http://pastebin.com/J9RwciFQ
It's 12 lines of code (one function) ... pasted here to save you a click during your analysis. Do follow the pastebin link if you are going to try to implement this though ... my plugin is installed in pathogen directory and I modified the plugin from the original slightly (details after code).
~/.v/b/v/p/quickfixopenall.vim
" Create command
command! QuickFixOpenAll :call StartQuickFixOpenAll()
function! StartQuickFixOpenAll()
if empty(getqflist())
return
endif
let s:prev_val = ""
for d in getqflist()
let s:curr_val = bufname(d.bufnr)
if (s:curr_val != s:prev_val)
exec "edit " . s:curr_val
endif
let s:prev_val = s:curr_val
endfor
endfunction
So once I have a grep result I'm satisfied with ... the plugin has a function :QuickFixOpenAll ... I had to modify the plugin as given (added the following line to the quickfixplugin.vim). And I renamed his given function StartQuickFixOpenAll ...
" Create command
command! QuickFixOpenAll :call StartQuickFixOpenAll()
Then you have all the files in the grep result open as buffers ... if you want to run any commeon operations such as find/replace you can prefix the regular command with the "bufdo" command which will perform your command in all ... in VIM type "help bufdo"
You can fairly trivially modify this plugin if you wan to use tabs ... it uses the commaned "edit" ... just replace that with "tabe" and :QuickFixOpenAll will open each result buffer in a new tab.
If you get a list of files you can browse them in a tree-like manner via
:cn
:colder
For more information
:help grep
and scroll to the bottom of the entry

Resources