How can I get quickfix to open results in the previously focused window? - windows

I wrote the following command to search Vim's help files:
command! -n=1 -complete=help SearchHelp \
help | execute "grep! -i <args> $VIMRUNTIME/doc/ -r" | botright copen
Now, for example, when I have forgotten the key to increment the number under the cursor, I can do:
:SearchHelp increment
to search through the help files. (The answer turned out to be <Ctrl-A>.)
The problem is ... I have a few windows open, and I want quickfix to leave them alone. When I hit <Enter> in the quickfix list, I want it to open the result in that help window which I split open at the beginning of the command.
But it doesn't do this. It seems to prefer to always open the result in the bottom-most of all my editing windows, regardless of which window I was focused on, and with complete disinterest for the help window I opened. Obviously this moves away from whatever file I happened to be editing in that window (yes I have :set hidden), requiring an arbitrary number of <Ctrl-O> keys to get back to the file when I have finished browsing the help files.
So how can I get quickfix to open in the previously focused window (ideally that help window that was split open)?
(Incidentally, I sometimes use a patched version of Yegappan Lakshmanan's old grep.vim plugin, and somehow this does open :cope results in the window that had focus before I ran :Grep. I am not sure what's different in the two situations.)

The behavior (among others like :sbuffer) is controlled by the 'switchbuf' option. With a value of useopen, you can make it re-use a window if it already displays that help page. If you need more control than what is offered by that option, you do need to use a custom command / mapping.

So far what I have come up with is to replace that help command with:
99wincmd j | wincmd s
This splits whatever window happens to be at the bottom of the screen, so that when the quickfix clobbers the bottom one, I still have a window that keeps the file visible.
This solution is workable, but not quite as neat as my preferred result, which is to open results in the help window which was split just above the window I was focused on when I ran the command.

Related

Why is VS Code adding many blank lines at the end of all my files?

I just installed Win 10 1809 on my Dell PC, and it seems to run okay. But the first thing I did was start up VS Code. I wanted to run yarn start for my React project, but I had to type it way down in the terminal window, while the prompt was at the top. It worked though.
And when I open any project file, there seems to be approximately 100 blank lines appended at the end of each. But the line numbers don't go down that far and the cursor stops at the last line number. But the scroll bar acts as if those blank lines are there.
So I'm guessing the same number of blank lines are being added to the terminal too. But in the terminal window, the cursor is positioned at the bottom of the scrollable window.
What's going on? Is there a fix?
EDITED:
I see that I can scroll the file's window up until the cursor and last line is at the top, but no further, regardless of the window size. Maybe this has always be the case, and I never noticed. But now because of my terminal problem, I am suspicious of everything and checking for any abnormalities. So I don't think that extra blank lines are being added to my files. Sorry for the mistake.
But the terminal problem persists. The screen clip below shows the terminal display after I enter "yarn start". The typed input shows up way down from the initial prompt, and the blank area is a long string of "0D0A" characters. Sometimes I can click on "kill Terminal" and then start a new one and the new one works correctly. But not always.
Still trying to figure this out.
Can you share snapshots for your problems?
This is some hint for your problem
In VS Code, type
Linux / Windows: Ctrl + Shift + P
MacOS: Cmd + Shift + P
Type Settings to go Settings
At settings, search end of file, at here you can configure something for your problem with auto append blank lines each file
I hope it will help you :)
Your Terminal Problem is described as Bug in VS Code Issues.
As of July 2019 the intergration is pending due to stability problems.
https://github.com/Microsoft/vscode/issues/57803
Addendum (August 2019):
The Problem seams to be fixed with VS Code 1.37.1 and Windows 10 Patchlevel 1903
Did you try uninstalling and reinstalling?
Going back to your questions.
What's going on? No idea.
Fix. Use this extension https://marketplace.visualstudio.com/items?itemName=rintoj.blank-line-organizer
or
Open Visual Studio project and collapse all the folder and make it handy.
Now press ā€œ Control + Shift + H ā€œ key combination and you can see the Popup which shows Find and Replace options.
In the Find place input ā€œ^&\nā€ combination and select Use Regular Expression checkbox without fail as this input will mainly work with the regular expressions only.
In the replace field leave it empty so nothing will be replaced rather remove the empty blank lines.
In the input Find field we have give ^ for Start of the line and $ for end of line and \n which is for new line break.
source for second solution: http://www.f5debug.net/post/2015/01/03/How-to-remove-Blank-lines-between-codes-in-Visual-Studio-Code-editor.aspx
The extension will be useful overall especially when working in a team project where a lot of people might be leaving too many empty lines. Good luck

