vim mapping :w with a shell command - vim

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.

Related

Prevent Vim warning after autocommand changes file being edited

I run a command every time a save a file which, among other things, lints the current file. Vim does two annoying things which I wanted to avoid:
It asks me to press enter after the command was executed.
It notifies me that the current file has been modified outside of Vim.
How to get rid of this? I just want this to behave unobtrusively as ALE linters. This is my code:
autocmd BufWritePost *.prisma :execute '!npx prisma format'
#romainl's comment gave me an idea on how to do it:
autocmd BufWritePre *.prisma silent write | silent :execute '!npx prisma format' | edit! %
It's not the cleanest way to do it but it works. silent hides the command output and edit! forces Vim to open the modified file.

Configure Vim to open in "easy mode" by default?

I open Vim in "easy mode" with: vim -y
Is it possible to configure .vimrc to always open Vim in "easy mode" with just vim instead (without typing the -y)?
You can, since vim -y simply sets a number of options for that mode. It's possible to set those exact same options in the vimrc as well. They are shown in the vim documentation for evim (which is equivalent to vim -y), summarised below, see the link given for full detail:
These options are changed from their default value:
:set nocompatible insertmode hidden backup backspace=2
:set autoindent history=50 ruler incsearch mouse=a
:set hlsearch whichwrap+=<,>,[,] guioptions-=a
Key mappings changed:
<Down> <Up> Q <BS> CTRL-X <S-Del> CTRL-C <C-Insert> CTRL-V
<S-Insert> CTRL-Q CTRL-Z CTRL-Y <M-Space> CTRL-A <C-Tab> <C-F4>
Additionally:
- ":behave mswin" is used.
- syntax highlighting is enabled.
- filetype detection is enabled, filetype plugins and indenting is enabled.
- in a text file 'textwidth' is set to 78.
You can also have a look into the actual source file used for this mode if you do the following within a vim session:
:e $VIMRUNTIME/evim.vim
This shows the actual code run when starting with vim -y.
But I suspect an easier way to do it, at least with a UNIX-style OS, would be to just set up an alias, something like the following in your start-up scripts:
alias vim='/usr/bin/vim -y`
This would allow command-line invocations to start with easy mode. It won't help non-command-line invocations but you could do that (in UNIXes and Windows) by providing a script earlier in the path to do so.
For example, you could create a bash script of the form:
/usr/bin/vim -y "$#"
and call it vim, ensuring that where you put it comes in the path before /usr/bin.

force vim to overwrite external changes

I use Vim 7.4 (Mac OS) to edit and run Lua scripts. I've mapped a key in my .vimrc to save the current buffer and run an external script.
The key map in .vimrc:
map V :w!<CR> :!python "$HOME/tools/client/concli.py" --lua %<CR>
It works fine but every once in a while the files are 'touched' by Xcode (touch shell command). Then when I hit the mapped key vim warns me that the file has been changed externally and I have to confirm to write to it.
This is quite annoying since the files are often touched. How could I force vim to overwrite external changes without prompting? I tried 'w!' without success.
Thank you, Laurent
Indeed, the overwrite confirmation cannot be turned off with :w!, and :set autoread doesn't help in this case, neither. What does work is instructing Vim to explicitly check for changes before the write:
:checktime | w
I believe
set autoread
should do it. It tells Vim to automatically re-reads the file changed outside Vim.
I saw this in a mailing list. Apparently it is called if the file has changed timestamp, after a call to an external shell command.
function! ProcessFileChangedShell()
if v:fcs_reason == 'mode' || v:fcs_reason == 'time'
let v:fcs_choice = ''
else
let v:fcs_choice = 'ask'
endif
endfunction
autocmd FileChangedShell call ProcessFileChangedShell()
But it did not consistently fire for me. (Depending whether or not I had edited the file since the change, which in my case was external.)
There are some more tricks on the VimTips wiki which may help.
Add this to your ~/.vimrc file:
set autoread
nnoremap <C-u> :checktime<CR>
Now whenever you want vim to reload external changes, just click CTRL-U :)

ctags not working as expected with Vim plus general setup problems (C programming)

