Explanation of default vimrc file for windows version of vim - windows

I've just downloaded vim7.4 for windows and I'll like to know what the default vimrc file that comes with it is doing. Just a brief explanation of each item would be nice to know.
Here it is.
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction

set nocompatible
By default, Vim behaves like its ancestor vi. It's not really a problem for quick edits but it seriously sucks if you intend to use it for more than 10 minutes. set nocompatible makes Vim more Vim-like than Vi-like.
See :help nocompatible.
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
Source those two files. vimrc_example.vim contains example settings. It may be a good idea to take a look at it: it may contain useful things for you.
behave mswin
mswin.vim contains a bunch of settings and mappings designed to make Vim work more or less like a typical Windows editor, like <C-v> to paste. This command "activates" that stupid behavior.
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
This is a function that defines Vim's behavior when used for diffing two or more buffers. It looks more like a hack than anything else, to me. It seems to be checking if your Windows environment is able to do the diff itself and, if not, use the builtin diff.
If you intend to use/learn Vim seriously, none of the above should go into your $HOME\_vimrc. Not even the first line since nocompatible is set automatically for you if Vim finds a $HOME\_vimrc.
I'm not really familiar with Windows so the diff part may be useful but you can safely drop the rest.
When you see a set something, do :help 'something (with the single quote).

You may have figured this out, but one thing you may want to add if you remove the vimrc_example.vim source is syntax on. Without this, text will be colorless in gvim regardless of colorscheme.

Related

jq in a Jenkins pipeline not saving output to variable

