How to clear output of function call in VIM? - vim

I know my question title is not explanatory enough so let me try to explain.
I created a vim function that displays my current battery state. My function is as follows:
function! BatteryStatus()
let l:status = system("~/battery_status.sh")
echo join(split(l:status))
endfunction
I have mapped the above function as nnoremap <F3> :call BatteryStatus()<cr>. Now, when I press F3 it displays my battery status as Discharging, 56%, 05:01:42 remaining which is my required output but my question is how do I make the above output disappear.
Currently what happens is after function call it continuously displays the output and I have to manually use :echo to clear the command window(:).
So, what necessary changes are to be made in my function so that I can achieve toggle like behaviour.
battery_status.sh
acpi | awk -F ": " '{print $2}'
PS: This is part of a learning exercise. So, please don't suggest alternative vim scripts.

Simplistic straightforward way to toggling the output:
let s:battery_status_output_flag = "show"
function! BatteryStatus()
if s:battery_status_output_flag == "show"
let l:status = system("~/battery_status.sh")
echo join(split(l:status))
let s:battery_status_output_flag = "clear"
else
echo ""
let s:battery_status_output_flag = "show"
endif
endfunction
Note s: prefix, see :help script-variable

You can define a autocmd:
:au CursorHold * redraw!
Vim redraws itself 4 sec (set by updatetime option) after it's idle.

vim
function! pseudocl#render#clear()
echon "\r\r"
echon ''
endfunction

You can just Ctrl+ L to clear the message on the status
line.

Related

How to reopen a closed file in vim? [duplicate]

