Vim Highlighting

I highlight my searches in VIM using the following in my .vimrc:

" Highlight search terms...
set hlsearch
set incsearch " ...dynamically as they are typed.
nmap  n :silent :nohlsearch
" stop highlighting with n

While demoing or in a code review it is nice to highlight more than one item. You can use searching to match multiple words:

/term1\|term2

Note: You can also us CTRL+R followed by a ‘/’ to paste the exact last search term

However, it is also nice to do highlights in different colors – or to keep a highlight persistent while you search for other terms. To do this I use:

:match Search /pattern/

Now this will use the same highlight color as normal search. You can replace Search, the name of the highlight group, with another group to highlight with a different color (tab completion works here).

To disable the highlighting from match use:

:match none

Enjoy highlighting!

Advertisements

How can I lock and secure (prevent access) to a Windows 10 User Account without deleting it?

We had a user account that we wanted to preserve as a back-up but we also wanted to make sure that if anyone gained access to the machine, that they wouldn’t be able to access the data.

To do this we used the built in Windows 10 Pro encryption in conjunction with disabling the account. This means that they only way to access the data is from the user account, and that account is disabled. Furthermore, if someone copies the files from that user account they won’t be able to read them unless they are on this original computer under the correct account.

To encrypt the files for this User:

  1. Login to the account to lock, select the C:\User\<name> folder
  2. Right-Click and select Properties
  3. In the General tab click on Advanced…
  4. Encrypt contents to secure data
  5. Click on OK
  6. Click on Apply
  7. Select to “Apply to this folder, all subfolders and files” so that all existing files get encrypted (this may take a long time)
    NOTE: If you only select “Apply to this folder” then only new files will be encrypted

To disable the User account:

  1. Logout of that User account
  2. Login to another User account
  3. Run cmd.exe as administrator
  4. For local User:
    net user “User Name” /active:no
    net user “User Name” /active:yes
    For domain User:
    net user “User Name” /active:no /domain

From this point the only way to access that User’s files are from logging in as that User. The User account is disabled and thus gaining access to the data requires special knowledge. Even if you access that User’s files from another account the files will be unreadable – of if you remove the hard drive and copy the files they will be useless.

You might want to back-up the encryption key. We backed up the data on a separate drive which was protected by an air gap, so we did not back-up the key.

If you want to preserve the key then press the Windows Key and type “encrypt” as a search term, you should see the “Manage file encryption certificates”, select that. This wizard will walk you through creating the security key. You should not store this key on this machine as if it is compromised the key to access the data would also be compromised.

 

Searching large source trees in an efficient way on Linux

TL;DR

Here is the alias:

alias search 'find \!:1 -noleaf -type f -not -path "*/boost/*" -not -path "*/extensions/*" -print0 | xargs -0 -n 100 -P 8 grep -I --color -H -n \!:2*'

 

How do I use it?

Here is how I use it:

search [dir] [term] [grep_options]
e.g.
search ./src/ the\ search\ term
search ./src/ keyTerm -A5 -B5

How does it work?

find

This search alias uses find as follows to locate all files under the provided directory (i.e. first argument) while excluding directories that we don’t care about:

find \!:1 -noleaf -type f -not -path "*/boost/*" -not -path "*/extensions/*" -print0

For aliases remember this:

!* is all but the first
!:0 is only the first, the command itself
!:1 is only the first argument
!:2* is all but the first argument
!$ is only the last argument
!:1- is all but the last argument
!! is all
$0 is the shell
$# is the number of args
$$ is the process id (PID)
$! is the PID of the previous command
$? is the return code from the previous command

Thus, the “\!:1” means only the first argument, and the bang (!) has to be escaped.

\!:1

The “-noleaf” is used because I am normally working on Windows/NTFS mounts and it is not safe to assume that directories containing 2 fewer subdirectories than their hard link count only contain files.

-noleaf

We only want to gather files for searching so I use the “-type f”.