So in my Jenkins pipeline I run a couple of curl commands across different stages. I store the ouput of Stage1 into a file and for every item in that list I run another curl command and use the output of that to extract some values using jq.
However from the second stage I can't seem to store the jq extracted values into variables to echo them later. What am I doing wrong?
{Stage1}
.
.
.
{Stage2}
def lines = stageOneList.readLines()
lines.each { line -> println line
stageTwoList = sh (script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
pfName = sh (script: "jq -r '.component.name' <<< '${stageTwoList}' ")
pfKey = sh (script: "jq -r '.component.key' <<< '${stageTwoList}' ")
echo "Component Names and Keys\n | $pfName | $pfKey |"
}
returns in the end for Stage2
[Pipeline] sh
+ jq -r .component.name
digital-hot-wallet-gateway
[Pipeline] sh
+ jq -r .component.key
dhwg
[Pipeline] echo
Component Names and Keys
| null | null |
Any help in the right direction appreciated!
You passed true as the argument for the returnStdout argument to the shell step method for stageTwoList, but then forgot to use the same argument for the JSON parsed returns to the next two variable assignments:
def lines = stageOneList.readLines()
lines.each { line -> println line
stageTwoList = sh(script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
pfName = sh(script: "jq -r '.component.name' <<< '${stageTwoList}' ", returnStdout: true)
pfKey = sh(script: "jq -r '.component.key' <<< '${stageTwoList}' ", returnStdout: true)
echo "Component Names and Keys\n | $pfName | $pfKey |"
}
Note you can also make this much easier on yourself by doing the JSON parsing natively in Groovy and with Jenkins Pipeline step methods:
String stageTwoList = sh(script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
Map stageTwoListData = readJSON(text: stageTwoList)
pfName = stageTwoListData['component']['name']
pfKey = stageTwoListData['component']['key']

integer-only wildcards in grep

i have command that outputs a collection strings that look like this:
json.formats[0]].url = "https://example.com/ar.html"
json.formats[1].url = "https://example.com/es.html"
json.formats[2s].url = "https://example.com/ru.html"
json.formats[3].url = "https://example.com/pt.html"
json.formats[73].url = "https://example.com/ko.html"
json.formats[1502].url = "https://example.com/pl.html"
(there are many more instances, however for simpilcity's sake theyve been removed)
i can use the command below
myCmd | grep -e 'json\.formats\[.*\]\.url\ \=\ '
however i only want the wildcard to match integers, and to throw out non-integer matches. it gives me the following:
json.formats[0]].url = "https://example.com/ar.html"
json.formats[1].url = "https://example.com/es.html"
json.formats[2s].url = "https://example.com/ru.html"
json.formats[3].url = "https://example.com/pt.html"
json.formats[73].url = "https://example.com/ko.html"
json.formats[1502].url = "https://example.com/pl.html"
what i really want is this:
json.formats[1].url = "https://example.com/es.html"
json.formats[3].url = "https://example.com/pt.html"
json.formats[73].url = "https://example.com/ko.html"
json.formats[1502].url = "https://example.com/pl.html"
Thanks :-)
You may use:
myCmd | grep -E 'json\.formats\[[[:digit:]]+\]\.url = '
or:
myCmd | grep -E 'json\.formats\[[0-9]+\]\.url = '
[[:digit:]] is equivalent of [0-9] for most of the locales.

Do I need to manually set $VIMRUNTIME on my pc

For me I think the following is the case:
$VIMRUNTIME='C:/Program Files (x86)/Vim/vim73'
But if I look in the pc's environment variables (via Computer>Properties etc) I cannot see this path - should I add it manually?
_vimrc currently looks like the following:
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
set diffexpr=MyDiff()
" Use CTRL-S for saving, also in Insert mode
:nnoremap <C-S> :<C-U>update<CR>
:vnoremap <C-S> :<C-U>update<CR>gv
:cnoremap <C-S> <C-C>:update<CR>
:inoremap <C-S> <C-O>:update<CR>
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
Though vim helpfiles describe $VIMRUNTIME as an environment variable, you may find that it is not set in your shell.
It should be set in Vim, and you should be able to see it, in Vim, thus:
:echo $VIMRUNTIME
The ever-so helpful Vim helpfiles don't explain this.
But they do describe in exhausting detail the steps Vim goes through to derive a value for $VIMRUNTIME in its own, er, runtime environment.
If you are blessed with some form of obsessive compulsion, you might read far enough through this dense help text to see, at the very bottom, where it says that if want $VIMRUNTIME in your shell, you can get it with the following command:
VIMRUNTIME=`vim -e -T dumb --cmd 'exe "set t_cm=\<C-M>"|echo $VIMRUNTIME|quit' | tr -d '\015' `
Despite appearances, this is not some sort of joke. I repeat:
VIMRUNTIME=`vim -e -T dumb --cmd 'exe "set t_cm=\<C-M>"|echo $VIMRUNTIME|quit' | tr -d '\015' `
No. $VIMRUNTIME is a special environment variable that automatically gets set by Vim.
:help $VIMRUNTIME would have already told you this:
You don't normally set $VIMRUNTIME yourself, but let Vim figure it out.
Learn how the excellent and comprehensive :help is structured; all the information is in there (you just need to know how to find it)!

vim: execute shell command with variable and redirect output to new buffer

I want to call msbuild from the function below and redirect the output to a new buffer.
My problem is that I need to use a variable, for the filename, and therefore cant use '!' (can I?), and when I use exe or system() read complains that I'm not giving it a proper file.
func! myFunction()
let findstr = "findstr /s /m " . '"' . expand("%:t") . '"' . " *.vcxproj"
for project in split(system(findstr), nr2char(10))
echo "Building '" . project . "'"
let msbuild = "c:\\windows\\Microsoft.NET\\Framework\\v4.0.30319\\msbuild.exe" . " " . project . " " . "/t:rebuild /p:configuration=debug"
:tabnew | r system(msbuild) "<--THIS LINE HERE
endfor
endfunc
The :read command takes a file not a vim expression. However it can read in from standard output via :read !{cmd}. Example :%r!ls. Using the :execute command you can build your new command with your variable.
exe '%r!' . msbuild
Or you can use :put along with the expression register if you want to use an expression like system(). (Probably want to follow this with :0d_ to delete the first empty line)
put=system(msbuild)
Now it looks like you are trying to build your project and get a list of errors. I would recommend you look into :make, the 'makeprg' option, and the quickfix list as this is a more vim way of building a project.
For more help see:
:h :r!
:h :exe
:h :pu
:h #=
:h :make
:h 'makeprg'
:h quickfix
This is a function you can use to execute arbitrary shell commands and present their output in a new window ( you can put this in your _vimrc):
let s:lastcmd = ''
function! s:RunShellCommand(cmdline, bang)
" Support for repeating last cmd with bang:
let _ = a:bang != '' ? s:lastcmd : a:cmdline == '' ? '' : join(map(split(a:cmdline), 'expand(v:val)'))
if _ == ''
return
endif
let s:lastcmd = _
let bufnr = bufnr('%')
let winnr = bufwinnr(_)
" You can position the new window whenever you want, I chose below + right:
silent! execute winnr < 0 ? 'belowright new ' . fnameescape(_) : winnr . 'wincmd w'
" I could set buftype=nofile, but then no switching back and forth buffers.
" The results are presented just for viewing, not editing, modify at will:
setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile wrap number
setlocal modifiable
silent! :%d
" Useful for debugging, if you encounter issues with fnameescape():
call setline(1, 'You entered: ' . a:cmdline)
call setline(2, 'Expanded to: ' . _)
call append(line('$'), substitute(getline(2), '.', '=', 'g'))
silent execute '$read !' . _
silent! execute 'autocmd BufUnload <buffer> execute bufwinnr(' . bufnr . ') . ''wincmd w'''
" If resizing is unwanted for commands with too much output, remove this line:
silent! execute 'autocmd BufEnter <buffer> execute ''resize '' . line(''$'')'
" You can use <localleader>r to re-execute the last command:
silent! execute 'nnoremap <silent> <buffer> <localleader>r :call <SID>RunShellCommand(''' . _ . ''', '''')<CR>'
execute 'resize ' . line('$')
setlocal nomodifiable
1
endfunction " RunShellCommand(cmdline)
command! -complete=shellcmd -nargs=* -bang Shell call s:RunShellCommand(<q-args>, '<bang>')
Use like:
:Shell gcc -ggdb -o test test.c && ./test

Is it possible to use AutoHotKey to run PSLoggedOn and edit the output in the stream and return the result?

I am using AutoHotKey to build a simple GUI tool that uses the Sysinternals tool PSLoggedOn. First if I use
run psloggedon.exe -l -x \\computername I don't get any output at all.
So I tried run %ComSpec% /C psloggedon.exe -l -x 1> %Temp%\psloggedon.txt and that gives me the output in domain\username format for each user logged in.
Like my first example I would rather just run psloggedon and get the output instead of first opening a command prompt; is that possible somehow?
Either way I want to take the output and avoid writing it to a file, but instead edit the output from the output stream to remove the "domain\" part and then just return the username. How can this be done without any third-party software?
This answer to another question on Serverfault shows a way to do it by reading the StdoutRead.
I too was looking at a way to avoid using a file but have decided it's actually the simplest option.
Code copied from user60738
#include <Constants.au3>
Dim $line, $line2, $file, $icount, $reg, $reg2, $reg3
$file = FileOpen("pclist.txt", 0)
While 1
$line = FileReadLine($file)
If #error Then ExitLoop
$reg3 = ""
$reg2 = ""
$reg = ""
If Ping($line, 200) Then
$reg = #ComSpec & ' /C "' & #ScriptDir & "\pstools\psloggedon.exe -l -x \\" & $line & '"'
$reg = Run($reg, "", #SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
While 1
$reg2 = StdoutRead($reg)
If #error Then ExitLoop
If $reg2 Then
$reg3 &= $reg2
EndIf
WEnd
If StringInStr($reg3, "Error") Then
$reg = "Error"
ElseIf StringInStr($reg3,"No one is logged") Then
$reg = "No one is logged on locally."
Else
$reg = StringTrimLeft($reg3, StringInStr($reg3, Chr(13), "", 3))
$reg = StringTrimLeft($reg, StringInStr($reg, "\"))
$reg = StringTrimRight($reg, StringLen($reg) - StringInStr($reg, Chr(13)))
$reg = StringStripWS($reg, 8)
EndIf
$icount += 1
$line2 &= $icount & #TAB & $line & #TAB & $reg & #CRLF
EndIf
TrayTip("Psloggedon", $icount & " " & $line & " User: " & $reg, 10)
WEnd
FileClose($file)
ConsoleWrite($line2)
FileDelete("C:\output.txt")
FileWrite("C:\output.txt", $line2)
ShellExecute("C:\output.txt")

Resources