How to use command to switch to Terminal-Normal mode? - function

I would like to make a key mapping which could move to terminal window and switch to Terminal-Normal mode, and perform the opposite action again to look over the long message.
map <F4> call Toggle_Terminal_Mode()<cr>
function Toggle_Terminal_Mode()
if &buftype ==""
move to terminal-window
switch terminal-normal mode " ?????
do blah blah blah....
elseif &buftype=="terminal" && mode()=="n"
swich terminal-mode
move back to edited buffer window
endif
endfunc
I know I can use <C-w>N or <C-\><C-n> to switch to Terminal-Normal mode, but I want to use command instead in function so as to do other thing.
And I try
:execute "normal \<C-w>N" doesn't work,
and <C-\><C-N> either.
:call term_sendkey(..) , does't work.
How to use command to switch to terminal-Normal mode ?

Finally, I try the <expr> to solve the conditional requirement.
nnoremap <expr><F3> &bt=="" ? "<C-w>j<C-\><C-n>z100<CR>H" : "z10<cr>i" . "<C-_>k"
If current buffer type is "" (null), it will jump down to terminal window(default),
go to Normal mode, and resize line height to max size(supposed 100).To look the long message in terminal.
If current buffer type is "terminal", it will restore to 10 line height, go to terminal mode , and go back to the up window. To continue editing the code window.
But I am still finding the method how to change terminal mode in command (not key mapping method).

Related

Cannot use auto-completion function in coc.nvim

