VIM: why do these bindings work only sometimes? -


i've got these bindings in .vimrc. work of time, don't: save file not run it.

then go insert mode , exit normal mode, , work again. 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 many topics discuss here out further ado:

mappings

your current mappings map <c-k> ... work in normal, visual, , operator-pending modes. executing mappings in visual mode or operator-pending modes save buffer range of lines (read not good). suggest make mappings normal mode only.

two general rules of thumb:

  • always supply mode n normal.
  • always use noremp instead of map unless mapping <plug> mapping.

so 1 of mappings might similar 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 2 things:

  • create mapping local specific buffer using <buffer> option noremap.
  • load mappings specific filetype.

this can done via autocmd , filetype event in .vimrc so:

autocmd filetype python nnoremap <buffer> <c-k> :w<cr>:!python %:p<cr> 

the other way option creating filetype plugin. (see :h ftplugin more details)

a simple example create file named, ~/.vim/ftplugin/python.vim , place mappings inside so:

nnoremap <buffer> <c-k> :w<cr>:!python %:p<cr> 

i lean more towards ftplugin approach having in .vimrc file can nice.

for more see:

:h :au :h filetype :h map-local :h ftplugin 

:make

a more vim way of doing use :make. (assuming want lint vs execute current buffer)

  • :make execute 'makeprg'. defaults make great of c projects
  • after running :make quickfix list contain errors.
  • set compiler via :compiler command.
  • extra parameter can passed :make foo-command
  • current filename can represented %. e.g. :make %

often people set :complier/'makeprg' in side of ftplugins e.g. ~/.vim/ftplugin/perl.vim or autocmd's e.g. autocmd filetype perl compiler perl.

fop more see:

:h :make :h 'makeprg' :h :compiler :h c_% 

quickfix list

  • use :cnext , :cprev move between errors.
  • :copen open quickfix list in window (:cclose close)
  • :cwindow open quickfix list window if there errors
  • may want use better mappings :cnext , friends. suggest tim pope's unimpaired plugin

for more see following:

:h quickfix :h :cnext :h :cope 

alternatives using :make

  • just use <c-z> suspend vim , run build system. (cons: loose out on quickfix list)
  • use :! compile. (same cons suspending) e.g. :!make
  • syntastic syntax checking system checks files on save
  • ale (asynchronous lint engine) plugin providing linting in neovim , vim 8 while edit text files
  • dispatch can used run things in background. great test suites
  • may want consider terminal multiplexers tmux or screen.
  • singlecomplile tries , takes of work out of using :make

conclusion

personally install ale removes need mappings. great idea learn how use location (or quickfix) list. when ready hands dirty can learn , use :make.

tl;dr

install ale.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -