Whenever I open a split or cycle through git's history with .:Glog my cursor always ends up at the top or bottom of the page. I always hit z. to align the text and I was looking for an automatic way of doing this.
Thanks to the link from #michaelberkowski I was able to figure out the following workaround for vertical splits:
" Open vertical split with identical configuration to parent
function! AwesomeSplit()
:let savex=winsaveview()
vsplit
:call winrestview(savex)
endfunction
nnoremap <Leader>vs :call AwesomeSplit()<CR>
For navigating buffers with :.Glog I use vim-unimpared from Tim Pope. The shortcuts I use -- ]q and [q -- are just aliases for :cnext and :cprevious so I'll see if I can create a patch for vim-unimpared using winsaveview() and winrestview().
Related
I am using ctags and I added map <C-]> :vsp <CR>:exec("tag ".expand("<cword>"))<CR> to my vimrc. However, this opens a new vertical split everytime. Is there a way to show tag definitions on vertical split without opening a new one everytime?
Update: I would also like to know if there is a way to use the ctag stack normally with this. That is, use ctrl + t to pop a location from the stack?
The following command achieves the result you're looking for:
:execute "vertical ptag " . expand("<cword>")
So, this mapping should also work:
nnoremap <C-]> :execute "vertical ptag " . expand("<cword>")<CR>
You might want to set 'previewheight' to a higher value.
Update
As an alternative solution and if you want to keep navigating in tags, then the following can be used:
function! FollowTag()
if !exists("w:tagbrowse")
vsplit
let w:tagbrowse=1
endif
execute "tag " . expand("<cword>")
endfunction
nnoremap <c-]> :call FollowTag()<CR>
Nevertheless, I think you should consider revising the need to create such a shortcut by taking the following standard Vim shortcuts into account:
<c-]> : Jumps to the tag definition of the word under cursor updating tag stack.
<c-w>} : Opens a preview window with the location of the tag definition. The cursor does not change its position, so tag stack is not updated.
<c-w>z : Close preview window.
<c-w>v : Split current window in two, keeping the cursor position.
So, you can use <c-w>}if you want to quickly check the tag declaration, followed by <c-w>z to close it. But if you want to navigate, then you can simply use <c-w>v to create a split followed by the standard <c-] to navigate in the tags. When you're done with it, you can simply close the window with <c-w>c.
The command below should do what you want, at least in a hack-ish way:
nnoremap <C-]> :only<bar>vsplit<CR>:execute "tag" . expand('<cword>')<CR>
Is there a way to switch in and out of NERDtree -- mouse disabled? I know I can press 'q' to quit NERDtree and go back to text editor, but when I go back to NERDtree it doesn't keep track of which directories I had pressed open before, and I would like to keep the list of directories open. I referenced NERDtree help, but I couldn't find what I was looking for. Perhaps I missed something?
Thanks in advance.
NERDTree opens as a sidebar next to the existing open windows. You can therefore use all Vim window movement commands to jump back and forth.
For example, <C-W>p (that's Ctrl + W followed by P) goes back to the previous window, and 1<C-W><C-W> goes to the first window (which usually is the leftmost split NERDTree). You'll find the whole list at :help window-move-cursor; for effective window handling, it's important you know these very well.
As a potential best practice , I map F2 key to toggle NERDTree.
map <F2> :NERDTreeToggle<CR>
let NERDTreeWinSize=32
let NERDTreeWinPos="left"
let NERDTreeShowHidden=1
let NERDTreeAutoDeleteBuffer=1
let NERDTreeAutoDeleteBuffer=1
And also, I map Ctrl+j/k/hl to quickly move between opened vim windows.
"Smart way to move between windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l
To switch between the NERDtree "window" and the text "window" with
Ctrl+w and then right arrow
and then to go back I use
Ctrl+w and then left arrow
For more info on "vim windows"
http://vimdoc.sourceforge.net/htmldoc/windows.html
I have noticed that when I use :cn :cp directly, vim always expands folds for me. But when I set my custom mapping to call :cn like :map <leader>n :cn<cr>, vim doesn't expand foldings when I use <leader>n in this case, I have to move horizontally after using that hotkey to unfold. I could script moving horizontally as well, but I still would like to understand why vim doesn't do that for me.
My foldopen setting contains quickfix.
Thanks.
According to this vim email archive.
This is the correct behavior and that you are supposed to open folds manually if you put :cn or :cp into mappings. The solution was to add zv to the end of the mappings.
So your maps should look like this
noremap <leader>n :cn<CR>zv
Note: I change map to noremap because noremap stops recursive mappings whereas map allows for it.
I have a lot of buffers open and to switch between then I Ctrl+w and then Ctrl+[movement key]. Is there a better way? Because this way when I've to do more than one movement I end up having to press 4 keys.
If you want to move between windows, you always can map Ctrl-W, H/J/K/L to anything you want:
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l
If you want to move between buffers, you can use :buffer command, print a part of the file name you want to go to: pressing Tab will cycle through all files that contains this part in the names. E.g you want to show buffer with filename 'some_long_c_file.c' and you know you don't have any other opened file whose name ends with 'file.c': just type :buffer file.c and press Tab (or even 'ile.c' or shorter), this will complete it to 'some_long_c_file.c'.
For quick switching between the two last buffers, use Ctrl-^
The best way I've found to do this is to install bufexplorer.vim. That let's you quickly switch to a screen in which you can select any buffer. You can also close buffers etc..
Here's a screenshot of my vim with bufexplorer open showing it's help screen:
For me the solution was two-fold:
1) Use unimpaired - so ]b and [b
2) map :b# to switch to previous buffer. I used map <cr> :b#<cr> . (so enter key would toggle back and forth between two open buffers).
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.