I have installed cvim and NodeTree plugins and generated an exuberant ctags file for my build tree.
This is what my ~/.vim/.vimrc file looks like:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
set tags=./tags;/
set number
set tabstop=4
set incsearch
When I start editing a file, I notice that Ctrl ] does not work and I have to resort to typing ta: funcname - which gets tiring after a while. Interestingly enough, Ctrl T pops me off the tag stack as expected - I don't understand whats going on - how do I fix this?
Incidentally, vim (appears to) completely ignores the contents of my .vimrc file and I always have to type the same commands in the editor, so as to get the settings I want - very annoying.
Last but not the least, I used to be able to type :make in the editor window, drop to the console and then have the build results displayed in a little window which I can then go to and select a line (with an error or warning say), and then have the editor automagically take me to the offending line - unfortunately, I don't remember the plugin (or commands) I used to allow me to build from within vim.
So, how do I:
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
Fix my .vimrc file so that contents are actually applied to my vim session.
Find the appropriate plugin to install to allow builds (using make) from within vim
You're asking about a weird mix of problems.
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
The tags functionality is working; I suspect that you have a mapping blocking Ctrl-]. Try
:verbose nmap <C-]>
and
:nunmap <C-]>
Fix my .vimrc file so that contents are actually applied to my vim session.
:echo $MYVIMRC
will tell you the location of the .vimrc that Vim uses. Also, check the output of :scriptnames which scripts get loaded, and read :help vimrc to understand the logic Vim applies.
Find the appropriate plugin to install to allow builds (using make) from within vim
That's built into Vim. With the appropriate 'makeprg' set (it defaults to make), you can run :make. Vim parses the output (through the 'errorformat' option), and you can open the quickfix list via :copen.
Your vimrc is:
~/.vim/.vimrc
If you run Vim 7.4, it should be:
~/.vim/vimrc
or
~/.vimrc
If you run Vim 7.3 or older, it should be:
~/.vimrc
And... what Ingo said.

How to open pdf files under cursor (using 'gf') with external PDF readers in vim

The current gf command will open *.pdf files as ascii text. I want the pdf file opened with external tools (like okular, foxitreader, etc.). I tried to use autocmd to achieve it like this:
au BufReadCmd *.pdf silent !FoxitReader % & "open file under cursor with FoxitReader
au BufEnter *.pdf <Ctrl-O> "since we do not really open the file, go back to the previous buffer
However, the second autocmd failed to work as expected. I could not figure out a way to execute <Ctrl-o> command in a autocmd way.
Could anyone give me a hint on how to <Ctrl-O> in autocmd, or just directly suggest a better way to open pdf files with gf?
Thanks.
That's because what follows an autocmd is an ex command (the ones beginning
with a colon). To simulate the execution of a normal mode command, use the
:normal command. The problem is that you can't pass a <C-O> (and not
<Ctrl-O>) directly to :normal, it will be taken as literal characters (<,
then C, then r) which is not a very meaningful normal command. You have two
options:
1.Insert a literal ^O Character
Use controlvcontrolo to get one:
au BufEnter *.pdf normal! ^O
2.Use :execute to Build Your Command
This way you can get a more readable result with the escaped sequence:
au BufEnter *.pdf exe "normal! \<c-o>"
Anyway, this is not the most appropriate command. <C-O> just jumps to the
previous location in the jump list, so your buffer remains opened. I would do
something like:
au BufEnter *.pdf bdelete
Instead. Still I have another solution for you.
Create another command with a map, say gO. Then use your PDF reader
directly, or a utility like open if you're in MacOS X or Darwin (not sure if
other Unix systems have it, and how it's called). It's just like double clicking
the icon of the file passed as argument, so it will open your default PDF reader
or any other application configured to open any file by default, like images or
so.
:nnoremap gO :!open <cfile><CR>
This <cfile> will be expanded to the file under the cursor. So if you want to
open the file in Vim, use gf. If you want to open it with the default
application, use gO.
If you don't have this command or prefer a PDF-only solution, create a map to
your preferred command:
:nnoremap gO :!FoxitReader <cfile> &<CR>
If the default app is acceptable, then simply using :!open % in command mode works. You can always map this to a suitable leader combination in your vim config file etc.
If you want something that works with normal mode, then you could try something like the following (i use this too for opening HTML files), and modify to your own needs:
if has('win32') || has ('win64')
autocmd FileType html nmap <Leader>g :silent ! start firefox "%"<cr>
elseif has('mac')
autocmd FileType html nmap <Leader>g :!open "%"<cr><cr>
endif

Resources