Search This Blog

Tuesday, October 12, 2021

Reverse the files

tac command is reverse of cat. It writes each file to stdout, last line first.

If you want to reverse the whole file in vim you can do it by :%!tac 

Sunday, October 3, 2021

Find number of lines selected in visual mode

Command: 𝐠 𝐂𝐓𝐑𝐋-𝐆.

					*g_CTRL-G* *word-count* *byte-count*
g CTRL-G		Prints the current position of the cursor in five
			ways: Column, Line, Word, Character and Byte.  If the
			number of Characters and Bytes is the same then the
			Character position is omitted.
			If there are characters in the line that take more
			than one position on the screen (<Tab> or special
			character), both the "real" column and the screen
			column are shown, separated with a dash.
			See also 'ruler' option.  {not in Vi}


							*v_g_CTRL-G*
{Visual}g CTRL-G	Similar to "g CTRL-G", but Word, Character, Line, and
			Byte counts for the visually selected region are
			displayed.
			In Blockwise mode, Column count is also shown.  (For
			{Visual} see |Visual-mode|.)
			{not in VI}

Sunday, January 31, 2021

Pause Resume Terminal Output

Suppose there is a stream of output being printed in the terminal and you want to pause it to read or to capture the screenshot you can try 𝐂𝐭𝐫𝐥 + 𝐒 (XOFF control command) to freeze the output it can be resumed by 𝐂𝐭𝐫𝐥 + 𝐐 (XON Control Command)

Switching Linux XON/OFF Flow Control:

  • To enable: 𝐬𝐭𝐭𝐲 𝐢𝐱𝐨𝐧
  • To disable: 𝐬𝐭𝐭𝐲 -𝐢𝐱𝐨𝐧

"Long before there were computers, there were teleprinters (a.k.a. teletypewriters, a.k.a. teletypes). Think of them as roughly the same technology as a telegraph, but with some type of keyboard and some type of printer attached to them.

Because teletypes already existed when computers were first being built, and because computers at the time were room-sized, teletypes became a convenient user interface to the first computers – type in a command, hit the send button, wait for a while, and the output of the command is printed to a sheet of paper in front of you.

Software flow control originated around this era – if the printer couldn't print as fast as the teletype was receiving data, for instance, the teletype could send an XOFF flow control command (Ctrl+S) to the remote side saying "Stop transmitting for now", and then could send the XON flow control command (Ctrl+Q) to the remote side saying "I've caught up, please continue".

And this usage survives in Unix because modern terminal emulators are emulating physical terminals (like the vt100) which themselves were (in some ways) emulating teletypes."

Tuesday, January 26, 2021

Insert a file or the result from running an external program into the vim opened file

Suppose you have a file open in the vim and you want to insert output of some shell command into the file instead of closing the file or leaving the current terminal :read can be utilized to directly append the output of shell command into the file.

The :read command can insert a file or the result from running an external program into the current buffer. In case of the shell command it has to be prefixed with the !

Examples:

:read !openssl rand -hex 32 :read /etc/passwd