Below is a code snippet that I get from the internet as an example. Say we are at line 48 (the secong input near the bottom), however, we need to go back to the second of at line 8.Do we just hit :82o?
What if the line number is really large (e.g., 1425) and there are a few of the same word within the line.
In this case, it will take many key strokes, even I have to hit w after I get to the line. How can VIM compete with a mouse in a scenario of random access of a certain word in a large file, in which case you just need to move your pointer with the mouse to achieve this?
Edit: The code is a snippet that I got from the internet in order to demostrate my question. The possible scenario could be I am working on line 1148 and I'd like to go back to line 1108 just to change a word in a comment or I need to fix a bug by making a little bit change within the line 1108. Therefore, the place to go back is hardly predictable and pretty much random.
Edit: I would prefer to know a best practice to achieve this with a vanilla Vim. But you can feel free to provide a solution based on a plugin.
<HTML>
<HEAD><TITLE> Banner</TITLE>
<SCRIPT LANGUAGE= "javascript">
// Puts the text to scroll into variable called sent - SECTION A
// uses length propert to assess its length and put into variable slen
// initalizes a,b,n, and subsent variables
var sent = "This is a demonstration of a banner moving from the left to right. It makes use of the substring property of Javascript to make an interesting display"
var slen = sent.length
var siz = 25
var a = -3, b = 0
var subsent = "x"
// Creates a function to capture substrings of sent - SECTION B
function makeSub(a,b) {
subsent = sent.substring(a,b) ;
return subsent;
}
//Creates a function that increments the indexes of the substring - SECTION C
//each time and calls the makeSub() function to geneate strings
//a indicates start of substring and siz indicates size of string required
function newMake() {
a = a + 3;
b = a + siz
makeSub(a,b);
return subsent
}
//function uses loop to get changing substrings of target - SECTION D
//repeatedly calls newMake to get next substring
//uses setTimeout() command to arrange for substrings to display
// at specified times
function doIt() {
for (var i = 1; i <= slen ; i++) {
setTimeout("document.z.textdisplay.value = newMake()", i*300);
setTimeout("window.status = newMake()", i*300);
}
}
</SCRIPT>
</HEAD>
<BODY >
<HR> <CENTER>
<FORM NAME="z">
<INPUT NAME="textdisplay" TYPE="text" SIZE=25> <P>
<INPUT NAME="doit" Type="button" value = "Run Banner" onClick = "doIt()">
</FORM></CENTER>
<HR>
</BODY></HTML>
Best way to navigate if you know which word you should end up at is to search for it.
I'd do a /of to search for of and I'm there.
I find this easier than moving my mouse pointer.
Now coming to the scenario of really large files, relativenumbers can help you.
set relativenumber
Now to go from 1148 to 1104, you can just do a 40k then use f to get to desired character.
You can prefix f with numbers to move to nth appearance of that character. You can search too.
Again I find this easier than using mouse.
If you have enough command over searching and moving with jk prefixed with motion numbers, you'll be faster than using mouse.
Editing is not something you do at random like in your example. Navigation usually happens from one point of interest to another related point of interest, whether both POIs are part of the same task or parts of related tasks.
Your first example is very unrealistic for two reasons:
because there would be no reason whatsoever to jump from that first POI to that second POI as they are completely unrelated,
because the whole file may not fit wholly in the editing window, which would make the mouse largely irrelevant.
The only potentially useful navigation from your initial state would be to jump to the definition of the event handler, which is only a ?do<CR> away. Note that the function is already under our nose so there's no need to navigate to begin with if all we want is to know what doIt() does.
Your second example is too abstract to work with.
The local navigation tools at your disposal correspond to various zoom levels:
gegEbBwWeE at the word level,
fFtT;,0^$g_g^g$ at the line level,
(){} at the paragraph level,
ggnG at the buffer level,
nHMnL at the window level,
and a bunch of other commands at various levels like hjkl or gm.
But the most powerful of the lot is search, ?/nN (together with set incsearch), which lets you jump directly to wherever you want with minimal effort.
Keyword Search
For random access, keyword search using \{keyword} is normally your best option. Although I have a feeling that this question (with its use of the word of) was geared to sabotage this method, I will point out that you could still run this search by just continuing to type the next word after of. With this approach we are looking at 5-7 keystrokes at most I'd say. That's quite a lot better than using the mouse if you ask me.
EasyMotion
Several others have mentioned the EasyMotion project. I am not too familiar with this project so I will not comment on it any further.
Why to really avoid the mouse?
I do not believe that the added efficiency one acquires by using Vim is the result of avoiding the mouse at all costs; rather, it is the result of training yourself to avoid leaving the home row at all costs. It is not the mechanics of the mouse but the constant task-switching between keyboard to mouse that slows us down the most.
Related
I have different commands my program is reading in (i.e., print, count, min, max, etc.). These words can also include a number at the end of them (i.e., print3, count1, min2, max6, etc.). I'm trying to figure out a way to extract the command and the number so that I can use both in my code.
I'm struggling to figure out a way to find the last element in the string in order to extract it, in Smalltalk.
You didn't told which incarnation of Smalltalk you use, so I will explain what I would do in Pharo, that is the one I'm familiar with.
As someone that is playing with Pharo a few months at most, I can tell you the sheer amount of classes and methods available can feel overpowering at first, but the environment actually makes easy to find things. For example, when you know the exact input and output you want, but doesn't know if a method already exists somewhere, or its name, the Finder actually allow you to search by giving a example. You can open it in the world menu, as shown bellow:
By default it seeks selectors (method names) matching your input terms:
But this default is not what we need right now, so you must change the option in the upper right box to "Examples", and type in the search field a example of the input, followed by the output you want, both separated by a ".". The input example I used was the string 'max6', followed by the desired result, the number 6. Pharo then gives me a list of methods that match that:
To get what would return us the text part, you can make a new search, changing the example output from number 6 to the string 'max':
Fortunately there is several built-in methods matching the description of your problem.
There are more elegant ways, I suppose, but you can make use of the fact that String>>#asNumber only parses the part it can recognize. So you can do
'print31' reversed asNumber asString reversed asNumber
to give you 31. That only works if there actually is a number at the end.
This is one of those cases where we can presume the input data has a specific form, ie, the only numbers appear at the end of the string, and you want all those numbers. In that case it's not too hard to do, really, just:
numText := 'Kalahari78' select: [ :each | each isDigit ].
num := numText asInteger. "78"
To get the rest of the string without the digits, you can just use this:
'Kalahari78' withoutTrailingDigits. "Kalahari"6
As some of the Pharo "OGs" pointed out, you can take a look at the String class (just type CMD-Return, type in String, hit Return) and you will find an amazing number of methods for all kinds of things. Usually you can get some ideas from those. But then there are times when you really just need an answer!
Нello! I'm running a clustered node project with a number of nodes. They do a fair bit of console output. I also want to be able to do beautiful coloured output.
My problem: I'm getting jumbled, race-condition-y console output ONLY WHEN USING COLOURS.
I've been boiling things down to isolate my issue, and my current setup is for every node in the cluster to have its own unique string. This is the only string the node will output to console.
let chars = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let getMyUniqueString = () => {
return chars[Math.floor(Math.random() * chars.length)].repeat(100);
};
I run a bunch of nodes which are using this function to determine their unique strings, and I see something like the following:
Isn't that beautiful! No matter how long and how furiously all those nodes output, this console output never gets jumbled.
Now, I try with unique strings which contain just a tiny bit of colour:
let chars = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let getMyUniqueString = () => {
let redEscSeq = '\x1b[41m';
let clearEscSeq = '\x1b[0m';
let aRedBoxOfText = redEscSeq + ' ' + clearEscSeq;
let repeatedChars = chars[Math.floor(Math.random() * chars.length)].repeat(100);
return aRedBoxOfText + repeatedChars;
};
And look how sad some of my results look!
The ONLY way data is being sent to the terminal, across all nodes, is through the console.log function.
Why is console.log smart enough to keep the output from many nodes unjumbled when there is no colour, but not smart enough to do it when even a bit of colour is included??
Thanks for any help!
(Just for reference, the following image is the kind of unjumbled output I'd expect to see consistently in the coloured case; it's just a red box (two spaces with red background colour) prefixing each line:)
EDIT: While this problem exists in the native windows "cmd.exe" console, in the powershell console, and in ConEmu (a nice 3rd party windows terminal shown in the screenshots), it does NOT exist in the Cygwin terminal! In Cygwin there is never any jumbling, even with tons of colours and async output. Is there anything I can do to encourage this Cygwin behaviour in other consoles??
It is a race condition, and it's unlikely you can do anything about it, maybe except reporting a bug to library used by nodejs.
While windows API itself does in fact support printing multi-colored string in a single API call, as shown here: https://learn.microsoft.com/en-us/windows/console/writeconsoleoutput, where every character contains its color information. But it also doesn't support ANSI escape codes. And this is not what's actually used by javascript.
Nodejs engine uses a library called libuv to write strings to terminal, which is crossplatform and internally translates ANSI escape codes to do the right thing. But if you look closely at the source of libuv for handling ANSI escape codes, you will see that it does a complete buffer flush after every escape code, meaning at some poiint it has to become multiple windows API calls to print one line of text.
Under normal circumstances this is obviously not an issue, but if you have multiple threads doing the writing, it means that parts of those lines may become jumbled... just like what you see here.
So the answer here is that there is no solution. Or you accept using cygwin as a solution.
Experimenting with Mathjax on my site, I face a problem when I type in
$1<x<2$
The outcome will be as follows
This, for example, has no issues.
$x<1\text{ or }x>2$
How do I make the first one display normally?
I have attached the issue at http://teach.sg/mathematics/additional-mathematics/mathjax/.
Since < is used to start a tag in HTML, the browser considers 1<x to be a 1 followed by a tag beginning <x, and everything up to the next > becomes part of that tag. This happens long before MathJax has a chance to look for mathematics on the page, so MathJax is not able to process this math as you intended it.
You have already identified one solution (using \lt and \gt) You can also just use spaces in most cases: $1 < x < 2$.
Instead of using $1<x<2$, use $1\lt x \lt 2$.
Trying to write a visual studio extension that will let me pass in a string and value pair and highlight the value.
I have an extension that looks for specific code and example is that it runs and might returns all if statements
If(someString == someOtherString){
return “This was something”;
}
If(someStringElse == someOtherString){
return “This was interesting”;
}
In this example my value might be the open parentheses that is part of
If(someString
I played around with the tutorial on the MSDN site but it seems to only show an all or nothing. I can get it to highlight all open parentheses and I cannot seem to limit it to only the code block I want (pass in the code blocks I want to search for)
If you're following that prototype, then you can adjust these lines here:
//Find the new spans
FindData findData = new FindData(currentWord.GetText(), currentWord.Snapshot);
findData.FindOptions = FindOptions.WholeWord | FindOptions.MatchCase;
wordSpans.AddRange(TextSearchService.FindAll(findData));
TextSearchService.FindAll is returning the list of spans in the editor that matched the text passed in. Nothing is stopping you from simply changing the spans or computing new ones before calling AddRange. Customize that logic to whatever you want. Obviously, if what you want to highlight doesn't depend upon the position of the caret, there's a fair bit of code you can delete for the determination of currentWord.
I'm using the following groovy code to search a file for a string, an account number. The file I'm reading is about 30MB and contains 80,000-120,000 lines. Is there a more efficient way to find a record in a file that contains the given AcctNum? I'm a novice, so I don't know which area to investigate, the toList() or the for-loop. Thanks!
AcctNum = 1234567890
if (testfile.exists())
{
lines = testfile.readLines()
words = lines.toList()
for (word in words)
{
if (word.contains(AcctNum)) { done = true; match = 'YES' ; break }
chunks += 1
if (done) { break }
}
}
Sad to say, I don't even have Groovy installed on my current laptop - but I wouldn't expect you to have to call toList() at all. I'd also hope you could express the condition in a closure, but I'll have to refer to Groovy in Action to check...
Having said that, do you really need it split into lines? Could you just read the whole thing using getText() and then just use a single call to contains()?
EDIT: Okay, if you need to find the actual line containing the record, you do need to call readLines() but I don't think you need to call toList() afterwards. You should be able to just use:
for (line in lines)
{
if (line.contains(AcctNum))
{
// Grab the results you need here
break;
}
}
When you say efficient you usually have to decide which direction you mean: whether it should run quickly, or use as few resources (memory, ...) as possible. Often both lie on opposite sites and you have to pick a trade-off.
If you want to search memory-friendly I'd suggest reading the file line-by-line instead of reading it at once which I suspect it does (I would be wrong there, but in other languages something like readLines reads the whole file into an array of strings).
If you want it to run quickly I'd suggest, as already mentioned, reading in the whole file at once and looking for the given pattern. Instead of just checking with contains you could use indexOf to get the position and then read the record as needed from that position.
I should have explained it better, if I find a record with the AcctNum, I extract out other information on the record...so I thought I needed to split the file into multiple lines.
if you control the format of the file you are reading, the solution is to add in an index.
In fact, this is how databases are able to locate records so quickly.
But for 30MB of data, i think a modern computer with a decent harddrive should do the trick, instead of over complicating the program.