Is it possible to reopen closed window in vim, that was in split?
Something like ctrl+shift+t with browser tabs?
:vs# will split current window vertically and open the alternate file.
It's so simple that you don't need to bind it to key.
Nice question! I was thinking to something like the following:
nmap <c-s-t> :vs<bar>:b#<CR>
It should work as you want.
No need for SHIFT:
nmap <c-t> :vs<bar>:b#<CR>
In conjunction with CTRL the characters are handled equally by vim, capitalized or not.
Actually also in the answer before, CTRLn and CTRLSHIFTN should both work.
I've gotten this to work by using bufmru.vim!
The following command, :ReopenLastTab, will re-split the last-open buffer:
command ReopenLastTab execute "vsplit" bufname(g:bufmru_bnrs[1])
I installed bufmru using Vundle, as below, but of course you can install it any way you like.
#.vimrc
" Install bufmru with Vundle
Plugin 'vim-scripts/bufmru.vim'
let g:bufmru_switchkey = "<c-t>" " I never use this: the default is Space, but I don't need to use it so set it to something I don't care about.
If anyone need something more generic I made this function.
Just place it in your .vimrc
" open last closed buffer
function! OpenLastClosed()
let last_buf = bufname('#')
if empty(last_buf)
echo "No recently closed buffer found"
return
endif
let result = input("Open ". last_buf . " in (n)ormal (v)split, (t)ab or (s)plit ? (n/v/t/s) : ")
if empty(result) || (result !=# 'v' && result !=# 't' && result !=# 's' && result !=# 'n')
return
endif
if result ==# 't'
execute 'tabnew'
elseif result ==# 'v'
execute "vsplit"
elseif result ==# 's'
execute "split"
endif
execute 'b ' . last_buf
endfunction
nnoremap <C-t> :call OpenLastClosed() <CR>
Call it with Ctrl+t and then select where you want to open that file.

Vim, reopen last closed window, that was in split

Is it possible to reopen closed window in vim, that was in split?
Something like ctrl+shift+t with browser tabs?
:vs# will split current window vertically and open the alternate file.
It's so simple that you don't need to bind it to key.
Nice question! I was thinking to something like the following:
nmap <c-s-t> :vs<bar>:b#<CR>
It should work as you want.
No need for SHIFT:
nmap <c-t> :vs<bar>:b#<CR>
In conjunction with CTRL the characters are handled equally by vim, capitalized or not.
Actually also in the answer before, CTRLn and CTRLSHIFTN should both work.
I've gotten this to work by using bufmru.vim!
The following command, :ReopenLastTab, will re-split the last-open buffer:
command ReopenLastTab execute "vsplit" bufname(g:bufmru_bnrs[1])
I installed bufmru using Vundle, as below, but of course you can install it any way you like.
#.vimrc
" Install bufmru with Vundle
Plugin 'vim-scripts/bufmru.vim'
let g:bufmru_switchkey = "<c-t>" " I never use this: the default is Space, but I don't need to use it so set it to something I don't care about.
If anyone need something more generic I made this function.
Just place it in your .vimrc
" open last closed buffer
function! OpenLastClosed()
let last_buf = bufname('#')
if empty(last_buf)
echo "No recently closed buffer found"
return
endif
let result = input("Open ". last_buf . " in (n)ormal (v)split, (t)ab or (s)plit ? (n/v/t/s) : ")
if empty(result) || (result !=# 'v' && result !=# 't' && result !=# 's' && result !=# 'n')
return
endif
if result ==# 't'
execute 'tabnew'
elseif result ==# 'v'
execute "vsplit"
elseif result ==# 's'
execute "split"
endif
execute 'b ' . last_buf
endfunction
nnoremap <C-t> :call OpenLastClosed() <CR>
Call it with Ctrl+t and then select where you want to open that file.

Create a mapping for Vim's command-line that escapes the contents of a register before inserting it

Suppose that I have a document like this, and I want to search for all occurences of the URL:
Vim resources: [http://example.com/search?q=vim][q]
...
[q]: http://example.com/search?q=vim
I don't want to type it out in full, so I'll place my cursor on the first URL, and run "uyi[ to yank it into the 'u' register. Now to search for it, I'd like to just paste the contents of that register into the search field by running:
/\V<c-r>u<CR>
This results in Vim searching for the string 'http:' - because the '/' character terminates the search field.
I can get around the problem by running this instead:
/\V<c-r>=escape(#u, '\/')<CR><CR>
But it's a lot of typing!
How can I create a mapping for Vim's commandline that simplifies this workflow?
My ideal workflow would go something like this:
press /\V to bring up the search prompt, and use very nomagic mode
hit ctrl-x to trigger the custom mapping (ctrl-x is available)
Vim listens for the next key press... (pressing <Esc> would cancel)
pressing 'u' would escape the contents of the 'u' register, and insert on the command line
Try this:
cnoremap <c-x> <c-r>=<SID>PasteEscaped()<cr>
function! s:PasteEscaped()
" show some kind of feedback
echo ":".getcmdline()."..."
" get a character from the user
let char = getchar()
if char == "\<esc>"
return ''
else
let register_content = getreg(nr2char(char))
return escape(register_content, '\/')
endif
endfunction
By the way, something that might be useful to know (if you don't already) is that you can use ? as the delimiter for :s. Which means that you could write a search-and-replace for an url like so:
:s?http://foo.com?http://bar.com?g
I've accepted Andrew Radev's solution, which solved the hard parts. But here's the version that I've added to my vimrc file, which adds a couple of enhancements:
cnoremap <c-x> <c-r>=<SID>PasteEscaped()<cr>
function! s:PasteEscaped()
echo "\\".getcmdline()."\""
let char = getchar()
if char == "\<esc>"
return ''
else
let register_content = getreg(nr2char(char))
let escaped_register = escape(register_content, '\'.getcmdtype())
return substitute(escaped_register, '\n', '\\n', 'g')
endif
endfunction
This should work:
whether you use / or ? (to search forwards, or backwards)
and when the pasted register includes multiple lines
Also, I changed the prompt. While waiting for a register, the prompt switches to \ - which seems like a suitable cue for 'PasteEscaped'. Also, I've appended a ", which mimics Vim's behavior after pressing <c-r> at the command line.
If you've any further suggestions for improvements, please leave a comment.
How about different workflow? For example, creating your own operator to search target text as is:
" https://gist.github.com/1213642
" Requiement: https://github.com/kana/vim-operator-user
map YourFavoriteKeySequence <Plug>(operator-search-target-text)
call operator#user#define('search-target-text', 'OperatorSerachTargetText')
function! OperatorSerachTargetText(motion_wise)
execute 'normal!' '`['.operator#user#visual_command_from_wise_name(a:motion_wise).'`]"xy'
let #/ = '\V' . escape(substitute(#x, '[\r\n]$', '', ''), '\')
normal! n
endfunction
I like #nelstrom's solution and made a small change to support escaping [ and ].
cnoremap <c-x> <c-r>=<SID>PasteEscaped()<cr>
function! s:PasteEscaped()
echo "\\".getcmdline()."\""
let char = getchar()
if char == "\<esc>"
return ''
else
let register_content = getreg(nr2char(char))
let escaped_register = escape(register_content, '\'.getcmdtype())
let escaped_register2 = substitute(escaped_register,'[','\\[','g')
let escaped_register3 = substitute(escaped_register2,']','\\]','g')
return substitute(escaped_register3, '\n', '\\n', 'g')
endif
endfunction

Can I use cppcheck when I execute :wq in Vim editor for c/c++ in a interactive way

Can anybody help me to get solution for my requirement?
Requirement is when a user exits from vim, cppcheck should happen and if any warning or error occurs then it should be prompted to a user.
Thanks in advance.
I assume you don't care if the command is executed asynchronously, since you're quitting the buffer anyway. You can use the :! command to run shell commands and :read to capture the output to a new window:
function! s:RunShellCommand(cmdline)
let first = 1
let words = []
" Expand and escape cmd arguments.
" shellescape() should work with '\'
for part in split(a:cmdline)
if first
" skip the cmd. ugly, i know.
let first = 0
else
if part[0] =~ '\v[%#<]'
let part = expand(part)
endif
let part = shellescape(part, 1)
endif
call add(words, part)
endfor
let expanded_cmdline = join(words)
" Create the new window
botright new
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
call setline(1, 'Showing output from cmd: ' . expanded_cmdline)
call append(line('$'), substitute(getline(2), '.', '=', 'g'))
" This is where actual work is getting done :-)
silent execute '$read !'. expanded_cmdline
" Uncomment the line below if you want the buffer to be
" non-modifiable
" setlocal nomodifiable
1
endfunction
Then you can define an autocommand for when a buffer is unloading:
au BufUnload *.cpp s:RunShellCommand('cppcheck %')
or a somewhat more generic command which you can call at any time:
command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)
Now, to prevent closing your buffer, you have to remap :wq or :q to a function that will perform the aforementioned (plus perhaps some confirmation?), since once :quit is invoked, it cannot be aborted.

Vim autocommand trigger on opening "nothing"

I want vim to open up the :Explorer when no file is opened or created. Eg. when I call vim without any options.
calling vim newfile.txt should still behave the normal way though.
How would I go about doing this? I can't seem to find the correct autocmd for it.
If you want to do this for vim invocation only, the best way is to use argc():
autocmd VimEnter * :if argc() is 0 | Explore | endif
argc() function returns a number of filenames specified on command-line when vim was invoked unless something modified arguments list, more information at :h argc().
Found the answer myself:
"open to Explorer when no file is opened
function! TabIsEmpty()
" Remember which window we're in at the moment
let initial_win_num = winnr()
let win_count = 0
" Add the length of the file name on to count:
" this will be 0 if there is no file name
windo let win_count += len(expand('%'))
" Go back to the initial window
exe initial_win_num . "wincmd w"
" Check count
if win_count == 0
" Tab page is empty
return 1
else
return 0
endif
endfunction
" Test it like this:
" echo TabIsEmpty()
function! OpenExplorer()
if (TabIsEmpty())
:Explore
end
endfunction
The greatest part of this code was taken from this question.

Resources