How do I check if a file is empty using vimscript? - vim

I have written my own linter in vimscript which lints a file using an external linting tool, and then reads the output into the quickfix window. I want to echo a message if there were no errors, but how can I check if that file is empty to do this?

Usually there's no need to check if temporary file is filereadable(). So only this should be enough:
if getfsize(tempname) < 1
throw "cannot read temporary file"
endif

Related

vimrc how to invoke unix find?

i want to set the tags variable to the set of all gotags files i generated in specific folder(s) using exuberant Ctags. (gotags is nothing but the tags file renamed).
i put following lines in my .vimrc file.
set tags+=/usr/local/go/src/gotags
set tags+=`find /home/vimal/gowork/src -name gotags`
but it doesnt work and i get the following error
$ vi ~/.vimrc
Error detected while processing /home/vimal/.vimrc:
line 157:
E518: Unknown option: /home/vimal/gowork/src
Press ENTER or type command to continue
how can i fix the error and set the tags variable with the value: list of all the gotags files under one directory tree.
Inventing new syntax tends not to work that well in practice. Use system() to run external commands from Vim, not backticks. Also set in Vim is weird, it doesn't evaluate RHS the way you expect. Most of the time it's a lot simpler to use let &option = ... instead of set option=....
Anyway, to answer your question, you don't need to run find(1) for that, plain Vim functions are enough for what you want:
let &tags = join(extend([&tags, '/usr/local/go/src/gotags'],
\ findfile('gotags', '/home/vimal/gowork/src', -1)), ',')

Vim: cannot see :echomsg messages in message history list

I'm working on a vim script and want to see the value of a variable that is generated in the script for debugging purposes.
I use something like:
echomsg 'My variable = ' . b:variable
reload the source with
:source %
and then look the message history list with
:messages
But there's nothing there and I'm pretty sure that the command would be executed.
What am I doing wrong here?
EDIT:
I'm trying to make a change to vim-cucumber and so I wanted to see the value of a variable. In the ftplugin/cucumber.vim file, I have the following:
let b:cucumber_root = expand('%:p:h:s?.*[\/]\%(features\|stories\)/step_definitions/mobile_website\zs[\/].*??')
echomsg 'cucumber_root = ' . b:cucumber_root
If you look at the top of file you will find
if (exists("b:did_ftplugin"))
finish
endif
Which stops the file from being sourced again after the first time. You need to unlet b:did_ftplugin before sourcing the file again.
Other options would be to use another tpope plugin to do this for you called scripttease with the :Runtime command.

Vim write temporary info as :write does

I have a macro which calls a function I defined in my vimrc
:function! DoStuff()
:!mycommand
:end
map <C-p>i :call DoStuff()<CR>
When I press the macro keys I get right a shell with the output of mycommand and it works fine but I would like improve that.
I noticed that when I write a file (:w) a short message is displayed for short time in the command bar which says "File X written", I want to achieve the same result, when macro sequence is pressed I want check if the command went fine (checking the return code) and then display a message (such "Ok" or "Not Okay") as the :write command does.
Any ideas ?
Write external command output to a temporary file
via Vimscript functions
First, capture the output of the external command in a variable, whose contents can then be written to a (temporary) file via writefile().
:let output = system('mycommand')
:call writefile(output, 'path/to/tempfile')
via scratch buffer
If you need to apply arbitrary commands to the captured contents, or need Vim to handle different file encodings, you have to use a buffer:
:new path/to/tempfile
:0read !mycommand
:write
:bdelete
via file redirection
This is the simplest approach when you need the output as-is:
:!mycommand > path/to/tempfile
Check for command success and notify
After external command execution, the special variable v:shell_error contains exit status of the last shell command. You can test that to report a message to the user:
!mycommand
if v:shell_error != 0
echohl ErrorMsg
echomsg "The command failed"
echohl None
else
echomsg "The command succeeded"
endif
If you've used system() to execute the command, you also have the error message returned by it, and can include that in the notification.

Proper way to create a syntax file in vim that depends on another syntax file

I am currently creating a Vim syntax file for WordPress and have it inside a file called wordpress.vim
All WordPress files are PHP files but not all PHP files are WordPress files.
My wordpress.vim syntax file depends on the php syntax file. So I have included it using the following line
so <sfile>:p:h/php.vim
My question is what is the most elegant way to include all PHP syntax into WordPress syntax and where should I put the the new wordpress.vim file that I have created.
The right location for your syntax file should be:
~/.vim/syntax/wordpress.vim
You should take a look at $VIMRUNTIME/syntax/cpp.vim for what I assume is the correct syntax:
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
" Read the C syntax to start with
if version < 600
so <sfile>:p:h/c.vim
else
runtime! syntax/c.vim
unlet b:current_syntax
endif

Can i use something like tunnel in vim?

For example:
If my current directory is /temp/src/com. And the file edited in vim is from /java/test.And now i want to add the path of the file to path environment. So if there is a cmd like set path+=$(filepath) in vim?
case 2:
Run make in terminal will start to compile a project, and it will out put logs about this compile. And now i want to read the outputed logs into vim using some command like r !make.
1) Pull the path into the current Vim buffer:
:r !echo \%PATH\%
Append to the path:
:let $PATH="C:\Test" . $PATH
2) This question is ambiguous, because it depends on your makefile behavior.
If your Makefile simply print to the console, then, :r make should do the trick.
If your make file actually writes to files explicitly, then there is no automatic way.
You'll have to write a custom vimscript function to pull in the logs.
1) Part 2
I do not know of what a way to do it in one line, but here's one way to achieve the functionality you want.
:redir #a "redirect output to register a
:pwd
:redir END "stop redirecting
:let #a = substitute(#a, '\n', '', 'g') "remove the newlines
:let $PATH=#a .":". $PATH
You should be able to wrap this in a function if you need to use it often.
You may reference environment variables using $MYVAR syntax. To set system environment variables use
let $MYVAR=foo
e.g.
let $PATH = "/foo" . $PATH
See http://vim.wikia.com/wiki/Environment_variables or :help :let-environment
Then you may use filename-modifiers to get directory name of a file in a current buffer:
let $PATH = expand("%:p:h") . $PATH
To read and parse compilation output in vim you might be interested to check quickfix mode
Use :make instead of :!make

Resources