Less: Quickly Jump to Line Number in Large File

If the file is open you can type:

  • 100g to go to the 100th line.
  • 50p to go to 50% into the file.
  • 100P to go to the line containing 100th byte.

You can use these from terminal by adding + in front of them:

less +100g bigfile.txt

Running Python code in Vim without saving

Is there a way to run my current python code in vim without making any changes to the file? Normally, when I want to test my code from within vim, I would execute this:

:w !python

However, this overrides the current file I am editing. Often, I add print statements or comment stuff out to see why my code isn’t working. I do not want such changes to overwrite a previous version of whatever .py file I’m currently working on. Is there a way to do so? Perhaps a combination of saving to a temporary file and deleting it afterwards?

Solution:

You have already answered your own question:

:w !python

will run the file in python without saving it. Seriously, test it out yourself! make some changes, run :w !python and then after it runs, run :e!. It will revert all of your changes.

The reason this works is because :w does not mean save. It means write, and by default, it chooses to write the file to the currently selected file, which is equivalent to saving. In bash speak, it’s like

cat myfile > myfile

But if you give an argument, it will write the file to that stream rather than saving. In this case, your writing it to python, so the file is not saved.


I wrote a much longer answer on this topic here.

Search and replace the following in vi

Hi I want to replace the occurrence of [x] with [] in vi editor

I have used the following command

:%s/[x]/[]

and it gives me [[]], how to update my command so that I get desired result.

Solution:

You can replace:

  • It once (the first occurrence on the current line) with: :s/\[x\]/[]/
  • It everywhere on the current line with :s/\[x\]/[]/g
  • The first occurence on every line with :%s/\[x\]/[]/
  • It everywhere :%s/\[x\]/[]/g

(Obviously you must make sure you are in command mode, use the esc-key for this)

Able to override the root permission of a readonly file with a non-sudo user

I was performing some experiments in Docker and found a strange behaviour.

I was able to override the ownership of a file created with the root
user inside the Docker with another user without root permissions.

Below are the steps to reproduce it:

$> docker run -dit ubuntu:16.04 bash
$> docker exec -it cont_id bash
$> apt update && apt install -y vim
$> useradd cp -m
$> vim /home/cp/hello.txt
  #  Write some text and save it

$> su cp 
$> cd ~/ && ls -latr;
  #  Will list hello.txt with user and group as root

$> vim hello.txt
  #  Write some text and try saving it normally which will fail.
  #  Try saving it with `:wq!`

Voila, it is saved and the user and group to which the file belongs also change to the new user.

I have done a terminal recording for this and the same is posted here.

Solution:

This is not related to docker, but just normal behavior in vim. As the file is under user directory /home/cp, hence cp user will have all permissions. What wq! command does is to delete the the old one and put new content into /home/cp/hello.txt.

You can quickly test it by creating one more file in the folder that cp has no full permission.

Regex works in Grep but not vim

I have a file of this format, problem.txt:

StarBonds

↯ | 356 Positions | Gain: H$59,834,200 (15.26%)
Security    Shares  &   Paid    Current     Today   ▲   Value   Gain / Loss     ±
AANDE   20,000  !|!     H$26.35     H$29.84     H$0.08      H$596,800   H$69,800    13%
ABASS   20,000  !|!     H$51.68     H$51.29     -H$0.27         H$1,025,800     -H$7,800    -0%
ABRAG   20,000  !|!     H$39.65     H$44.98     -H$0.13     ▼   H$899,600   H$106,600   13%
ABRES   20,000  !|!     H$49.62     H$45.26     -H$0.40         H$1,079,600     H$87,200    8%

I want to remove all lines that do not start with the 2-5 Character, All capital letter identifier.

On the command line with grep, it shows me only those lines and not the ‘junk lines’ (First four at top of example above). i.e. The result I want.
$ grep ‘^[A-Z0-9][A-Z0-9]+’ problem.txt

AANDE   20,000  !|!     H$26.35     H$29.84     H$0.08      H$596,800   H$69,800    13%     
ABASS   20,000  !|!     H$51.68     H$51.29     -H$0.27         H$1,025,800     -H$7,800    -0%     
ABRAG   20,000  !|!     H$39.65     H$44.98     -H$0.13     ▼   H$899,600   H$106,600   13%     
ABRES   20,000  !|!     H$49.62     H$45.26     -H$0.40         H$1,079,600     H$87,200    8%

In vim, with the same regex, it doesn’t find it, even when I remove the beginning of line marker:

:%s/^[A-Z0-9][A-Z0-9]+
%s/E486: Pattern not found: [A-Z0-9][A-Z0-9]+  

When I try removing digits, I get the same error:

E486: Pattern not found: [A-Z][A-Z]+ 

The goal is to change the %s// to %g//d once I can get vim to find the regex.

Using vim on Debian Jessie, grep from same.

Vim is default install, I’m not sure if there’s a flag that needs to be set, other searches haven’t been an issue in the past.

Solution:

In vim + must be escaped, therefore you can keep the specified lines with:

:%g!/^[A-Z0-9][A-Z0-9]\+/d

If you only wish to match lines starting with 2 to 5 character sequences you could instead use:

:%g!/^[A-Z0-9]\{2,5\}/d

How to unfreeze after accidentally pressing Ctrl-S in a terminal?

This feature is called Software Flow Control (XON/XOFF flow control)

When one end of the data link (in this case the terminal emulator) can’t receive any more data (because the buffer is full or nearing full or the user sends C-s) it will send an “XOFF” to tell the sending end of the data link to pause until the “XON” signal is received.

What is happening under the hood is the “XOFF” is telling the TTY driver in the kernel to put the process that is sending data into a sleep state (like pausing a movie) until the TTY driver is sent an “XON” to tell the kernel to resume the process as if it were never stopped in the first place.

C-s enables terminal scroll lock. Which prevents your terminal from scrolling (By sending an “XOFF” signal to pause the output of the software).

C-q disables the scroll lock. Resuming terminal scrolling (By sending an “XON” signal to resume the output of the software).

This feature is legacy (back from the 80’s when terminals were very slow and did not allow scrolling) and is enabled by default.

To disable this feature you need the following in either ~/.bash_profile or ~/.bashrc:

stty -ixon