VIM prompting for a variable when running a macro? - session

I find I waste a lot of time closing and reopening sets of files so I'd like to improve my VIM macro for loading and saving the session to support multiple sessions.
I'd like for it to prompt for a string value, so that I could press my shortcut, then type in for example "foo", and have my macro save the session to .foo (so I also need to do basic string concat on it). Then I'd do the same for the load macro and manage sessions by theme (using MVC framework you tend to have a lot of files to work with).
" Control-S to save and Shift F5 to load
set sessionoptions=tabpages,winpos
map <S-F5> :source ~/.vim/.session<cr>
map <c-s> :mksession! ~/.vim/.session<cr>\| :echo "Session saved."<CR>
I have very little experience of VIM scripting. Is it possible to do this in a one liner, or perhaps a small function?
Thank you.

map <s-f5> :execute "source ".input("session name: ", "~/.vim/session.", "file")<cr>
Enter "foo" to load "session.foo".
Instead, you can also do:
map <s-f5> :source ~/.vim/session.
Note there isn't a <cr>, so you complete the command yourself and press enter — identical typing as above, even down to filename completion.
However, I'd look at calling a function or something else entirely at about this point.

Here's the snippet I have now, in case someone needs something similar (no need to vote). It saves sessions under .session.xyz which are also excluded from my Git project. I like to store them in the Git project folder so they are saved with backups.
I like confirmation echo as well because when you press enter after saving the session otherwise you can't see that anything happened. It's just for feedback.
map <S-F5> :execute "source ".input("Load session: ", "~/Some/Project/.session.", "file")<cr>
map <c-s> :execute "mksession! ".input("Save session: ", "~/Some/Project/.session.", "file")\| :echo "Session saved."<CR>
The file completion makes this very handy, thank you!

Related

How to display debug info or console.log equivalent in Lua

I am creating many games using Lua and LOVE2D, but whenever I implement a new function and want to test it out, or simply want to know a value of a variable in Lua, I either display it on the game screen or just hope that it works.
Now my question is...
IS THERE A WAY TO DISPLAY SOME INFO, such as A VARIABLE VALUE or something else into the terminal or somewhere else? Just like console.log in javascript which displays some content in the javascript console in the browser. So, is there a way to do this is Lua?? using LOVE2D?
I am using a Mac, so I have a terminal and not a command prompt. Is there a way to display some content there? Anywhere else would also be fine, I just need to see if those values are as expected or not.
Use a conf.lua file to enable the console, then you should be able to use a standard print(). You can read the wiki entry here.
Note: You have to run Lua and Love2D via the terminal for this to work. Running Lua and Love2D like this is required for the print statements to show:
/Applications/love.app/Contents/MacOS/love "/Users/myuser/Desktop/love2d-test-proj"
You just need to add a conf.lua file to the same location where your main.lua. Your file may be as simple as this:
function love.conf(t)
t.console = true
end
But feel free to copy the whole configuration file from the above link and edit what you need.
I can't be completely sure about this, because I have no access to Mac, but the console is disabled by default and even on Windows, no prints are shown until you turn it on.
Alternatively You can also display debug info in the game itself like some games do.
What I like to do is add something like debugVariable = {} for logging events that happen in each loop and debugPermanent = {} for events that happen rarely. Possibly add convenience functions for writing to the variables:
function debugAddVariable(str)
table.insert(debugVariable, str)
end
--..and similarly for debugPermanent
Now a function to draw our debug info:
function debugDraw()
love.graphics.push() --remember graphics state
love.graphics.origin() --clear any previous transforms
love.graphics.setColor(--[[select color for debug info]])
love.graphics.setFont(--[[select font for debug info]])
for i, v in ipairs(debugPermanent) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
for i, v in ipairs(debugVariable) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
debugVariable = {} --clear debugVariable to prepare it for the next loop
love.graphics.pop() --recall graphics state
end
And we just call this draw function at the end of our love.draw() and the texts should appear.
Obviously, this method can be refined further and further almost infinitely, displaying specific variables, and adding graphs for some other variables to clarify the information you want to show, but that's kind of outside of the scope of the question.
Lastly Feel free to check here for debug libraries submitted by users.

