I'm using GVim 8.1 on Windows 10 with no external plugins.
I have the following set up in my .gvimrc file:
let g:build_file_abs_path = fnamemodify(findfile("windows-build.bat", ";."), ":p:h")
" This build script is a basic wrapper for 'clang.exe file.c -o file.exe' style invocation
let &makeprg=g:build_file_abs_path . "\\windows-build.bat"
nnoremap <silent> <C-B> :cd <C-R>=g:build_file_abs_path<CR> <bar> make! <bar> copen <bar> redraw <bar> cd -<CR>
Now, this automatically opens a quickfix window with the correct compiler output. However, when I press ENTER over the error, the cursor jumps to the buffer for the affected file, yet it is completely blank with a single line. Furthermore, this occurs as I use :cn and :cp commands inside the quickfix window. e.g:
Images showing these two states:
before
after
Please note that:
:verbose nmap <CR> returns no mappings, so there is not conflict there.
I would appreciate it if someone could provide some insight as to how to avoid the buffer becoming empty and actually jump to the error in the appropriate file. Many thanks.
Thanks to Christian Brabandt's comment, I was able to solve the issue. I was misunderstanding the distinction between the working directories of vim and the build script. I made the following changes:
let &makeprg="cd " . g:build_file_abs_path . " && windows-build.bat"
nnoremap <silent> <C-B> :make! <bar> copen <bar> redraw <CR>
I am looking for a way to automatically resize my open v-split panes in Vim after I call NERDTreeToggle.
I have NERDTreeToggle being called on the shortcut "ctrl+\" at the moment, and ideally what I want is to call the keyboard shortcut "ctrl+w =" immediately afterwards.
Any ideas? Thanks.
If this is your current mapping:
:nnoremap <C-\> :NERDTreeToggle<CR>
You can just append the window command after it:
:nnoremap <C-\> :NERDTreeToggle<CR><C-w>=
Alternatively, you can execute this from command-line mode as well, via :normal!:
:nnoremap <C-\> :NERDTreeToggle<Bar>execute "normal! \<lt>C-w>="<CR>
Note that for window commands, there's also a special :wincmd to invoke them:
:nnoremap <C-\> :NERDTreeToggle<Bar>wincmd =<CR>
I'd like to be able to use the same shortcut to jump to a file in NERDTree and close NERDTree. I have no experience with VimL and could use some help.
Add the following to you vimrc
set autochdir
noremap <F2> :NERDTreeTabsToggle<cr>
The first command will automatically change the current directory to be the same the same as the file you are editing.
The second command will then set F2 to toggle toggles NERDTree on/off for all tabs. you can use any key i prefer F2.
Just say you want to do this with <leader>n:
" This in your ~/.vimrc
nnoremap <leader>n :NERDTreeFind<CR>
" This in ~/.vim/ftplugin/nerdtree.vim
nnoremap <buffer> <leader>n :NERDTreeClose<CR>
Substitute whatever you want for <leader>n.
I read this stack thread on how to save a mapping permanently in vim.
I went to /etc/vim/vimrc and added the :map ,k :!python % <enter> in the first line of the file. When I open a new python file with vim the command responds by showing :!python % <enter> at the bottom of the screen. But it doesn't execute automatically. How do I fix this command to execute 'enter' automatically?
i think you rather want to use a local version ~/.vimrc for local settings than editing global settings in the file /etc/vim/vimrc. There you can add:
let maplocalleader=","
map <localleader>k :!python %<cr>
i would advise to not use ^M as explained in ZyX comment to ghoti's answer see
Edit
ZyX' comment made me change the code above to
filetype plugin on
augroup ft_python
au!
au FileType python nnoremap ,k :!python %<cr>
augroup END
which keeps ,k from messing with other stuff
filetype plugin on says that we want to have things done if a file has a certain filetype - in our case python
augroup ft_python says i want to have a group of autocommands := "commands that are invoked when opening a file" with that group-name
au! removes all autocommands
au Filetype python declares an autocommand=au for all files of type python
augroup END leaves the group ft_python and goes back to general settings
so this makes it possible to have ,k act as 'compile' in python but when i accidentally press ,k in another file say a c-source code, it does nothing (or the things i told ,k to be for c-source code somewhere else in my .vimrc).
Put in a literal carriage return character instead - press Ctrl + V, then Enter when writing the map:
:map ,k :!python %^M
You'll see the carriage return shown as ^M after typing the sequence Ctrl + V, then press the enter key.
That Ctrl + V key tells vim to insert the next character that you type.
The answer I was looking for is to paste :map :!python % <enter> in ~/.vimrc
One of the primary distinction betweeen vi (vim) and emacs, is emacs is designed and supposed to be run at times without quitting, where as given the quick load time of vim, it is easier to quit and start for editing tasks. I read that it has become a cultural difference between these two editor users.
I tend to think that keeping the editor running at all times, kind of helps in productivity as you know that, something is in progress and you need not start again. What is best tricks and plugins that you have found to run a single vim session and do all your tasks from there?
For e.g, I edit Python programs, then quit to run the appengine appcfg and go back. Sometimes quit current file to open another file. I have not yet gotten used to concept of buffers and tabs, but rather run vim in my screen sessions, if I want to edit multiple files.
So you're running one file Vim per screen session? That sounds pretty bad man. You don't really need any special plugins to use multiple files in Vim easily. Just do
:e /home/project/myfile.py
I have set autochdir in my .vimrc which automatically changes current working directory to whatever buffer is currently active. So once you have that file open you can just do
:e myfile2.py
:e myfile3.py
etc. BTW opening any files in Vim can be completed with tab completion so make sure you are doing that. Once you have a bunch of buffers open to switch between I just do
:b myfile1.py
which you can also use tab completion for you can just type :b 1 and hit tab and it will figure out you want myfile1.py open so it is super quick if you can remember the general file name and if there is more than one similar match it will give you a list that you can tab through. For that I would also advise taking a look at the wildmode and wildmenu settings to see what you prefer they will give you enhanced tab completion menus. If at any time you start getting lost with what buffers are open and what you want to look at you can just do
:ls
and it will show you everything open.
Also remember you can run external commands by preceding a command with !
:!ls
for example. Hope some of this helps or at least gets you looking in the right direction.
Everything the others said plus three:
With set hidden you can open a new buffer in place of the current one, even if it's not saved. You can open dozens of buffers like that, no need to close Vim! See :help windows or the Vim wiki.
Supposing Vim is compiled with the correct flag (+clientserver) you can have a single Vim running as a "server" (in a terminal window A) and open all your files in that single Vim (from terminal window B). It's done in two steps:
$ vim --servername WHATEVER to start Vim
$ vim --remote file.js to open a file
Your file is opened in Vim in terminal window A and you still have your prompt in terminal window B.
Don't touch tabs. They are terribly wrongly named and don't work like tabs at all.
You can even drop down to a shell using :sh, and then get back to Vim using exit in the shell. For editing multiple files in the same Vim, you can use :vsplit filename or :split filename (for vertical and horizontal splits), and then use Esc+Ctrl+w+arrow keys to navigate between the different splits. This way you don't need tabs. Works especially well if you're working with small pieces of code.
Just use the :! command to run stuff in a shell. It mixes great with :cd and % expansion
bash> vim path/to/ex.c
...
:cd %:h. " move to path/ex/
:!gcc -o %:r % && %:r " compile ex.c into ex and run it
You can also mix it with :read if you want to put the output of a command in the current buffer:
:read !ls " read in the names of all the files in the current directory
Here's good video tutorial that helps with workflow of how and why to use a single Vim session to manage all your edits:
http://www.derekwyatt.org/vim/vim-tutorial-videos/vim-intermediate-tutorial-videos/#onevim
If I'm running vim from console (which I do on linux because I use ssh exclusively), then I often use CTRL-z to suspend vim. Then do my shell stuff and fg to return to vim.
Using ctags in vim is incredibly useful -- help tags for more info.
I use a perforce plugin that is quite powerful: http://www.vim.org/scripts/script.php?script_id=240. The diff support is amazing because you can cycle through all opened files or look at file history and diff between 2 older versions. Try :PVDiff, :PFilelog and :POpened.
I define a lot of macros for things like search and buffer windows manipulation. I have some interesting macros/functions listed here that help me live in vim.
Clipboard:
let mapleader=","
" put from clipboard
nmap ,p "*p
" yank to clipboard
nmap ,y "*y
Tags:
" jump to tag in other window
map t :call TagJumpOtherWindow()<cr>
function! TagJumpOtherWindow()
let cw = expand("<cword>")
winc p
exec "tjump " . cw
let #/ = cw
normal z.
winc p
endfunction
Scratch:
let mapleader=","
nmap ,x0 :e e:/work/scratch0.txt<CR>
nmap ,x1 :e e:/work/scratch1.txt<CR>
nmap ,x2 :e e:/work/scratch2.txt<CR>
nmap ,x3 :e e:/work/scratch3.txt<CR>
nmap ,x4 :e e:/work/scratch4.txt<CR>
nmap ,x5 :e e:/work/scratch5.txt<CR>
nmap ,x6 :e e:/work/scratch6.txt<CR>
nmap ,x7 :e e:/work/scratch7.txt<CR>
nmap ,x8 :e e:/work/scratch8.txt<CR>
nmap ,x9 :e e:/work/scratch9.txt<CR>
IDE:
function! GetMsdevFile(line)
let mx = '^\s*\([a-zA-Z_/\.0-9:\- ]*\)'
let line = matchstr( a:line, mx )
let file = substitute( line, mx, '\1', '' )
let file = substitute( line, '\\', '/', '' )
return file
endfunction
function! GetMsdevLineNumber(line)
let mx = '^\s*\([a-zA-Z_/\.0-9:\- ]*\)(\(\d\+\))'
let line = matchstr( a:line, mx )
let linenumber = substitute( line, mx, '\2', '' )
return linenumber
endfunction
function! GetMsdevFile2(line)
let file = expand("%:p:h") . "/" . GetMsdevFile(a:line)
let file
return file
endfunction
function! GetMsdevFile2(line)
let file = expand("%:p:h") . "/../" . GetMsdevFile(a:line)
let file
return file
endfunction
function! GotoMsdevMake( thiswin, version )
exec "cd ".$DIRECTORY."\\.."
let l = getline(".")
if a:version==0
let file = GetMsdevFile(l)
let linenumber = GetMsdevLineNumber(l)
elseif a:version==1
let file = GetMsdevFile2(l)
let linenumber = GetMsdevLineNumber(l)
else
let file = GetMsdevFile3(l)
let linenumber = GetMsdevLineNumber(l)
endif
if a:thiswin==1
winc p
endif
exec "e +" . linenumber. " " . file
exec "cd -"
endfunction
function! GetGCCFile(line)
let mx = '^\([a-zA-Z_/\.0-9:\- ]*\):[0-9]\+: .*'
let line = matchstr( a:line, mx )
let file = substitute( line, mx, '\1', '' )
let file = substitute( file, '\\', '/', '' )
return file
endfunction
function! GetGCCLineNumber(line)
let mx = '^\([a-zA-Z_/\.0-9:\- ]*\):\([0-9]\+\):.*'
let line = matchstr( a:line, mx )
let linenumber = substitute( line, mx, '\2', '' )
return linenumber
endfunction
function! GotoGCCMake()
exec "cd ".$DIRECTORY."\\.."
let l = getline(".")
let file = GetGCCFile(l)
let linenumber = GetGCCLineNumber(l)
winc p
exec "e +" . linenumber. " " . file
exec "cd -"
endfunction
function! MakeOut( filename )
exec ":e " . a:filename
call MakeBuffer()
normal zz
endfunction
" use the current buffer into a Visual Studio build output buffer to jump to errors
function! MakeBuffer()
normal! gg
exec "/).*error\\|failed"
nnoremap <buffer> <cr> :call GotoMsdevMake(1, 0)<cr>
nnoremap <buffer> :call GotoMsdevMake(1, 1)<cr>
nnoremap <buffer> o :call GotoMsdevMake(1, 1)<cr>
" nnoremap <buffer> :call GotoMsdevMake(0, 0)<cr>
endfunction
" use the current buffer into a GCC build output buffer to jump to errors
function! MakeGCCErr()
normal! gg
exec "/: error:"
nnoremap <buffer> <cr> :call GotoGCCMake()<cr>
nnoremap <buffer> :call GotoGCCMake()<cr>
nnoremap <buffer> o :call GotoGCCMake()<cr>
endfunction
function! MakeGCCOut( filename )
exec ":e " . a:filename
call MakeGCCErr()
endfunction
nmap ,mr :call MakeOut( "e:/perforce/branch/obj/release/BuildLog.htm" )<cr>
nmap ,md :call MakeOut( "e:/perforce/branch/obj/debug/BuildLog.htm" )<cr>
nmap ,mm :call MakeBuffer()<CR>
nmap ,mq :call MakeGCCErr()<cr>
I keep a single vim window open for days at a time. split windows work really nicely on large screens.
I also like tabs; I cluster my splits for a single project in a tab, but keep other tabs around for my day plan, my vim wiki, scratch notes for when I'm interrupted. I find tabs easier to use than multiple windows.