Automatically center vim search results - vim

When I do a search with vim or gvim, the resulting positioning of the cursor within the window is somewhat random, all too frequently landing on the last (or first) line in the window. Search highlighting helps, but it's still cumbersome to have to look all around on the screen to find the cursor ... and a bit ironic, that after vim locates the next result in some megabytes-long log file, I have to use the ol' neocortex to isolate it from the last 4K or so.
I can manually get the effect I want by hitting 'zz' after every search, but would prefer to doctor up my _vimrc to make this happen automatically.

Will this work for you ?
:nmap n nzz
:nmap p pzz

I use this trick with other commands too:
nnoremap n nzz
nnoremap N Nzz
nnoremap <C-o> <C-o>zz
nnoremap <C-i> <C-i>zz

An alternative to nzz and Nzz is probably
:set scrolloff=5
which keeps 5 lines below/above cursor in view.

As #Mike-B pointed out, the accepted answer of doing
nmap n nzz
does not open folds.
As an alternative I propose the following. It is a bit hacky, but seems to work for me:
nmap nzvzz
and equivalent for the other motions, if you want. zv should open all folds till the current cursor position is visible.

Related

How To Switch Between Buffers in vim by number

I'm using vim and i want to switch between buffers by number.
(e.g when i press Ctrl+2 vim should go to second buffer)
What should i write in .vimrc?
I would suggest:
" list buffers and jump to a chosen one
nnoremap <Leader>b :ls<CR>:b<Space>
The command :ls<CR> will list all opened buffers and :b[uffer]<space> allows you to type the number shown by the ls
I personally have something like this in my .vimrc:
map <F9> :bp<CR>
map <F10> :bn<CR>
As stated in the comments and :help keycodes there is no available mapping for the numbers 0-9.
I tried using map <C-k1> ... (as those are available in the keycodes), but it didn't work.
The answer depends on the approach of working with buffers.
I use the following three ways:
the buffers are showed in tabs;
the buffers are showed in windows;
the buffers can be hidden and the user switches between them using the current window.
For the first case, to switch between tabs, you may just type the buffer number and then hit the g and t keys. If it’s not suitable, make a map for the gt combination.
nnoremap <your_key_for_tab_switching> gt
For myself, I mapped the capital letters of the middle row, from “A” to “L”, for switching between tabs, but it may be not suitable for every user:
nnoremap A 1gt
nnoremap S 2gt
nnoremap D 3gt
...
nnoremap L 9gt
For switching between windows I prefer just the “next” and “prev” commands:
nnoremap <your_keys_to_the_next> <c-w>w
nnoremap <your_keys_to_the_prev> <c-w>W
To switch between opened but currently hidden buffers within a window you can:
nnoremap <your_shortcut_key> :buffer
" there is a space after “buffer”
That will be exactly you’ve asked for — “by numbers”. But it’s not a convenient way as I think. I prefer to switch between hidden buffers by short names but it’s not a standard feature.
There is yet another way — capital letters and digits for the marks. See help ma for more information.
And a little piece of advice: consider using letter-like keyboard keys instead of the number keys because they are placed much more closely to the fingers.

Quickly switching buffers in Vim normal mode

Recently I found out that I'm "using tabs incorrectly" in Vim. I've been trying to just use buffers in Vim since, assisted through MiniBufExplorer, but I find it painful because of how many keystrokes it takes to change buffers from normal mode. With tabs, I can just do gt or gT to hop back and forth between tabs in normal mode, and I can also do NUMBERgt to go to a specific tab.
With buffers, I either have to enter command mode with :bn, :bp, or with MiniBufExplorer, use Ctrl + k or Ctrl + Up to hop up to the buffer window, scroll left or right with h and l and then hit Enter to select the buffer I want. Or I can do something involving a leader sequence, but it always requires removing multiple fingers away from home row. That's a real pain.
How can I get something equivalent switching tabs in normal mode to switch buffers in normal mode, so I can do something like gn/gp for :bn/:bp and NUMBERgn for :buf NUMBER?
Add this to your .vimrc
map gn :bnext<cr>
map gp :bprevious<cr>
map gd :bdelete<cr>
" I personally use <leader>
map <leader>n :bnext<cr>
map <leader>p :bprevious<cr>
map <leader>d :bdelete<cr>
Note that you are remapping gp and gd, but maybe you don't care about that (:help gp, :help gd).
For more information on how to map key strokes see :help map-overview and :help map.txt.
Btw, I personally use <leader> instead of g in the mapping. My <leader> is set to ;. This puts my <leader> key on the home row which makes me willing to map all kinds of stuff using <leader>. :help mapleader if you want to change your <leader> key.
The way I usually switch between buffers is to use the :buffer command with the built-in autocompletion, e.g. :b prof<Tab> to switch to folder/path/LoginProfileFactory.php.
You can just start typing any part of the file name of the buffer you need, which is nice.
Less often, I actually remember the numbers of the buffers I want and I use something like :b 3 or :3b.
I see you mention you don't like :buf 3 though, so Rumple Stiltskin has an alternative to the :3b style that you may prefer.
{count}CTRL-^ switches to the count numbered buffer.
I have the following lines in .vimrc:
nnoremap <silent> <tab> :if &modifiable && !&readonly && &modified <CR> :write<CR> :endif<CR>:bnext<CR>
nnoremap <silent> <s-tab> :if &modifiable && !&readonly && &modified <CR> :write<CR> :endif<CR>:bprevious<CR>
Now a Tab let you go to the next buffer and a Shift-Tab to the previous.
This is based on Nick Knowlson's answer, but I wanted to expand on my comment there ...
Type :b <Tab> (note the space), then cycle through the open buffers with Tab or ← / →.
... which gets us closer to the Ctrl + Tab in all the other editors and browsers I use.
It's actually even better in some ways, you can then go backwards and forwards with ← / → arrows. It avoids the thumb + finger fu to type Ctrl + Shift + Tab to go backwards through the tabs in editors and browsers.
N.B. Shift + Tab just does the same as Tab
This is then actually something like Win + Tab in Windows 10, where once you first open up the window and you can then move around using the arrow keys.
Edit: I have two further tricks that I picked up for using buffers:
From this answer I have this in my .vimrc:
nnoremap <leader>bb :buffers<cr>:b<space>
it opens the :ls / :buffers command and pre-types the :b so that you just have to type the buffer number as you'll see a list with all the buffers and their numbers.
I also have
nnoremap <leader><tab> :b#<cr>
which toggles between the current and most recently used buffers, it's a bit like doing cd - when switching back and forth between directories
I use the plugin unimpaired.vim
it defines mappings [b and ]b that jump to the previous and next buffer in the list.
For jumping for a specific buffer the best option I know is the one you mentioned: :b<number>
If you go into another buffer you can came back quickly by typing <c-^>
Expanding on Rumple Stiltskin's answer, if you know that the file you want to get to is in buffer 4, for example, you can get there quickly with
4Ctrl-^
On my UK keyboard, I can actually do 4Ctrl-6, as explained in
:help CTRL-^
By the way, you can see the buffer numbers with
:buffers
or
:ls
I use LustyExplorer: I hit <leader>b to open a list of buffers then a couple of letters from the name of the buffer I want to open then enter. Easy.
But, you are not "using tabs incorrectly", you are using tabs the way you want. If it worked for you why go through the pain of unlearning your way to learn "the right way"?
I use F9 and F10 to move between the previous/next buffer with this mapping:
map <F9> :bprevious<CR>
map <F10> :bnext<CR>
For me this is the fastest way to switch buffers.
fzf.vim is another fast way to changes buffers using fuzzy matching. This plug-in ships with the default command:
:Buffers
which opens the list of all open buffers similar to :ls but a buffer can be (fuzzy) searched and selected.
Opening the buffer in the current window is through enter, but can can also be opened in a new split (h or v) or tab using ^X ^V or ^T respectively.
Noteworthy is also:
:Lines
Which allows to search through the content of all open buffers. This can be handy if you forget the name of a buffer but you know what it should contain.
Here is my solution:
" `<leader><Tab>` - next buffer;
nnoremap <silent> <leader><Tab> :bnext<CR>
" `<leader><S-Tab>` - previous buffer;
nnoremap <silent> <leader><S-Tab> :bprevious<CR>
" `_bufferNumber_ + <Tab>` - go exact the buffer number;
nnoremap <silent> <Tab> <C-^>
By the way, I use 'buftabline' plugin and set let g:buftabline_numbers = 1 to spread my buffer on the tabline.
I make it easier for myself:
In .vimrc :
nnoremap <leader>bf :buffers<CR>:buffer "<- Last spaces is necessary
For example, in normal mode, say your leader key is \(default it is), type \bf, then you have a list of opened buffers, type number of buffer you want and hit enter key.
NOTE: remember that last spaces not necessary at all if you wich type it after :D
Jut like that ;)
More detail:
<C-O> Navigate backward
<C-I> Navigate forward
So there is no need extra remapping, otherwise you remapped them.
I think bufexplorer is a nice plugin to use. <leader>be brings up an interactive buffer explorer that lists all open buffers. You could quickly move through the list and Enter puts you in the selected buffer. Unlike LustyExplorer It has no dependency to ruby.
I prefer navigating between buffers similarly to how I'm navigating between window panes: <alt-h> and <alt-l>. This is to straightforward to set on Mac because <alt>/<option> key binds are bounded to specific characters.
" Buffer navigation
map ˙ :bp<cr>
map ¬ :bn<cr>
map § <c-^>
Here is a good answer that shows how you can see characters maped to <alt-..> combinations

