When I switch buffer and then go back to it (when I get back to any buffer that was previously opened), cursor is placed in the middle of the screen, loosing previous screen position (e.g. cursor at top of the screen). Maybe this is the normal behaviour of vim, but is there any way to fix this?
This problem happens when I use :bn and bp: to switch buffers. However this behaviour does not happen when switching between tabs which is really strange.
See http://vim.wikia.com/wiki/Avoid_scrolling_when_switch_buffers
" When switching buffers, preserve window view.
if v:version >= 700
au BufLeave * if !&diff | let b:winview = winsaveview() | endif
au BufEnter * if exists('b:winview') && !&diff | call winrestview(b:winview) | endif
endif
Related
I love the linters and the quickfix window, but on occasion I don't want to fix anything and simply want to exit the file. The current issue is that when I close the file, the quickfix window stays open. I know I can use :qa to exit both at the same time, but I often forget to.
I spent a few hours trying to figure out how to close the quickfix buffer if it was the only open window/buffer left, but had no luck.
Anyone else know how to handle this better?
That feature is part of my vim-qf plugin but you don't really need a full-fledged plugin for such a mundane task. Actually, there have been many snippets floating around the internet for many years. Here is one:
augroup MyGroup
autocmd!
if exists('##QuitPre')
autocmd QuitPre * if &filetype != 'qf' | silent! lclose | endif
endif
augroup END
Basically, it closes the current quickfix/location window if it is the last remaining window in the current tab page.
Reference:
:help :augroup
:help :autocmd
:help exists()
:help QuitPre
if version >= 700
" automatically close quickfix if it's the only window left
autocmd WinEnter * if winnr('$') == 1 && &buftype == "quickfix" | quit | endif
endif
NERDTree has :NERDTreeFind to find the current file in the tree.
How can I make NERDTree update the current file in tree automatically whenever I switch files?
This should do it, but be aware that the frequent updates make switching buffers quite slow:
:autocmd BufEnter * if &filetype !=# 'nerdtree' | NERDTreeFind | wincmd p | endif
The conditional avoids triggering a find if the NERDTree sidebar itself is entered.
I use iTerm2 (Build 1.0.0.20130319) and Vim (bin of MacVim Snapshot 66) on top of OS X 10.7.5 as my CLI editing team.
In iTerm2 I defined to use a non-blinking vertical bar as a cursor shape.
In Vim I defined
" Enter insert mode (Cursor shape: vertical bar)
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
" Leave insert mode (Cursor shape: block)
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
to be able to distinct between insert and normal mode. Basically this works fine. The problem arises when I leave Vim and return to the CLI. What happens is that the cursor does not return in its initial shape (vertical bar). Instead it decides to stay in a block shape.
Could I reset the cursor to it's initial shape or force it to return to be a vertical bar? I Could imagine to trigger an event on e.g. "VimLeave". But I don't know what I could pass as an escape sequence.
After a little bit more digging into :help I found out that
autocmd VimLeave * let &t_me="\<Esc>]50;CursorShape=1\x7"
would revert the cursor shape to its initial, or let's say a defined, shape.
That works great so far.
Does anyone know downsides of that approach? Besides VimLeave one could also trigger VimLeavePre or QuitPre.
EDIT:
Even better there's a new plugin available which does exactly what some people are looking for.
https://github.com/jszakmeister/vim-togglecursor
It's a little bit configurable, too:
let g:togglecursor_default = "block"
let g:togglecursor_insert = "line"
let g:togglecursor_leave = "line"
let g:togglecursor_disable_tmux = 0
Did you try this?
autocmd VimLeave * let &t_SI = "\<Esc>]50;CursorShape=1\x7"
I'm using Iterm with regular vim. This sets cursor solid on launch vim, blinks on insert mode. And returns to blinking on exit vim.
augroup myCmds
au!
autocmd VimEnter * silent !echo -ne "\e[2 q"
autocmd VimLeave * silent !echo -ne "\e[1 q"
augroup END
I tried this method before because I was used to Gvim's cursor style when switching to terminal. But I met the same problem and had no way to fix it.
Finally I have been using changing Cursor colour method. Once I got used to the style, I'm quite happy it and forget the cursor shape at all.
Change Cursor colour method
autocmd InsertEnter * set cul
autocmd InsertLeave * set nocul
Then set similar but different colour for Cursor than Normal in the theme if there is no built-in scheme in this theme.
Source: http://vim.wikia.com/wiki/Configuring_the_cursor
My sum of three methods for identifying insert mode in terminal:
How to make cursor change in different modes in vim?
Normally when quickfix window opens it changes the screen layout, but Vim restores
it when that window is closed.
But there is a situation where the layout restoration fails: when the
preview window is open, vertical splits are presents and :wincmd J is executed
in quickfix (or it is opened with :botright copen). In this case the size of
preview window is changed.
I came with a solution which I placed on ~/.vim/ftplugin/qf.vim,
" Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
finish
endif
" expand quickfix when there are vertical splits
wincmd J
func! RestorePreviewWindow()
let l:quickfixHeight = winheight(0)
wincmd p " include previous window on jump list
silent! wincmd P " jump to preview window
if &previewwindow " if we really get there...
exe "resize " . (&previewheight - l:quickfixHeight - 1)
wincmd p " back to old window
endif
endfunc
augroup quickfixClosing
au!
au BufDelete <buffer> call RestorePreviewWindow()
augroup END
, but I was wondering if there is some better/simpler solutions to this
problem.
If you can reproduce the problem in plain vanilla Vim (vim -N -u NONE), I'd report it to the vim_dev mailing list to have it fixed inside Vim. The preview window should not change its size when other, normal windows could stand in for it.
If this is just a peculiarity of your setup, I think your implemented workaround is fine; I would probably solve it along the same lines.
I was with this problem and I've tried your proposed qf.vim but it didn't work. I found something that did, in the qf help page, =|, so put this in your .vimrc:
au FileType qf botright cwindow
I used your answer to improve the default auto resize behaviour of vim.
It's not really an answer to this question but hopefully other people might find it useful as I stumbled across this question for that reason:
nmap <silent> <C-w>= :call ResizeAllWindows()<cr>
function! ResizeAllWindows()
call RestorePreviewWindowHeight()
wincmd = "set all equal after restore
endfunction
function! RestorePreviewWindowHeight()
silent! wincmd P "jump to preview, but don't show error
if &previewwindow
exec "resize" &previewheight
wincmd p "jump back
endif
endfunction
how do I make vim horizontally center the text of the open file?
I don't want to modify the file, just to change the way vim displays it.
To be more clear, when I open a file I currently have this situation:
|<------ textwidth=80 ------->|<-------------- padding -------------->|
|lorem ipsum dolor sit amet..
|dsdsda da dsa dsa
What I'd like to have is the following:
|<--- padding/2 --->|<------ textwidth=80 ------->|<--- padding/2 --->|
| lorem ipsum dolor sit amet..
| dsdsda da dsa dsa
Of course, for every value of textwidth and padding.
Vim isn't meant to be a single, centered document editor (when programming, you want to fill every single pixel with relevant information), so there are only workarounds:
a) You can achieve a larger left margin by expanding the fold column. Unfortunately, this is limited to 12 character cells:
:let &foldcolumn = (&columns - &textwidth) / 2
b) You can create an empty padding window to the left (and potentially also to the right, for symmetry).
:execute 'topleft' ((&columns - &textwidth) / 2 - 1) . 'vsplit _paddding_' | wincmd p
The annoying window split can be cleared with:
:hi VertSplit guifg=bg guibg=NONE gui=NONE
I think you could reach the point using screen.
you can open three different windows tiled vertically with
CTRL+a , SHIFT+|
then move to the second one.
Exist several plugins that allow you to get that look, most of them seem to be inspired by Writeroom editor for MacOS.
See for example this screenshot of the the VimRoom plugin, or just search the web for "vim writeroom plugin".
I use NERDTree for that reason. NERDTree is a file explorer for vim. You can load NERDTree at startup with any width you like:
This is how I run NERDTree at startup to get what you see in the image. I use vim-plug (a package manager for vim) to load NERDTree.
# Load NERDTree Plugin via vim-plug
call plug#begin()
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
call plug#end()
let g:NERDTreeWinSize=60 # Set NERDTree width.
NERDTreeToggle # Open NERDTree at startup.
au VimEnter * wincmd l # Remove focus from NERDTree window at startup.
I was looking for something similar and thanks to #ingo-karkat answer I managed to create a function to toggle the center content on/off.
function! WriteRoomToggle()
let l:name = '_writeroom_'
if bufwinnr(l:name) > 0
wincmd o
else
let l:width = (&columns - &textwidth) / 5
execute 'topleft' l:width . 'vsplit +setlocal\ nobuflisted' l:name | wincmd p
execute 'botright' l:width . 'vsplit +setlocal\ nobuflisted' l:name | wincmd p
endif
endfunction
Suggested options and mappings:
" hide vertical split separator
hi VertSplit guifg=bg guibg=NONE gui=NONE
" Do not resize windows on close (good if you also use NERDTree)
set noequalalways
" toggle writeroom on/off
map <silent><Leader>v :call WriteRoomToggle()<CR>
" delete buffer without closing the window
command! Bdelete if len(getbufinfo({'buflisted':1})) > 1 | bprev | bdelete# | else | bdelete | endif
noremap <Leader>x :Bdelete<CR>