Can I make emacs grep windows just use the other window to open files in?

I've got emacs in front of me.
I've run a find-grep, and it's got many hits, which are displayed in a window. The file names are displayed in green as hyperlinks.
I make that the only window, with C-x 1.
If I click on a file name, the window splits vertically, and the file with the found text is displayed in the other window.
If I click on further filenames, then the new file replaces the old file, which is what I want to happen.
So far, so good...
However if I resize the windows, then emacs will periodically (when I click) split one of the two windows again, rendering the display difficult to read. It will then cycle opening new files between the two new windows. Occasionally it will open more windows and make the situation worse. If I close any of these new windows they just get reopened again.
In fact sometimes this perverse behaviour happens even if I don't resize anything. It just seems to happen more often if I do.
I would like emacs to stop buggering around and just have one find-grep window and one 'display' window, and always replace the display window with the new file. I would also like to be able to set these windows to the sizes that seem most convenient.
Is there any way to achieve this?
Or can anyone point me to an essay on how the whole (replace the contents of this window/replace the contents of a different window/create another window by splitting) thing works, so I can go and hack it sane.
Short fix:
Try doing this
(setq split-height-threshold nil
split-width-threshold nil)
This will prevent Emacs from splitting windows automatically (horizontally or vertically). This might be undesirable in other situations, but this should do the job. Try it for a week or so and see if it disrupts your flow.
Also, I found that if the point was in one of the windows, and I clicked on a link, the file opened up in the next window (if any).
So, if you want to make the file open in the right window (when you have more than one window), you can ensure that the point is in the window before the window you want.
Longer answer:
OK. I was able to reproduce the problem. The thing is the window showing the files is pretty big (wide or tall) because you resized it and Emacs sees that the width or height is greater than the respective threshold and splits it likewise. So, we have to either make the threshold higher or disallow the behaviour completely.
And, just to answer the last few questions:
To get current window - (selected-window)
To get next window - (next-window)
To select a window - (select-window foo-window)
To get the buffer of the current window - (current-buffer)
To get the buffer of some window - (window-buffer foo-window)
To set a buffer for a window - (set-window-buffer foo-window bar-buffer)
I'm sure you can hack together decent window/buffer management functions using these functions.
You can use C-h f to get more details on each of these functions.
Also check out the Elisp manual - http://www.chemie.fu-berlin.de/chemnet/use/info/elisp/elisp_26.html

How do I switch tabs based on incremental search in vim?

For example, let's say I have three tabs open in vim:
1: nice_program.c
2: something_fun.h
3: super_script.sh
So if I hit some magic modifier key, and then type 'n' and hit enter I change tab to tab 1. Likewise, typing 'su' instead will navigate me to tab 3 instead.
Is such behavior possible? There are so many vim extensions, and I dont really get the whole vim extension lingo.
BTW, I am using gVim on XP and MacVim on OS X. Preferably the solution will work on both...
EDIT:
Note that I only want the incremental search to search across the names of the open tabs. That is, it's not supposed to actually search inside the tabs themselves.
Also, I never use buffers, it's tabs that I want this working for.
From the wording of the question it seems that you take the idea of tabs in
Vim not the way it is supposed to be taken by design of this feature. A Vim
tab page is not a form of a buffer or a window, it is a window layout
container, instead. No wonder there is no built-in way for switching to a tab
by the name of a buffer that is active (or the only one in its tab page, or
special in some other way). Semantically, that is switching to a buffer, not
a tab (but tab could be switched in order to show a buffer, if it is
necessary).
To switch to a buffer by its name use the :sbuffer command (:sb, for
short). It is not necessary to type the whole buffer name each time, since
the command has auto-completion. Usually one have to type only few letters of
a name to uniquely identify a buffer (the same way as you described
incremental search in the question).
By default, Vim open the requested buffer displacing one in the current
window. This behavior is governed by the switchbuf option. One of the
choices (called usetab) provided by that option allows to switch to a window
in another tab page if that window contains the buffer to edit. This is
exactly what suits your manner of work with tab pages.
To summarize, change the switching behavior as follows
:set switchbuf=usetab
and use the :sb command to open a buffer by typing a few letters of its name
and using Tab-completion.
I use this snippet I picked up in vim wiki to switch between open buffers (mapped to F5):
" switch between numbered buffers
:nnoremap <F5> :buffers<CR>:buffer<Space>
(put in your .vimrc file or whichever dotfile you use).
As for incremental search across open buffers, whenever I look up something using either /[something] or with */# on current word, it's automagically also highlighted in other buffers/tabs. Then I can switch buffers and hit n or N to move between matches in the currently viewed buffer. That's already baked into Vim.
Hope that helps.
The :set switchbuf=usetab solution given by ib never worked for me for whatever reason (even without loading plugins or my .vimrc) but :tab drop name-of-file works just the way you want (I found it on the Vim wiki).
Make it a custom mapping to save a few keystrokes with nnoremap <leader>t :tab drop.
Also I second ib's comment on the right and wrong way to use tabs in Vim.

How to use vim in the terminal?

How does one setup and start using vim in the terminal on OS X?
I want to start writing my C code using vim in the terminal rather than a separate text editor. How does one get started on this?
The basics like: opening, creating, saving files via terminal using vim and writing code using vim. Also, does one compile directly using vim in the terminal?
Get started quickly
You simply type vim into the terminal to open it and start a new file.
You can pass a filename as an option and it will open that file, e.g. vim main.c. You can open multiple files by passing multiple file arguments.
Vim has different modes, unlike most editors you have probably used. You begin in NORMAL mode, which is where you will spend most of your time once you become familiar with vim.
To return to NORMAL mode after changing to a different mode, press Esc. It's a good idea to map your Caps Lock key to Esc, as it's closer and nobody really uses the Caps Lock key.
The first mode to try is INSERT mode, which is entered with a for append after cursor, or i for insert before cursor.
To enter VISUAL mode, where you can select text, use v. There are many other variants of this mode, which you will discover as you learn more about vim.
To save your file, ensure you're in NORMAL mode and then enter the command :w. When you press :, you will see your command appear in the bottom status bar. To save and exit, use :x. To quit without saving, use :q. If you had made a change you wanted to discard, use :q!.
Configure vim to your liking
You can edit your ~/.vimrc file to configure vim to your liking. It's best to look at a few first (here's mine) and then decide which options suits your style.
This is how mine looks:
To get the file explorer on the left, use NERDTree. For the status bar, use vim-airline. Finally, the color scheme is solarized.
Further learning
You can use man vim for some help inside the terminal. Alternatively, run vimtutor which is a good hands-on starting point.
It's a good idea to print out a Vim Cheatsheet and keep it in front of you while you're learning vim.
Run vim from the terminal. For the basics, you're advised to run the command vimtutor.
# On your terminal command line:
$ vim
If you have a specific file to edit, pass it as an argument.
$ vim yourfile.cpp
Likewise, launch the tutorial
$ vimtutor
You can definetely build your code from Vim, that's what the :make command does.
However, you need to go through the basics first : type vimtutor in your terminal and follow the instructions to the end.
After you have completed it a few times, open an existing (non-important) text file and try out all the things you learned from vimtutor: entering/leaving insert mode, undoing changes, quitting/saving, yanking/putting, moving and so on.
For a while you won't be productive at all with Vim and will probably be tempted to go back to your previous IDE/editor. Do that, but keep up with Vim a little bit every day. You'll probably be stopped by very weird and unexpected things but it will happen less and less.
In a few months you'll find yourself hitting o, v and i all the time in every textfield everywhere.
Have fun!
if you want to open all your .cpp files with one command, and have the window split in as many tiles as opened files, you can use:
vim -o $(find name ".cpp")
if you want to include a template in the place you are, you can use:
:r ~/myHeaderTemplate
will import the file "myHeaderTemplate in the place the cursor was before starting the command.
you can conversely select visually some code and save it to a file
select visually,
add w ~/myPartialfile.txt
when you select visualy, after type ":" in order to enter a command, you'll see "'<,'>" appear after the ":"
'<,'>w ~/myfile $
^ if you add "~/myfile" to the command, the selected part of the file will be saved to myfile.
if you're editing a file an want to copy it :
:saveas newFileWithNewName
If you want to learn by reading yourself:
Open MacOS terminal app.
Write this and press enter -> vimtutor
For quit write this and click -> :q