-type f

I normally have very large directories which I do not care to search in, so I specify:

-not -path "*/boost/*" -not -path "*/extensions/*"

Finally for the find command I pass “-print0” which returns null (instead of new line) terminated strings. This adds support for paths with spaces in them:

-print0

xargs

The xargs command controls how many files are being passed into grep and it is handling running them in parallel.

xargs -0 -n 100 -P 8 grep -I --color -H -n \!:2*

The “-0” option is used here to tell xargs that the strings coming in are null terminated (this adds support for files with spaces):

-0

The “-n 100” and the “-P 8” options are where the speed and power of this alias come from. The “-n 100” is telling xargs to pass 100 files from find into grep at a time. The “-P 8” is telling xargs to run 8 grep commands in parallel.

This means that if we have a source tree of 1600 files, then grep will be called 16 times and each will be passed 100 files. The best part is that 8 of those grep commands will be running in parallel each on 100 files, so the command finishes as if there were only two (2) grep invocations – very fast even on large source trees:

-n 100 -P 8

grep

The grep command is used to do the actual searching in files.

grep -I --color -H -n \!:2*

The “-I” option ignores binary files:

-I

Colored results make it much easier to see hits:

--color

Because we are passing in the files to grep it may not show the file name where the hit occurred so we add “-H” to print the file name:

-H

The line number is also important, so we add “-n”:

-n

The ability to control grep is handled with an arguments wildcard. Here the “\!:2*” means the second and all subsequent arguments passed into the search alias. Thus the grep search term and all other grep options can be specified after the directory to search:

\!:2*

The final piece is that the xargs command will add the files from the find command to the grep command. It will add 100 (or less if there are less than 100) files to every grep command and each of those will be run in parallel with up to 8 running at any given time.

Enjoy your searching.

Catching a double free or corruption error with memcheck (a Valgrind tool)

I was randomly getting errors (1 run in 50 would reproduce) like:

$ ./myprogram
*** glibc detected *** double free or corruption (out): 0x093014a4 ***

Linux has a randomization of virtual address space which is supposed to help thwart buffer overflow attacks etc.

This can cause errors to randomly not show, so in the spirit of trying to consistently reproduce the problem I disabled this using:

$ setarch x86_64 -R ./myprogram

This didn’t seem to help.

$ valgrind --tool=memcheck ./myprogram

The output of this produced a:

Invalid free which showed where the error was.

How to generate a ctags files and use it with vim

The ctags command allows you to index source – any source. The command generates a single file called a tags file (which [ironically] is the name of the file). Then you can point to this file with editors like VIM/GVIM/EMACS, etc. for auto-complete and for cross-probing.

Let’s see it in action…

First tag the source you want to index using:

ctags -V -R --c++-kinds=+p --fields=+iaS --extra=+q --languages=c++ .

This will recursively index all files from this point down and generate a ‘tags’ file in the current directory. You should open the tags file and look around to see if it’s getting what you expect and that it isn’t getting things that you don’t expect.

From here we can open vim and start auto-completing. This works because VIM will automatically search the current directory for a tags file, which we just generated. If you want to point to a tags file that lives in another directory, then use:

:set ctags=/path/to/file/with/tags

Now you can start typing. Let’s act like we have a class called MyClass, in VIM you must be in insert mode (press ‘i’) then type…

 MyC<CTRL+N>

…and a drop-down menu should show with the option to auto-complete to MyClass (presuming some MyClass.cpp was indexed with the ctags command).

CTRL+N : to go forward through tags

CTRL+P : to go backward through tags

Now the cool part. Once you’ve auto-completed a word or you put your cursor on any keyword and press CTRL+], then you will see a menu that will let you pick the implementation you would like to go to and once selected VIM will open the source file that defines that keyword.

CTRL+] : to push into tags

CTRL+T : to pop out of keywords

 

How can I run something in one thread and wait for the result in a different thread using Java/JavaFX?

