How to Undo, Redo, Repeat Command and Search Commands in Vi

1. Introduction

In this article, we’ll talk about undoing and redoing actions in Vi as well as repeating commands and navigating the command history.

2. Undoing and Redoing in Vi

2.1. Undo

While being in normal mode, press u to undo the latest change regardless of where it happened (no need to position the cursor). You can press u repeatedly to undo preceding changes.

You can also use U (uppercase u) to revert all changes done to the line in which the cursor is positioned (this will regress it to its original state).

Note that U is not a “true” undo as it doesn’t use the undo tree to revert the changes. This means that you can actually undo the U command.

3. Redo

For redoing the last undone change, regardless of where it happened, just press Ctrl+R. As with u, this will traverse the undo tree so it can be pressed repeatedly to redo several changes.

Repeating Commands

In Vi, you can also repeat the last colon command you have executed. The last command entered with : can be repeated with @: and further repeats can be done with @@.

This is useful for commands like :+5.

Traversing the Command History

Every command executed gets saved in the command history. This history can be traversed by typing : and then using the up and down arrows.

In order to see the list of command history, you can type q: (note that with q/ you’ll get the search history.

Setting the History Size

You may have Vi’s history set to 0. If this happened, the last option wouldn’t work.

The solution would be to run :set history=100, then you’ll be able to use the up arrow to view previous commands.

Conclusion

With this, we’ve learned how to undo and redo in Vi and how to traverse the command history.

Advertisements

How to Copy, Cut, and Paste in Vi

In this article, we’ll show you how to copy, cut and paste in vi.

This will be explained both selecting text as using movement commands. We’ll also tell you how to copy and cut multiple chunks of text.

1. Introduction

To remember easily the commands used for copying and cutting in vi, it’s useful to know the words they come from:

  • d comes from delete, which you usually know as cut.
  • y comes from yank, which you usually know as copy.
  • p comes from paste, so nothing new here.

Keeping this in mind, you may find it easier to memorize the commands.

2. Selecting, Copying (or Cutting) and Pasting Text

We’ll start with normal mode and position the cursor at the start of the text that we want to copy. Then we’ll do the following steps:

  • Press v to start selecting.
  • You can also use V (capital v) to select whole lines or Ctrl+v to select rectangular blocks.
  • Move the cursor to the end of the text you want to copy.
  • Press y to copy the text (or d to cut it).
  • Move the cursor where you want to paste the text.
  • Press p to paste the text after the cursor (or P (capital p) to paste it before the cursor).

3. Copying and Cutting with Movement Commands

As well as you can copy and cut by selecting the text that you want to, you can also use movement commands to specify what text will be selected. This way you can do:

  • yy or `Y — copy the whole line (including the new line character).
  • y$ — copy from the cursor to the end of the line (excluding the new line character).
  • y0 — copy from the cursor to the start of the line.
  • yiw— copy the current word (excluding surrounding whitespace).
  • yaw— copy the current word (including surrounding whitespace).

4. Multiple Copying or Cutting

To copy or cut several chunks of text at the same time, you’ll need to use vi’s registers. A register is a location in Vim’s memory identified with a single letter. A double quote plus character is used to specify that the next letter typed is the name of a register (so "a would be the a register).

After this short explanation, for doing this you should type "ay and yank that word to the a register. Then you’ll be able to paste that word anywhere in the text using "ap" (or“aP` if you want to paste it before the cursor).

