My .vimrc is organized into multiple sections and subsections, each associated with a foldmarker.
How can I easily sort the folds alphabetically by their description, retaining the rest of the fold block as it is?
There is no requirement to do the sorting in vim itself, as long as the process is not manual.
I.e. the following section:
" Syntastic --------------------------------------------------{{{3
foo bar baz
" Emmet.vim --------------------------------------------------{{{3
let g:user_emmet_mode='n'
let g:user_emmet_leader_key='<leader>e'
should look like this after sorting:
" Emmet.vim --------------------------------------------------{{{3
let g:user_emmet_mode='n'
let g:user_emmet_leader_key='<leader>e'
" Syntastic --------------------------------------------------{{{3
foo bar baz
There is an older question, which concerns itself with a similar problem, the solutions offered were not of much help for my particular issue.
Related
Here is what I currently have; the only problem being the external file loads without the placeholder text updated -- instead rather, the placeholder text just says '[NOUN]' instead of actual noun inserted from user in earlier program prompt.
Update; cleaned up with #tadmans suggestions, it is however, still not passing user input to placeholder text in external .txt file.
puts "\n\nOnce upon a time on the internet... \n\n"
puts "Name 1 website:"
print "1. "
loc=gets
puts "\n\Write 5 adjectives: "
print "1. "
adj1=gets
print "\n2. "
adj2=gets
print "\n3. "
adj3=gets
print "\n4. "
adj4=gets
print "\n5. "
adj5=gets
puts "\n\Write 2 nouns: "
print "1. "
noun1=gets
print "\n2. "
noun2=gets
puts "\n\nWrite 1 verb: "
print "1. "
verb=gets
puts "\n\nWrite 1 adverb: "
print "1. "
ptverb=gets
string_story = File.read("dynamicstory.txt")
puts string_story
Currently output is (i.e. placeholders not populated):
\n\nOnce upon a time on the internet...\n\n
One dreary evening while browsing the #{loc} website online, I stumbled accross a #{adj1} Frog creature named Earl. This frog would sit perturbed for hours at a time at the corner of my screen like Malware. One day, the frog appeared with a #{adj2} companion named Waldo that sat on the other corner of my screen. He had a #{adj3} set of ears with sharp #{noun1} inside. As the internet frogs began conversing and becoming close friends in hopes of #{noun2}, they eventually created a generic start-up together. They knew their start-up was #{adj4} but didn't seem to care and pushed through anyway. They would #{verb} on the beach with each other in the evenings after operating with shady ethics by day. They could only dream of a shiny and #{adj5} future full of gold. But then they eventually #{ptverb} and moved to Canada.\n\n
The End\n\n\n
It's important to note that the Ruby string interpolation syntax is only valid within actual Ruby code, and it does not apply in external files. Those are just plain strings.
If you want to do rough interpolation on those you'll need to restructure your program in order to make it easy to do. The last thing you want is to have to eval that string.
When writing code, always think about breaking up your program into methods or functions that have a specific function and can be used in a variety of situations. Ruby generally encourages code-reuse and promoting the "DRY principle", or "Don't Repeat Yourself".
For example, your input method boils down to this generic method:
def input(thing, count = 1)
puts "Name %d %s:" % [ count, thing ]
count.times.map do |i|
print '%d. ' % (i + 1)
gets.chomp
end
end
Where that gets input for a random thing with an arbitrary count. I'm using sprintf-style formatters here with % but you're free to use regular interpolation if that's how you like it. I just find it leads to a less cluttered string, especially when interpolating complicated chunks of code.
Next you need to organize that data into a proper container so you can access it programmatically. Using a bunch of unrelated variables is problematic. Using a Hash here makes it easy:
puts "\n\nOnce upon a time on the internet... \n\n"
words = { }
words[:website] = input('website')
words[:adjective] = input('adjectives', 5)
words[:noun] = input('nouns', 2)
words[:verb] = input('verb')
words[:adverb] = input('adverb')
Notice how you can now alter the order of these things by re-ordering the lines of code, and you can change how many of something you ask for by adjusting a single number, very easy.
The next thing to fix is your interpolation problem. Instead of using Ruby notation #{...}, which is hard to evaluate, go with something simple. In this case %verb1 and %noun2 are used:
def interpolate(string, values)
string.gsub(/\%(website|adjective|noun|verb|adverb)(\d+)/) do
values.dig($1.to_sym, $2.to_i - 1)
end
end
That looks a bit ugly, but the regular expression is used to identify those tags and $1 and $2 pull out the two parts, word and number, separately, based on the capturing done in the regular expression. This might look a bit advanced, but if you take the time to understand this method you can very quickly solve fairly complicated problems with little fuss. It's something you'll use in a lot of situations when parsing or rewriting strings.
Here's a quick way to test it:
string_story = File.read("dynamicstory.txt")
puts interpolate(string_story, words)
Where the content of your file looks like:
One dreary evening while browsing the %website1 website online,
I stumbled accross a %adjective1 Frog creature named Earl.
You could also adjust your interpolate method to pick random words.
I have a bash autocompletion function which consults a database to provide possible completions for the current command line in bash. It takes about 3 seconds to complete and during this time the user doesn't have any indication that he has triggered the autocompletion -- prompting him to press TAB a few more times, leading to the autocompletion possibly being run several more times.
Is it possible to manipulate the command line in a way to show that there is something being done, as soon as autocompletion is initiated? For example, when I press TAB twice after the command foo, I would like the following to happen immediately:
$ foo
autocompleting...
and then after the possible completions are determined, to change to:
$ foo
bar baz jazz
Alternatively, if I type foo j and press TAB once, the following:
$ foo j
autocompleting...
should change to
$ foo jazz
without any extra text beneath it, as expected.
The next step would be to consider if it's possible to have a dynamic output while the autocompletion is running, for example, printing the characters \ | / - in a single place, as if a line is rotating in place -- to visually indicate that something is happening. Would this be possible?
Sure, it's possible, I think you'll find everything you need in this answer (so this might be considered a duplicate question):
Using BASH to display a progress (working) indicator
Your question is slightly different in that you'll want to remove the spinner and then output your results. So you might consider inverting the process that is backgrounded. In other words, background your spinner, and then it can be explicitly killed when your autocomplete function is finished (but prior to outputting results if that's possible for your code).
I'm trying to come up with a way to approach typehead in such a way that uniq substrings are given priority. For example, lets say I have the following:
foo.bar
foo.bar.baz
foo.bar.biz
foo.bar.bat
foo.bar.bat.art
foo.bar.bat.zap
...
foo.dog.alt
foo.dog.rar
...
foo.zed
foo.zed.rarg
Due to the drill down nature of the data, as the users types, the best thing to see what be the next unique option (since the list below the typeahead box would limited in length). So for example if someone where too type foo, they would see foo.bar, .foo.dog.alt, and foo.zed.
Any suggestions on how to approach this?
I'm working on a Ruby gem and I would love to be able to hide all the documentation comments in the file because they are more for people using the library than reading or writing the code. I see the value in having the comments, but when I'm working on the code they are visually distracting to me.
In MacVim I can manually fold lines of code by selecting them and clicking Tools > Folding > Create Fold, but is there a way to automatically hide all comments using some sort of shortcut?
For example, the following code:
# Returns a 2D array for Rails select helper options.
# Also used internally for Formtastic support
#
# ==== Example
# # Create an Enum with some elements
# class Priority < ClassyEnum::Base
# end
#
# class Priority::Low < Priority; end
# class Priority::ReallyHigh < Priority; end
#
# Priority.select_options # => [["Low", "low"], ["Really High", "really_high"]]
def select_options
map {|e| [e.text, e.to_s] }
end
would be displayed as:
def select_options
map {|e| [e.text, e.to_s] }
end
You could try this method:
:set fdm=expr
:set fde=getline(v:lnum)=~'^\\s#'?1:getline(prevnonblank(v:lnum))=~'^\\s#'?1:getline(nextnonblank(v:lnum))=~'^\\s*#'?1:0
The problem is that this method would become the only folding option so that's probably a little bit extreme.
I guess you would like to play with vim’s foldmethod setting. Sorry for slightly vague answer, but I have no MacVim here so you are supposed to adjust directories/filenames in my suggestion yourself.
First of all, try :setlocal foldmethod=syntax in command mode to enable folding within your current file only. If it works, you have all the prerequisites installed (namely, the ruby.vim syntax file.) Try to add let ruby_fold=1 to your .vimrc file. The latter should enable folding over all ruby files (or, alternatively you may explicetely set folding to true for all filetypes supporting folding with set foldmethod=syntax.)
Now you are to find ruby.vim over your file system to tune it up. To give a hint, on Linux distros it’s located at /usr/share/vim/vim73/syntax/ruby.vim. My syntax file enables folding for all the stuff which “may” be folded (e. g. functions, methods, etc.) Copy the original file to your $HOME/.vim/syntax directory and adjust it according to your needs. Navigate through it (by searching for fold, for instance) and remove fold keyword where you don’t want the folding is applied. The names in syntax file are self-explanatory, so you would not be in trouble here.
Restart vim and enjoy your folding. Hope that helps.
I'd like to be able to mix text and computation. Something like this:
blah blah blah ...
blah blah ... The average mass is (m1 + m2 + m3)/3 = 23.4 g ... blah blah
blah blah blah ...
Where the "(m1... )/3" is the input, and the "23.4" is the output. Right now I only know how to show input in one cell, and the output in another cell below it.
Is this possible?
Update: I want to include these bits of computation in the midst of larger blocks of writing, so I'm not sure how to use a Print statement as Koantig suggested, because it seems I'd have to concatenate an entire paragraph/cell worth of strings and styles.
thanks,
Rob
I suppose that you have entered your expression in a text cell. If you highlight the expression to evaluate, in your case
(m1 + m2 + m3)/3
and hit Shift+Ctrl+Enter (on my Windows box, not sure about your box, but it's option Evaluation | Evaluate in Place if you prefer the menu), then your expression will be replaced by the result of evaluating it. I know that this is not exactly what you want, but it's the closest I have found myself. I copy the expression to the rhs of the = sign and evaluate the copy.
I expect that someone will come along soon and tell us the smart way to do this.
Maybe something like this?
m1 = 10;
m2 = 20;
m3 = 50;
f = "(m1+m2+m3)/3";
Print[f <> "= " <> ToString#N#ToExpression#f <> " g"]
The result:
(m1+m2+m3)/3= 26.6667 g
For smaller projects, I have a scratch notebook and a presentation notebook. Calculations are performed in the scratch notebook, then copied into the presentation notebook.
For larger projects, I attach the computations to the section headers, then fold the section down to just the header for presentation purposes. There's still cutting and pasting.
I have once written a notebook that parsed a pair of scratch/presentation notebooks into a final notebook where specially marked sections of the presentation notebook were replaced with results computed in the context of the scratch notebook. This was sufficiently difficult to maintain that I have never repeated the experience.
Which did you want: visible, editable expressions to be evaluated, or dead, fixed results of (past) evaluations?