Vim Question Regarding Views

I have been reading a lot of the questions here on vim. I can't locate something that I want to do with vim but I am sure its possible.
I like vim(I am still new at it) using tabs and I have adjusted my vimrc so that H & L keys take me back and forth between tabs.
I was hoping to find a way to be able to use tab commands to open up a tab as output, so that if I am writing something in my case Ruby and I want to test it I could run it and flip to a new tab with the output. Or flip a tab to an interactive console to test code. This is possible?
As an aside is it possible to expand tabs to views so if I had two tabs open say script and output I could :spx or similar and have tabs come to split screen.
You can load the ruby output into a vim window in a couple of ways. One way would be to open a new split window and then load output of command:
" vimscript command to open new split window
wincmd n
" run ruby command and insert output at line 1
1,1!ruby foo.rb
alternatively you could get view the output on a new tab with a single window:
tabnew
1,1!ruby foo.rb
I know it's not the answer you want, but use buffers and ditch the tabs. I have a screencast going over how to use splits to manage your window.
http://lococast.net/archives/111
As for the ruby output, that seems like it might be covered here:
https://superuser.com/questions/133886/vim-displaying-code-output-in-a-new-window-a-la-textmate
In answer to your question about opening up a tab as output, I don't think there's any built in way to do this. There's the RunView plugin (see my answers to this question and this one), but I don't think that it supports using a separate tab (it works with a split window: what I think you refer to when you say 'view').
Regarding an interactive console: no, this is not possible. See :help design-not.
As for general use of Vim, try to get used to the idea of buffers and the fact that each 'view' (my term), whether it be a split window, a tab or whatever is just a means of looking at a particular buffer. You can have multiple views on a single buffer, so you can have a source file vertical split with two headers in one tab and the same source file vertically split with another header and a different bit of the same source file in another tab. This is very powerful once you get used to it. The Ctrl-W keyboard shortcuts are your friend (e.g. Ctrl-W, h to go left one window).
As for changing a tab into a split window, I don't think there's a direct way to do this (how would Vim know which tabs you wanted to join?). You can break a split into a tab with Ctrl-W + T, but to go back you'd have to create a couple of mappings. This is off the top of my head but something like this might work:
command! TakeThis let takebufnr = bufnr("")<CR>
command! SplitTaken exe 'split #' . takebufnr<CR>
nmap ,t :TakeThis<CR>
nmap ,s :SplitTaken<CR>
Then press ,t on the buffer you want to grab and ,s on the one you want to split with the 'taken' buffer.

Resources