4.1. A Little More About Vi’s Registers

  • By default, all copying and cutting operations are stored in the unnamed registry "" (also named quotequote).
  • A double quote plus an uppercase character is used to append content to a register(with "Ay you would append a yanked text to the a register).
  • There are more default registers, some of them are:
  • "+ for Linux clipboard.
  • "* for Windows clipboard.
  • "0 will store the text of the last yank.
  • "1 will store the text from the last delete.
  • All current registers can be listed the list of all of them with the :reg command.

5. Conclusion

At this point, we’ve learned how to copy and cut text in vi.

To sum it up, you need to v select, y copy, d cut and p paste.

How to make git skip asking for password

Since Git 1.7.9 (released in late January 2012), there is a neat mechanism in Git to avoid having to type your password all the time for HTTP / HTTPS, called credential helpers.

So, with Git 1.7.9 or later, you can just use one of the following credential helpers:

git config --global credential.helper cache

… which tells Git to keep your password cached in memory for (by default) 15 minutes. You can set a longer timeout with:


git config --global credential.helper "cache --timeout=3600"

You can also have Git store your credentials permanently using the following:


git config credential.helper store

GitHub’s help also suggests that if you’re on Mac OS X and used Homebrew to install Git, you can use the native Mac OS X keystore with:


git config --global credential.helper osxkeychain

For Windows, there is a helper called Git Credential Manager for Windows or wincred in msysgit.


git config --global credential.helper wincred

For Linux, you can use gnome-keyring(or other keyring implementation such as KWallet).

Bootstrap 3 & JQuery Filechooser

I found here a fast way of reshaping a filechooser using Bootstrap 3 and JQuery that is basically:

Style

.input-group-file .input-group-file-btn{
    position: relative;
    overflow: hidden;
}

.input-group-file .input-group-file-btn input[type=file]{
    opacity: 0;
    position: absolute !important;
    top: 0;
    right: 0;
    min-width: 100%;
    min-height: 100%;
    font-size: 100px;
    text-align: right;
    filter: alpha(opacity=0);
    outline: none;
    background: white;
    cursor: inherit;
    display: block;
}

JS

$(document).ready(function () {
    $(document).on('change', '.input-group-file-btn :file', function () {
        var input = $(this);
        var label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
        // find text sibling and set value
        input.parent().siblings('input[type="text"]').val(label);
    });
});

HTML example

<div class="input-group input-group-file">
  <input id="file" type="text" class="form-control" readonly=""/>
  <span class="btn btn-primary btn-file input-group-addon input-group-file-btn">
    Search
    <form:input class="form-control" type="file" path="multipartFile" cssErrorClass="form-control field-error"/>
  </span>
</div>

Using a WAR module as dependency in Maven

The Pragmatic Integrator

Today I created a Maven module which holds our integration tests. The disadvantage when holding these tests in your ‘normal’ Maven modules is that the tests are fired every time the module is built (by you or your continuous integration server like Hudson). Since these tests take rather long to execute, developers tend to use the ‘skipTests=true‘ option when they build the code, which is taking away a great part of one the main reasons why you should use Maven. See also this article for more info about this subject.

View original post 295 more words

Howto Manual Hot Deploy for Liferay

Steps for getting manual hot-deploy for Liferay:

  1. Set developer mode to active by adding -Dexternal-properties=portal-developer.properties in file setenv.sh (or setenv.bat on Windows) located in Liferay’s bundled Tomcat bin directory.
  2. For CSS:
    1. Copy the CSS files (which should preferably be in the same directory, eg. CSS) to a directory with the same name to the following paths:
    • ${liferay.tomcat}/webapps/${app.folder}/css
    • ${liferay.tomcat}/webapps/${app.folder}/css/.sass_cache
    • ${liferay.tomcat}/temp/${app.temp.folder}/css
    • ${liferay.tomcat}/temp/${app.temp.folder}/css/.sass_cache
    • Please note that if ${app.folder} is for example my-app, ${app.temp.folder} usually has the same name prefixed by a number, eg. 10-my-app.
    1. Remove ${liferay.tomcat}/work/Catalina/localhost/${app.folder} dir.
    2. When reloading the page, Liferay will load the necessary files on the work directory and so the new styles will be used without the need to generate and deploy a new war.
  3. The same process can be used for jsp files ommiting the step for the .sass_cache dir.

Grep Search with Result Context

In this entry, we’ll explain how to show a context when doing a grep search.

With context, we mean that before and/or after the line in which grep will show a search hit, it will also output one or more extra lines of the text that surrounded the line in which the hit was found.

1.  Snippet

To look for foo with 2 lines of context we’ll use:

grep -C2 foo

This means that if we have the following text:

one
two
three
foo
five
six
seven

We’ll get as grep’s output this:

two
three
foo
five
six

2.  Deepening

For getting a context when searching with grep you must use the -Cnum parameter (with num being a positive integer with the number of lines of context that we want). Note that if 2 is used, there will be 2 lines before and another 2 lines after each hit.

You can also use directly -num for the same purpose.

2.1. Extra

As noted by srmafias you can also use -B num for a context only before the hit and -A num for a context only after the hit.