I use NERDTree and have it set to automatically open when I open vim, but that causes the NERDTree window to be the one selected, which isn't what I want. I want to be able to go right into editing the file I opened, but autocmd vimenter * <c-w><c-p> just gives me an error on startup. Is there any way to do what I'm wanting to do, or is it impossible?
I'm not completely familiar with Vim and its configuration files, so excuse me if this is a simple question, but I haven't been able to find it anywhere.
:autocmd's execute ex-commands not normal mode commands. This means you can use :normal! or use :wincmd (method I prefer).
Your new command:
autocmd VimEnter * wincmd p
Note: wincmd p is the same as <c-w>p or <c-w><c-p> in normal mode.
For more help see:
:h :wincmd
:h :normal
:h :au
Related
Since I've started using Vim tabs (I'm a bit late to the game since they've been there since 2009!), I find I'd like most thing to default to tabs, so I always open files with vim -p file1 file2 etc. I would like :help / :h to always open in tabs also. I can do :tab help ctrl-w or :tab h ctrl-w but how can I use .vimrc to always default :h or :help to open in a new tab?
Additionally, is there a way to take an open file and push it into a tab, or to tell vim to take all open buffers and dynamically shift them all into tabs?
"take an open file and push it into a tab"
You can do that with this normal mode command:
<C-w>T
or this equivalent Ex command:
:wincmd T
See :help ctrl-w_t and :help :wincmd.
":h to always open in tabs"
You only need to put the aforementioned Ex command in after/ftplugin/help.vim:
wincmd T
You can use autocmd to always open help in a new tab
" This won't work if you open the same help file more than once
autocmd FileType help wincmd T
" This works
autocmd BufEnter *.txt if &filetype == 'help' | wincmd T | endif
Or you can define a new command to only open it in a new tab, when in a small screen or there are many to read, for example.
With the following, you can use :Tabhelp ....
function! Tabhelp(arg)
execute 'tab help '.a:arg
endfunction
command -nargs=? -complete=help Tabhelp call Tabhelp(<q-args>)
How to autoupdate search vimgrep results from quickfix window after file is autoudpated due to change?
I use :vim/pattern/% to search and put result in quickfix window.
You can always do <up> on the command line with a prefix of :vimgrep to make searching history easier. You may also want to check out q: and the use ?// to search for the command to re-execute.
Assuming the quickfix title is set correctly, you can use following the command:
:execute get(getqflist({'title':1}), 'title')
This however I am not certain this will work with all :grep/:make commands. You also need a newer Vim version to get the Quckfix list title (Vim 8+ I think).
If you find yourself doing this often you may want to bind a mapping or command.
command! -nargs=0 -bar QFRefresh execute get(getqflist({'title':1}), 'title')
Now how to add do this automatically? We can use FileChangedShellPost autocmd to run our QFRefresh command once a file change has been detected. Add the following to you vimrc file:
augroup QFRefresh
autocmd!
autocmd FileChangedShellPost * if get(b:, 'qfrefresh_auto', 0) | QFRefresh | endif
augroup END
command! -nargs=0 -bar QFAutoRefreshToggle let b:qfrefresh_auto = !get(b:, 'qfrefresh_auto', 0) | echo b:qfrefresh_auto ? 'Auto Refresh' : 'No Auto Refresh'
Now you can use :QFAutoRefreshToggle to toggle refreshing a file.
Note: As stated before QFRefresh uses the quickfix's title to get the quickfix command. If the title is not set correctly a refresh may not work correctly. Also I am not sure what guarantees Vim has on triggering FileChangedShellPost. You can force a check via :checktime command.
For more help see:
:h getqflist()
:h :execute
:h :get
:h q:
:h cmdwin
:h c_Up
:h FileChangedShellPost
:h :checktime
I've got these bindings in my .vimrc. They work most of the time, but sometimes they don't: they will just save the file but not run it.
Then I go into insert mode and exit it back to normal mode, and they work again. What could be the problem?
Thanks!
autocmd FileType python map <C-k> :write <CR> :! python % <CR>
autocmd FileType lisp map <C-k> :write <CR> :! clisp % <CR>
autocmd FileType scala map <C-k> :write <CR> :! scala % <CR>
There are many topics to discuss here so with out further ado:
Mappings
Your current mappings map <c-k> ... will only work in normal, visual, and operator-pending modes. However executing your mappings in visual mode or operator-pending modes would save the buffer with only a range of lines (read not good). I suggest you make your mappings for normal mode only.
Two general rules of thumb:
Always supply a mode like n for normal.
Always use noremp instead of map unless you are mapping to a <Plug> mapping.
So one of your mappings might look something similar to this:
nnoremap <c-k> :w<cr>:!python %<cr>
For more information:
:h :map-modes
:h map-overview
:h :nore
:h map-listing
:h map-verbose
Filetype based mappings
You need to do 2 things:
Create a mapping local to a specific buffer by using the <buffer> option for noremap.
Load the mappings for just a specific filetype.
This can be done via an autocmd and FileType event in your .vimrc like so:
autocmd FileType python nnoremap <buffer> <c-k> :w<cr>:!python %:p<cr>
The other way option is by creating a filetype plugin. (see :h ftplugin for more details)
A simple example is do create a file named, ~/.vim/ftplugin/python.vim and place your mappings inside like so:
nnoremap <buffer> <c-k> :w<cr>:!python %:p<cr>
I personally lean more towards the ftplugin approach but having a everything in your .vimrc file can be nice.
For more help see:
:h :au
:h FileType
:h map-local
:h ftplugin
:make
A more vim like way of doing this would be to use :make. (Assuming you want to lint vs execute your current buffer)
: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 %
Often people set do :complier/'makeprg' in side of ftplugins e.g. ~/.vim/ftplugin/perl.vim or autocmd's e.g. autocmd FileType perl compiler perl.
Fop more help see:
:h :make
:h 'makeprg'
:h :compiler
:h c_%
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
For more help see the following:
:h quickfix
:h :cnext
:h :cope
Alternatives to using :make
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
Ale (Asynchronous Lint Engine) is a plugin for providing linting in NeoVim and Vim 8 while you edit your text files
Dispatch can be used to run things in the background. Great for test suites
May want to consider terminal multiplexers like tmux or screen.
SingleComplile tries and takes some of the work out of using :make
Conclusion
Personally I would install ALE as this removes the need for your mappings. It is also a great idea to learn how to use the location (or quickfix) list. When you are ready to get your hands dirty then you can learn and use :make.
tl;dr
Install ALE.
The space inserted between <CR> and : could lead to errors, because this space is really entered during the execution of the mapping.
With :noremap or :nnoremap (which should be used most of the times, as Peter Rincker wrote in his answer), it wouldn't be a big problem, since it only would move the cursor one char to the right (see :h <space>).
But with :map, it could trigger a custom mapping (or a partial one).
Then, instead of:
autocmd FileType python map <C-k> :write <CR> :! python % <CR>
the following would be better:
autocmd FileType python map <C-k> :write <CR>:! python % <CR>
and this one, really better:
autocmd FileType python nnoremap <C-k> :write <CR>:! python % <CR>
I currently have the following in my .vimrc:
au VimEnter * NERDTree
au BufWinEnter * NERDTreeMirror
This I believe launches NERDTree when I open Vim, as well as when I open a new tab using a plugin. Currently, when I launch Vim using
vim
at the command line, the cursor is sitting in NERDTree ready for me to navigate my files, which is great. However, when I use
vim my-file.txt
the cursor remains in the NERDTree window. I'm aware that I can add this to my .vimrc:
au BufNew * wincmd l
But that means the cursor will always be placed in the window to the right of NERDTree, even if I don't specify a file.
Does anyone have any ideas?
You can make the command conditional on whether arguments were passed to Vim:
:au VimEnter * if argc() > 0 | wincmd l | endif
(I'd use the VimEnter event for that; just make sure this autocmd comes after the one that opens NERDTree.)
PS: There are a couple of related questions here:
Auto-open NERDTree in "EVERY" tab
How do you add NERDTree to your vimrc?
This is getting really annoying for me. I only have this line for my NERDTree in my .vimrc file,
nmap <silent> <c-o> :NERDTreeToggle <CR>
Every time I hit return key in my Vim window in normal mode (Command Mode). NERDTree pops up.
Can anyone help me out with this problem?
Here's generally what I use.
:noremap <Leader>n :NERDTreeToggle<CR>
autocmd VimEnter * exe 'NERDTree' | wincmd l
starts up nerdtree on start and give me ',n' as my toggle
I had this issue when i mapped
<C-m>
to NERDTree. Fixed it by changing to something else which didn't involve ctrl key