What is the best way to do smooth scrolling in Vim?

The main scrolling commands in Vim are:
Ctrl-B and Ctrl-F, as well as PageUp and PageDown scroll by full page
Ctrl-U and Ctrl-D scroll half a page by default
Ctrl-Y and Ctrl-E scroll one line
I lose visual context every time for the former two, so I have developed the bad habit of hitting the latter (Ctrl-Y and Ctrl-E) repetitively.
Since there is currently no first party support for smooth scrolling, what are the least objectionable workarounds/plugins?
I use both Vim and GVim depending on the task, and am happy to customize them separately if there is no one really good hack that works for both. The mouse scroll wheel works nicely in GVim, but I'm looking for keyboard based solutions.
Update: I have now pushed this code, refactored somewhat according to the guidelines at :help write-plugin, to a Github repo.
Using the Keyboard
Here is what I have in my .vimrc:
function SmoothScroll(up)
if a:up
let scrollaction="^Y"
else
let scrollaction="^E"
endif
exec "normal " . scrollaction
redraw
let counter=1
while counter<&scroll
let counter+=1
sleep 10m
redraw
exec "normal " . scrollaction
endwhile
endfunction
nnoremap <C-U> :call SmoothScroll(1)<Enter>
nnoremap <C-D> :call SmoothScroll(0)<Enter>
inoremap <C-U> <Esc>:call SmoothScroll(1)<Enter>i
inoremap <C-D> <Esc>:call SmoothScroll(0)<Enter>i
Features:
Scroll on the base of the Vim scroll option.
Customizable scrolling speed (adjust time argument of the sleep command; I use ten milliseconds). Note: just like slowing down the frame rate on a video, if you slow down the smooth scroll too much it will be jerky scroll, not smooth scroll. But whatever works best for you.
Works in normal or insert mode.
Note: all you copy-and-pasters, remember that the ^ character indicates a control character; copy-paste will produce invalid results and these must be entered manually!
^Y – CTRL-V then CTRL-Y
^E – CTRL-V then CTRL-E
However, the <C-U> and <Enter> style syntaxes are literally typed as those characters; the map command intelligently converts them to control characters.
Using the Mouse
The question mentions that scrolling with the mouse works well in GVim, but a keyboard solution is desired. This implies to me that the asker may be interested in a mouse solution if it works in regular terminal Vim.
For me, turning mouse support on allows smooth scrolling through the mouse wheel. Also, for me, smooth scrolling is most important when I am looking around (i.e. in normal mode), not when I am editing (in insert mode), and if I am not actively editing, the need for my hands to stay on the keyboard at all times is removed, so this works well.
On the basis of this question, though, it would seem that some people have to do some more manual setup beyond simply turning the mouse on (I just use set mouse=n):
My .vimrc has the following lines
set mouse=a
map <ScrollWheelUp> <C-Y>
map <ScrollWheelDown> <C-E>
There is a simple remap hack in vim's tips.txt:
Smooth scrolling *scroll-smooth*
If you like the scrolling to go a bit smoother, you can use these mappings:
:map <C-U> <C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y><C-Y>
:map <C-D> <C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E><C-E>
Shameless plug, but I created a plugin here that you can use to easily adjust the distance, speed, and duration of the scrolling animation: https://github.com/terryma/vim-smooth-scroll
A Recent Plugin
I have posted something similar here, but basically there is a great plugin that we can use now for scrolling, called terryma/vim-smooth-scroll. It provides a very nice and smooth scrolling.
The install is quite easy:
1 I use Vundle so I simply appended this in .vimrc.bundles :
Bundle terryma/vim-smooth-scroll
In the latest version of Vundle available today, 29th April of 2016, you can put this in your .vimrc:
Plugin 'terryma/vim-smooth-scroll'
2 As stated in the doc, you can set up 3 arguments: distance, duration and speed.
I am using this in my .vimrc file:
noremap <silent> <c-b> :call smooth_scroll#up(&scroll*2, 10, 4)<CR>
noremap <silent> <c-f> :call smooth_scroll#down(&scroll*2, 10, 4)<CR>
What I do is I set the keyboard repeat to very fast, about 120 chars / second, and the delay small.
Then I map to 4j and to 4k
I navigate up and down source code using j and k which moves the cursor up and down nice and quick, pretty smooth.
But here's the good part, and this works on Linux, not Windows.
For a number of years now, X11's keyboard input works in such a way that when you press and hold j it obviously starts putting out j characters. But when you then keep holding down j and then also press the ctrl key, X11 starts putting out c-j characters without you having to re-press the j key. Then when you let go of the ctrl key and still keep on pressing j, X11 continues to put j's again.
So j makes the cursor start moving nice and smooth downwards, and you can periodically hit ctrl without letting go of j to give it a boost, a jolt.
Also, I do what Devin does, and I set scrolloffset to 5.
Lastly, I swap ctrl and cap lock. The default position of the ctrl key is completely retarded (no offense intended). It makes you have to rotate your left hand. I almost never use caps lock, so I swap them. Then my left pink finger can reach the ctrl key without any yoga moves.
These things have worked for me for years. I only use vim, never gvim.
This isn't exactly smooth scrolling, but it's how I handle not losing context when jumping pages.
set so=7
'scrolloff' 'so' number (default 0)
global
{not in Vi}
Minimal number of screen lines to keep above and below the cursor.
This will make some context visible around where you are working. If
you set it to a very large value (999) the cursor line will always be
in the middle of the window (except at the start or end of the file or
when long lines wrap).
For scrolling horizontally see 'sidescrolloff'.
NOTE: This option is set to 0 when 'compatible' is set.
The Plugin cskeeters/vim-smooth-scroll supports smooth scrolling and requires no configuration. It supports to support smooth scrolling with zt, zz, and zb. It's a fork of terryma's plugin. Some of the open pull requests have been applied.
This combines many of these answers, and this is what I use.
noremap <expr> <C-u> repeat("\<C-y> :sleep 10m<CR>", winheight('%')/2)
noremap <expr> <C-d> repeat("\<C-e> :sleep 10m<CR>", winheight('%')/2)
N <CR-E>
N <CR-Y>
...where 'N' is the number of single lines you want to scroll.
Not smooth in literal sense, but you keep the keyboard.
This may be controversial for hardcore users, but... the best way to smooth scroll in Vim is... mouse wheel.
I just found this plugin called "accelerated-smooth-scroll" ("Vim plugin for accelerated smooth scroll (mapping to <C-D>/<C-U>, <C-F>/<C-B>)") which can be for example used through Vundle by putting this line in your .vimrc:
Plugin 'yonchu/accelerated-smooth-scroll'
Then by restarting Vim and running the :PluginInstall command, then again restart Vim and use the <C-D> (Ctrl+D) and <C-O> (Ctrl+O) commands normally.
Sadly, this plugin also moves the cursor instead of what I wanted: to just scroll the screen like the <C-E> and <C-Y> commands.
I slightly modified #Keith Pinson's code so that ctrl-f and ctrl-b can be mapped too:
function SmoothScroll(scroll_direction, n_scroll)
let n_scroll = a:n_scroll
if a:scroll_direction == 1
let scrollaction=""
else
let scrollaction=""
endif
exec "normal " . scrollaction
redraw
let counter=1
while counter<&scroll*n_scroll
let counter+=1
sleep 10m " ms per line
redraw
exec "normal " . scrollaction
endwhile
endfunction
" smoothly scroll the screen for some scrolling operations
nnoremap <C-U> :call SmoothScroll(1,1)<cr>
nnoremap <C-D> :call SmoothScroll(2,1)<cr>
nnoremap <C-B> :call SmoothScroll(1,2)<cr>
nnoremap <C-F> :call SmoothScroll(2,2)<cr>