TL;DR

The code is as simple as:

import java.util.concurrent.CountDownLatch;

final CountDownLatch latchToWaitForJavaFx = new CountDownLatch(1);

Platform.runLater(() -> {
  ...REMEMBER THROWS IN THIS THREAD, WILL NOT IMPACT THE CALLING THREAD...
  latchToWaitForJavaFx.countDown();
});

latchToWaitForJavaFx.await();

Details…

Hopefully you would rarely need to synchronize two threads. This is normally an indicator of a poor design – ideally each thread would be able to operate independently. However, I have found a common need to synchronize between threads when testing JavaFX GUIs using JUnit.

For instance, you may want to invoke some GUI behavior, and then check if the GUI was updated. For these cases, you will need to find a way to block the testing thread until the GUI action has completed, this can be done using a CountDownLatch as follows:

import java.util.concurrent.CountDownLatch;
...
@Test
public void showAnchorPane_shouldFocusTextField() throws Exception {
  // Note: We are in the testing thread.  

  // Here we create the count down latch which is thread safe:
  final CountDownLatch latchToWaitForJavaFx = new CountDownLatch(1);

  // We now create a runnable and register that runnable to be called
  // by the JavaFX thread using the Platform.runLater method. This call
  // is asynchronous and returns immediately:
  Platform.runLater(new Runnable() {
    @Override
    public void run() {
      // Do required work on JavaFX thread...
      Stage stage = new Stage();
      Scene scene = new Scene(anchorPane);
      stage.setScene(scene);
      stage.show();

// If we threw before this line, while in the JavaFX thread, then the
// calling thread would never know!

      // Now the work is done we release the testing thread:
      latchToWaitForJavaFx.countDown();
    }
  });

  // Because the last call returned immediately, we wait here until the
  // JavaFX thread has completed it's work for this test:
  latchToWaitForJavaFx.await();

  // Now check if GUI is open and that the right object has focus, etc.
}

Let’s refactor to a function:

private void runAndWaitOnJavaFx(Runnable guiWorker) throws Throwable {
  final CountDownLatch latchToWaitForJavaFx = new CountDownLatch(1);
  Platform.runLater(() -> {
    guiWorker.run();
// If we threw before this line, while in the JavaFX thread, then the
// calling thread never would know!
    latchToWaitForJavaFx.countDown();
  });
  latchToWaitForJavaFx.await();
}

To deal with the  strange behavior if the runnable were to throw and the parent thread wouldn’t know…

private void runAndWaitOnJavaFx(Runnable guiWorker) throws Throwable {
  final CountDownLatch latchToWaitForJavaFx = new CountDownLatch(1);
  final Throwable[] javaFxException = {null};
  Platform.runLater(() -> {
    try {
      guiWorker.run();
    } catch (Throwable e) {
      javaFxException[0] = e;
    } finally {
      latchToWaitForJavaFx.countDown();
    }
  });
  latchToWaitForJavaFx.await();
  if (javaFxException[0] != null) {
    throw javaFxException[0];
  }
}

How can I easily access my Linux command history? Is there a hot key?

One of the fastest ways to search your previous commands is to use CTRL+R and start typing, once you’ve entered enough text you can use CTRL+R again and again to search your history for matches.

Let’s assume we execute the following commands:

$ echo dog
dog
$ echo cat
cat
$ echo hotdog
hotdog

Now press CTRL+R and you will see a new “bck:” prompt at the bottom:

$
bck:

Now if you type “dog” you will see the last command that had that string anywhere in it, populated on the previous command prompt:

$ echo hotdog
bck:dog

Pressing CTRL+R again will cycle through the history:

$ echo dog
bck:dog

You can press ENTER to execute the command, or CTRL+E to go to the end of the command without executing it.

I also hear you can use CTRL+S to go backwords through the search results, but that never works for me – I believe my terminal or window manager is swallowing the CTRL+S.