I'm trying to open a PDF file from Vim using the commands
let openpdfname = "!open " . expand("%:t:r") . ".pdf"
map <F6> :silent execute openpdfname<CR> | redraw!
The PDF opens up in Preview and all is well, except that the Vim screen is not repainted. I use vim 7.2.108 (shipped standard with OSX 10.6) from the terminal window. I tried removing my ~/.vimrc entirely to try and determine the cause, with no luck.
Hitting Ctrl-L repaints the screen, but I thought 'redraw!' would have the same effect!?
Thanks for any hints!
Move <CR> to the end of mapping. You are trying to execute redraw after you exited command mode. Of course, it does not work.
The correct syntax that does what I expect is:
map <F6> :silent execute openpdfname<CR>:redraw!<CR>
Try this:
map <silent> <F6> :silent execute openpdfname<CR>
Related
I'm currently trying to write my own setup for autocompiling and previewing LaTeX code using Vim and Zathura. This is the code I have currently have in my tex.vim file:
:map <F6> :w <bar> ! pdflatex -synctex=1 -interaction=nonstopmode %<CR><CR>
:map <F7> :! zathura $(echo % \| sed 's/tex$/pdf/') & disown<CR><CR>
This works fine as long as when I compile the code (from Vim) I am in the directory where my tex files are. However, I would like this to work regardless of my position (since I frequently open files from Vim using the :e command). How do I fix this?
Also, I would like Zathura to open only if the PDF preview is not already open. Does anyone have any suggestions as to how to do such a thing?
You can use :h filename-modifiers instead of your sed hack, so this should do:
nnoremap :<C-u>w <bar> ! pdflatex -synctex=1 -interaction=nonstopmode %:p<CR>
nnoremap :<C-u>! zathura %:p:r.pdf<CR>
I have also changed these mappings to use :nnoremap which you should prefer anyway.
If you want to expand your Vim-fu even further you can check :h compiler and :h compilet-tex which will handle setting proper 'makeprg' for you which will allow you to use :make to build your TeX files.
An alternative would be to simply use vimtex (https://github.com/lervag/vimtex)
Mapping: <localleader>lv
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>
When I open Vim from a terminal, copy some text to the system clipboard, and exit Vim, the system clipboard gets cleared.
How to keep the copied text in the clipboard?
Synthesizing answers from superuser, just add the following to your .vimrc
autocmd VimLeave * call system("xsel -ib", getreg('+'))
Install Parcellite, or glipper for Gnome and klipper for KDE.
Restart your computer or run it manually.
see: https://wiki.ubuntu.com/ClipboardPersistence
Based on Matt's answer, the following uses xclip instead of xsel:
autocmd VimLeave * call system('echo ' . shellescape(getreg('+')) .
\ ' | xclip -selection clipboard')
I ran into this issue and a related problem where suspending vim with ctrl-z would also clear the clipboard. I've extended Matt's solution to fix both:
set clipboard=unnamedplus
if executable("xsel")
function! PreserveClipboard()
call system("xsel -ib", getreg('+'))
endfunction
function! PreserveClipboadAndSuspend()
call PreserveClipboard()
suspend
endfunction
autocmd VimLeave * call PreserveClipboard()
nnoremap <silent> <c-z> :call PreserveClipboadAndSuspend()<cr>
vnoremap <silent> <c-z> :<c-u>call PreserveClipboadAndSuspend()<cr>
endif
The if executable("xsel") guard is there to avoid errors if xsel is not installed. The nnoremap mapping preserves the clipboard when suspending from normal mode and the vnoremap mapping handles suspending from visual or select modes.
I've confirmed this works on vim 7.4 and 8.0.
Use NeoVim. It by default doesn't clear the clipboard on exit. You will still need to set clipboard=unnamedplus (typically in ~/.config/nvim/init.vim) and have xsel or xclip tools installed.
Keep in mind that some other defaults are different as well.
Please correct me if I'm wrong but from my understandings of Vim...
1) Vim uses registers instead of the clipboard to store copied/cut data.
2) These registers are preserved when exiting vim in a status file but are not accessible outside of the running process unless you manually open the file and inspect its contents
3) Saving stuff to the + registre while Vim runs allows you to paste to other applications.
4) By suspending vim (CTRL-Z) instead of closing it, these registers are still accessible.
Does that provide assistance?
Based on Matt's answer
When using his method copying multiple lines added slashes to the end of lines when pasting.
This should remedy that.
autocmd VimLeave * exe ":!echo " . shellescape(getreg('+')) . " | xclip -selection clipboard"
When i used "shellescape" with "system" newlines kept getting escaped. But that didn't happen when i used exe.
Don't know the real reason. but this worked.
First poster here at SO.
I'm currently using cygwin with external gvim. I got this in my vimrc
autocmd FileType sass setlocal shiftwidth=2 tabstop=2
map <F5> <Esc>:w<CR>:!sass %<.sass %<.css<CR><CR>
I want to work just like a normal :w while also running sass, how do i do that?
And also, when running this in my vimrc, I always have bash.exe popping out then says
c:\cygwin\bin\bash.exe -c "sass style.sass style.css"
Hit any key to close this window...
Can I get rid of "Hit any key to close this window...". I just want to run it directly without hitting any key.
You may be able to do this using the :silent command:
map <F5> <Esc>:w<CR>:silent !sass %<.sass %<.css<CR><CR>
The help for :silent says:
":silent" will also avoid the hit-enter prompt. When
using this for an external command, this may cause the
screen to be messed up. Use |CTRL-L| to clean it up
then.