is there a way to stop the popping up of the md5.exe tool while it generates the hash for a file

i have written a program in vbscript for which i have used md5.exe to generate hash. since there are many files to which the hash has to be generated, the md5 hash repeatedly generates hash for each file one after the other. but while this process is in progress, i can see it popping out on the screen as it generates the hashes ( it does not pop out the hashes, the tool itself pops out on the screen repeatedly). i want to do something such that it stops popping out yet generate the hash for all the files. please help guys!
Generally, when running a command, youj can supress its output by directing it to a file, then deleting the file
md5.exe blahblah >null the >null being the critical part. Note however, that null doesn't directly it to a magical blackhole. It creates a file named null and prints the output there
This would supress the output. IF you posted some code, I could have told you how to do that there. but if you are running it as a shell exec, this should work
You can use the run method to supress the window.
Set WshShell = CreateObject("WScript.Shell")
cmd= "C:\Users\Administrator\desktop\experimenting\md5.exe"
'OR whatvere your whole command is
cmdRun = WshShell.Run(cmd,0,true);
Answer from here
Want to hide command prompt window in using WshShell.Exec method
(Upvote the guy if you find it useful)
Please note that you have tro use the output file to read. You cannot read the output from the shell anymore.

What is the best way to get keyboard events (input without press 'enter') in a Ruby console application?

I've been looking for this answer in the internet for a while and have found other people asking the same thing, even here. So this post will be a presentation of my case and a response to the "solutions" that I have found.
I am such new in Ruby, but for learning purposes I decided to create a gem, here.
I am trying to implement a keyboard navigation to this program, that will allow the user use short-cuts to select what kind of request he want to see. And in the future, arrow navigations, etc.
My problem: I can't find a consistent way to get the keyboard events from the user's console with Ruby.
Solutions that I have tried:
Highline gem: Seems do not support this feature anymore. Anyway it uses the STDIN, keep reading.
STDIN.getch: I need to run it in a parallel loop, because at the same time that the user can use a short-cut, more data can be created and the program needs to show it. And well, I display formated text in the console, (Rails log). When this loop is running, my text lost the all the format.
Curses: Cool but I need to set position(x,y) to display my text every time? It will get confusing.
Here is where I am trying to do it.
You may note that I am using "stty -raw echo" (turns raw off) before show my text and "stty raw -echo" (turns raw on) after. That keeps my text formated.
But my key listener loop is not working. I mean, It works in sometimes but is not consistent. If a press a key twice it don't work anymore and sometimes it stops alone too.
Let me put one part of the code here:
def run
# Two loops run in parallel using Threads.
# stream_log loops like a normal stream in the file, but it also parser the text.
# break it into requests and store in #requests_queue.
# stream_parsed_log stream inside the #requests_queue and shows it in the screen.
#requests_queue = Queue.new
#all_requests = Array.new
# It's not working yet.
Thread.new { listen_keyboard }
Thread.new { stream_log }
stream_parsed_log
end
def listen_keyboard
# not finished
loop do
char = STDIN.getch
case char
when 'q'
puts "Exiting."
exit
when 'a'
#types_to_show = ['GET', 'POST', 'PUT', 'DELETE', 'ASSET']
requests_to_show = filter_to_show(#all_requests)
command = true
when 'p'
#types_to_show = ['POST']
requests_to_show = filter_to_show(#all_requests)
command = true
end
clear_screen if command
#requests_queue += requests_to_show if command
command = false
end
end
I need a light in my path, what should I do?
That one was my mistake.
It's just a logic error in another part of code that was running in another thread so the ruby don't shows the error by default. I used ruby -d and realized what was wrong. This mistake was messing my keyboard input.
So now it's fixed and I am using STDIN.getch with no problem.
I just turn the raw mode off before show any string. And everything is ok.
You can check here, or in the gem itself.
That's it.

Prevent vim from loading sessions when reading from stdin

I've been customizing my .vimrc a lot lately and love the power and convenience that :mksession gives me. I currently have the following in my .vimrc to autoload sessions:
function! LoadSession()
if argc() == 0 && ! &diff
let g:sessiondir = $HOME . "/.vim/sessions" . getcwd()
let g:sessionfile = g:sessiondir . "/session.vim"
if (filereadable(g:sessionfile))
exe 'source ' g:sessionfile
else
echo "No session loaded." + argc() + argv()
endif
else
let g:sessionfile = ""
let g:sessiondir = ""
call ResCur()
endif
endfunction
I then call this with au VimEnter * nested :call LoadSession(). This works great for most cases, except when vim is reading from stdin. In that case the session is still loaded, however I want to prevent that from happening. I would have thought the argc() == 0 conditions would be enough, but it appears that the - that vim is being called with to read from stdin causes argc() to not return 0. Poop! ;]
I've tried all sorts of things from looking at argv(0) (it's empty in this case - why?), trying to find ways of identifying that vim is reading from stdin (it shows a message that it's doing so, but I can't figure out how to tap into that), etc., but no luck so far.
I'm sure I'm missing something terribly obvious here, but the Googles and vim :help isn't getting me anywhere, so I'm hoping some kind soul here can shed some light on this for me.
What I found works is having
autocmd StdinReadPre * let g:my_is_stdin = 1
in your .vimrc and then test for exists("g:my_is_stdin") in your session saving/loading functions. Mind that these have to be run also via autocmd on events VimLeave/VimEnter for this scheme to work.
The session.vim plugin that I'm using offers extended session handling. Among others, it asks whether a previously saved session should be restored on Vim startup.
But unless you need the other functionality of the plugin, your workaround with mappings triggering the restore is probably fine, too.
I've been fiddling a lot with loading sessions on Vim startup, and ultimately decided that's it's not a very good idea, mainly because it doesn't work well with plugins.
I ultimately added some mappings for saving and restoring a session. This has the bonus that you don't have to mess arround with your session when you're doing quick edits.
map <leader>ss :call CustomSessionSave()<CR>
map <leader>sl :call CustomSessionRestore()<CR>
map <leader>sd :call CustomSessionDelete()<CR>
Maybe this helps

I get this window while editing Ruby Files in Vim. What is it?

I usually get this new window open up suddenly while I am editing a Ruby file in VIM. This is getting irritating because, i cant type in anything while its processing. And it usually happens arbitarily. Does any one here know which plugin could be doing this? Or is this somekind of VIM's process?
This is happening when you hit K in normal mode.
K Run a program to lookup the keyword under the
cursor. The name of the program is given with the
'keywordprg' (kp) option (default is "man"). The
keyword is formed of letters, numbers and the
characters in 'iskeyword'. The keyword under or
right of the cursor is used. The same can be done
with the command >
:!{program} {keyword}
There is an example of a program to use in the tools
directory of Vim. It is called 'ref' and does a
simple spelling check.
Special cases:
- If 'keywordprg' is empty, the ":help" command is
used. It's a good idea to include more characters
in 'iskeyword' then, to be able to find more help.
- When 'keywordprg' is equal to "man", a count before
"K" is inserted after the "man" command and before
the keyword. For example, using "2K" while the
cursor is on "mkdir", results in: >
!man 2 mkdir
- When 'keywordprg' is equal to "man -s", a count
before "K" is inserted after the "-s". If there is
no count, the "-s" is removed.
{not in Vi}
If you notice, it's running ri in the open window, which is the ruby documentation app.
In Unixy environments, the help program normally runs inline, just displacing the vim output for a minute.
Is this using gvim, or command-line vim?
In either case, you can try monkeying with 'keywordprg' to fix the popup
Or, if you can't train yourself not to type it, you can just use :nnoremap K k to change what K does (in this case, just treat it as normal k command and go up one line).
I have this same issue on my work desktop, but not my home machine. The setups are near identical.
While stalking down a possible cause, I noticed that when I leave my cursor over a Ruby symbol such as File, Vim would popup a short description of the File class. After comparing all the various vim scripts and ri-related files that I could find, I finally settled on the only solution that worked...
Open $HOME/_vimrc and add the following line:
autocmd FileType ruby,eruby set noballooneval
Previously, I commented out a block in $VIMRUNTIME/ftplugin/ruby.vim, but Brian Carper suggested a better solution of :set noballooneval. I added the autocmd line so it is only executed with Ruby files.
If anyone figures out a true solution, please contact me. :(

Resources