Whenever I enter a certain auto-completion suggestions from coc.nvim, it just flashes the suggestion and removes it entirely and it throws an error 'coc.nvim: command not found'. The suggestion tab shows normally and acts normally.
This bug always happens in a gdscript file, all other files work as intended.
I can't find a fix for this, maybe its the language server or the coc configuration file.
Try adding this to your init.vim file.
inoremap complete_info().selected != -1 ?
\ &filetype == "gdscript3" ? (coc#expandable() ? "<C-y>" : "<C-y>a") : "<C-y>"
\ : "<C-g>u<CR>"

How to type text into Vim's command-line without executing it?

My context:
I frequently take notes in VIM. I'd like a VIM function to type a standard-header into the command line (specifically, a timestamp such as :sav 20180418_) without executing; control would return with VIM still in command-mode (so that user could append remainder of the filename and execute).
My fundamental difficulty: I cannot seem to get a Vim function/macro to enter command-mode, supply text to the command line, then exit while staying in command-mode and not executing the text supplied.
Is this possible?
Thank you.
You can use expand() function. For example if you are currently editing file 20180418_.txt you can type:
:sav <c-r>=expand("%:r")<cr>
where <c-r>= should be typed as Ctrl+R followed by =. Enter key is <cr>. This will expand the text in the command line into:
:sav 20180418_

Function to open a file and navigate to a specified line number

I have the output of recursive grep (actually ag) in a buffer, which is of the form filename:linenumber: ... [match] ..., and I want to be able to go to the occurrence (file and line number) currently under the cursor. This told me that I could execute normal-mode movements, so after extracting the file:line portion, I wrote this function:
function OpenFileNewTab(name)
let l:pair=split(a:name, ":")
execute "tabnew" get(l:pair, 0)
execute "normal!" get(l:pair, 1) . "G"
endfunction
It is supposed to open the specified file in a tab and then do <lineno>G, like I am able to do manually, to go to the specified line number. However, the cursor just stays on line 1. What am I doing wrong?
This question, by title alone, would be an exact duplicate, but it talks locating symbols in other files, while I already have the locations at hand.
Edit: My mappings for grep / ag are as follows:
nnoremap <Leader>ag :execute "new \| read !ag --literal -w" "<C-r><C-w>" g:repo \| :set filetype=c<CR>
nnoremap <Leader>gf ^v2t:"zy :execute OpenFileNewTab("<C-r>z")<CR>
To get my grep / ag results, I put the cursor on the word I want to search and enter <leader>ag, then, in the new buffer, I put the cursor on a line and enter <leader>gf - it selects from the start up to the second colon and calls OpenFileNewTab.
Edit 2: I'm on Cygwin, if it is of any importance - I doubt it.
Why don't you set &grepprg to call ag ?
" according to man ag
set grepprg=ag\ --vimgrep\ $*
set grepformat=%f:%l:%c:%m
" And then (not tested)
nnoremap <Leader>ag :grep -w <c-r><c-w><cr>
As others have said in the comments, you are just trying to emulate what the quickfix windows already provides. And, we are lucky vim can call grep, and it has a variation point to let us specify which grep program we wish to use: 'grepprg'.
Use file-line plugin. Pressing Enter on a line in the quicklist will normally open that file; file-line will make any filename of the form file:line:column (and several other formats) to open file and position to line and column.
I only found this (old) thread after I posted the exact same question on vi.stackexchange: https://vi.stackexchange.com/q/39557/44764. To help anyone who comes looking, I post the best answer to my question below as an alternative to the answers already given.
The gF command, like gf, opens the file in a new tab but additionally it also positions the cursor on the line after the colon. (I note the OP defines <leader>gf so maybe vim/neovim didn't auto-define gf or gF at the time this thread was originally created.)

Start Chrome Minimized?

rundll32 shell32.dll,ShellExec_RunDLL "C:\...\shortcut.lnk"
rundll32 shell32.dll,ShellExec_RunDLL "C:\...\shortcut2.lnk"
That's what I've got so far running inside a batch. It starts Chrome just fine, but I want it to start minimized as I only want a page to autorun and then Chrome be closed later automatically. I don't ever want the window to be anything other than minimized.
So far I've tried /min in every location of this command that I could and none have worked. The only position where it does anything is before the directory and it seems to run the Chrome process and then kills it immediately after. Putting --no-startup-window as a parameter in the shortcut also reacts the same way.
I've tried a few other things as well like setting the shortcuts to "minimized" in the window mode, but nothing has worked so far. I could really use some help with this as I'm pretty much stuck. The solution could either be a command for a batch executable or something having to do with the actual shortcut file.
Any help would be greatly appreciated. I'd hate to have to manually minimize these two windows every day.
Here are the two shortcuts and batch file if you want to mess with them.
https://drive.google.com/uc?export=download&id=0B_KZqubEguX6N04xOVZfWVVlQUE
If you just need to run Chrome from a bat file with non full screen mode you can try to use these parameters:
--window-position=0,0 --window-size=1,1
While thinking about a solution within a batch file and coming to the conclusion that there isn't a better solution, than also described above, I've written this quick and dirty piece of code and compiled it in case that you don't have a compiler:
(You will find it also as a snippet on Gist)
Module Module1
Sub Main(ByVal Args() As String)
Try
If Args.Length = 2 Then
Dim fileName As String = Args(1)
If Not String.IsNullOrEmpty(fileName) Then
If System.IO.File.Exists(fileName) Then
Dim startInfo As New ProcessStartInfo(fileName)
Select Case Args(0).ToLower()
Case "hide"
startInfo.WindowStyle = ProcessWindowStyle.Hidden
Case "minimize"
startInfo.WindowStyle = ProcessWindowStyle.Minimized
Case Else
Console.WriteLine(Args(0) & " is unknown. Showing Window.")
startInfo.WindowStyle = ProcessWindowStyle.Normal
End Select
Process.Start(startInfo)
Else
Throw New System.IO.FileNotFoundException("The file specified was not found. (""" & fileName & """)")
End If
Else
Throw New System.ArgumentOutOfRangeException("No file specified.")
End If
Else
Throw New System.Exception("Invalid count of arguments")
End If
Catch ex As Exception 'Optional: Catch ex As System.IO.FileNotFoundException
Console.Error.WriteLine("ERROR" & vbCrLf & ex.Message)
End
End Try
End Sub
End Module
Download the compiled file: RunProcess.exe.
For those who aren't familiar with vb.net or don't want to read this bad formatted thing:
You can use it the following way:
RunProcess minimize "C:\Program Files\[...]\chrome.exe"
RunProcess hide "C:\Program Files\[...]\chrome.exe"
RunProcess show "C:\Program Files\[...]\chrome.exe"
At this point it doesn't check execute paths:
RunProcess minimize "cmd.exe" wont work. You would have to use RunProcess minimize "%systemroot%\System32\cmd.exe"
EDIT:
Also have a look at this: Start-Process -WindowStyle Hidden "chrome.exe" "www.google.com"
If you do not want them to stay open for very long you could do like this
start "C:\Portable Apps\Program Files\Google Chrome\GoogleChromePortable.exe" <somelink1>
start "C:\Portable Apps\Program Files\Google Chrome\GoogleChromePortable.exe" <somelink2>
Taskkill /F /IM GoogleChromePortable.exe
or
Taskkill /F /IM chrome.exe

Vim function to copy a code function to clipboard

I want to have keyboard shortcut in Vim to copy a whole function from a Powershell file to the Windows clipboard. Here is the command for it:
1) va{Vok"*y - visual mode, select {} block, visual line mode, go to selection top, include header line, yank to Windows clipboard.
But it would work only for functions without an inner {} block. Here is a valid workaround for it:
2) va{a{a{a{a{a{a{Vok"*y - the same as (1), but selecting {} block is done multiple times - would work for code blocks that have 7 inner {} braces.
But the thing is - the (1) command works fine when called from a vim function, but (2) misbehaves and selects wrong code block when called from a vim function:
function! CopyCodeBlockToClipboard ()
let cursor_pos = getpos('.')
execute "normal" 'va{a{a{a{a{a{a{Vok"*y'
call setpos('.', cursor_pos)
endfunction
" Copy code block to clipboard
map <C-q> :call CopyCodeBlockToClipboard()<CR>
What am I doing wrong here in the CopyCodeBlockToClipboard?
The (2) command works as expected when executed directly in vim.
UPDATE:
I've noticed that:
if there are more a{ then the included blocks in the function
then vim wouldn't execute V
Looks like vim handles errors differently here. Extra a{ produces some error and regular command execution just ignores it. But execution from withing a function via :normal fails and wouldn't call V (or probably any command that follows the error).
Any workaround for this?
Try this function
function! CopyCodeBlockToClipboard()
let cursor_pos = getpos('.')
let i = 1
let done = 0
while !done
call setpos('.', cursor_pos)
execute "normal" 'v' . i . 'aBVok"*y'
if mode() =~ "^[vV]"
let done = 1
else
let i = i + 1
endif
endwhile
execute "normal \<ESC>"
call setpos('.', cursor_pos)
endfunction
This preforms a execute command to select blocks until it fails to select a block larger block. ([count]aB selects [count] blocks) It seems when the selection fails we end up in visual mode. So we can use mode() to check this.
When this function exits you should be in normal mode and the cursor should be restored to where you started. And the function will be in the * register.
This macro should come close to what you want to achieve:
?Function<CR> jump to first Function before the cursor position
v enter visual mode
/{<CR> extend it to next {
% extend it to the closing }
"*y yank into the system clipboard