Auto 'zz' in vim after a jump

After I make a jump to anywhere in the world, whether in the current file or a different file, is it possible to make vim automatically run zz (re-center on current line)?
I want this after things like search, ctrl-o and ctrl-i ... and pretty much any movement other than hjkl.
Thanks.
Voila:
" Center screen on next/previous selection.
nnoremap n nzz
nnoremap N Nzz
" Last and next jump should center too.
nnoremap <C-o> <C-o>zz
nnoremap <C-i> <C-i>zz

How may I scroll with vim into a big file?

I have a big file with thousands of lines of thousands of characters.
I move the cursor to 3000th character.
If I use PageDown or Ctrl+D, the file will scroll but the cursor will come back to the first no-space character.
There's is an option to set to keep the cursor in the same column after a such scroll ?
I have the behavior with gvim on Window, vim on OpenVMS and Cygwin.
CTRL-E - scroll down
CTRL-Y - scroll up
100<CTRL-E> will scroll down 100 lines for example
If you like using PageUp, PageDown or Ctrl+D etc. you can set the "nostartofline" option
:set nostartofline
Well, one easy way to do so is using the movement keys. For example, to go down 100 lines keeping the cursor at the same column: "100j"
Edit:
Alright, searching a little more to really find an answer to your question, there's an option to do exactly what you want:
:set nostartofline
A Recent Plugin
I am using terryma/vim-smooth-scrolling plugin as it provides a very nice and smooth scrolling.
To install, you can use Vundle:
Bundle terryma/vim-smooth-scrolling
As stated in the doc, you can set up 3 arguments: distance, duration and speed.
I am using this in my .vimrc file:
noremap <silent> <c-b> :call smooth_scroll#up(&scroll*2, 10, 4)<CR>
noremap <silent> <c-f> :call smooth_scroll#down(&scroll*2, 10, 4)<CR>
This might not suit your situation at all, but if it makes it easier to break the lines up you can break them at a certain character e.g. after ':'
:%s/:/\r&/g
Or every 80 characters
:%s/.\{80}/&\r/g

Resources