Garbled text while pasting in vim


If you paste text in vim and sometimes it distorts the text like some extra indentation gets added. This is a pain for those who work with python ūüėõ
This problem occurs when there is auto indent or smart indent enabled. There might be other reasons.
To solve this simply type in command mode:

:set paste

this will put vim in paste mode. And try pasting the same text now. It works.
To disable the paste mode, type:

:set nopaste

You may also try :set noai and :set nosi for disabling autoindent and smartindent respectively. When you are done pasting, do :set ai and :set si

Vim tweaks


Get vim awesome vimrc

You can find the vimrc on GitHub from many repositories. I found https://github.com/amix/vimrc and I just gave it a try. You can read the Readme file for more on the installation of vimrc.

.vimrc is a file that is stored in the home directory (~/) usually and it stores the configuration settings related to Vim.


Note: You should remain in the command mode while performing selections etc. operations below. Be sure, by pressing the ‘Esc’ button (default).

Some Vim commands that I learnt while experimenting are discussed below.

  • Indent blocks:

First of all, select block of lines using v. Press v to start selecting and then press j to go down while selecting lines. Or simply press V to select whole lines and then press >> to indent the whole selected block the right and << to do the same to the left. Then press dot to repeat the same procedure (i.e. indent left or right). You may also do it for a single line.

You can also visually select the blocks using Ctrl-v. Then press jj and ll repeatedly to select a block of text.

This may be helpful while performing any operation on a selected text or block.

First select a visual block using above methods.

Then press the : (colon key or shift+;) 

You might notice something already written there when you press the ‘colon’ key. Don’t press Enter or selection will be lost.

Then you can perform operations like search and replace by pressing s/text-to-search/to-be-replaced-with/ immediately after pressing the colon.

  • gg to move to the¬†top of the file.
  • 0 to move to the beginning of the line.
  • Just press any number and then the command to repeat that command the number of times. e.g. press 7 and then j. We know that pressing j will let us go down by one line. But 7 j will do the j, 7 times i.e. move to 7 lines down.
  • To insert some text in the beginning of each line or some line: ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬†select the lines using v or V buttons and then press colon and then type s/^/% /

To add % in the beginning to each of selected lines. To remove them, you can do something like the following after making selection:

s/^%/ /

This will replace it with space.

You may like to read my post: Copy text to system clipboard in VIM.

Copy to system clipboard in VIM


You might have noticed that when you copy something from vim and try to paste somewhere else (maybe your browser), then it doesn’t work.

First of all you need to install the following packages:

sudo apt-get install vim-gnome

Most probably, vim-gui-common package will get automatically installed with it.

Now, you may confirm it using:

vim --version | grep 'clipboard'

If it says +clipboard then you are good to go. At present, my vim version is 7.4.52.

Now open any file with vim, and select text using v or V. Then press “+y to copy it to system clipboard. And “+p to paste in another vim file.

I found this helpful on vim wikia:

vim wikia

Nested Tmux


If you don’t know the basic functionality of tmux,¬†then visit this.

Now, we know that we can create different sessions using tmux. But what if we want the sessions open concurrently on the same window. After opening tmux, we can divide the window in several sessions. To execute things in tmux, we first have to send command prefix first and then press some key. For example, first type Ctrl-b and then press %, to divide the window into two vertical panes. You can do the similar if you want to do it horizontally. Type Ctrl-b and then (double quotes or shift+’). You can use different combination of them. To cycle between all the panes, press Ctrl-b and then o.

Now if you want to nest one tmux session into existing opened tmux session, you would type tmux again! But this doesn’t work simply. You’ll probably got something similar: “sessions should be nested with care, unset $TMUX to force“. To overcome this, you may use shortcut Ctrl-b and then type :new¬†and press ‘Enter‘. This will open a new session window. But as they warned, sessions should be nested with care. ūüôā You can always use tmux list-sessions to know list of currently opened sessions and you can connect to them using:

tmux attach -t 2

Here 2 is the id of the session, that you’ll get from the list-session command.

Another easy way to switch between the sessions is to press Ctrl-b and then press s. Then browse the sessions and press ‘Enter’ to get attached to it.

Now I had a problem while nesting tmux sessions. Suppose you have tmux running and then you ssh into a remote machine and run tmux there. Then, while detaching the tmux session on the remote machine, your local tmux will be detached with the normal Ctrl-b and d.

So, to overcome this, you have to send two command prefixes i.e. Ctrl-b (one for the local tmux and another for talking to the remote machine’s tmux). Hence type: Ctrl-b Ctrl-b and then d to detach the tmux on ssh one.

 

 

 

Wake On LAN


If you have one computer at a location on a network and you want to turn it on remotely i.e. without physically present at that location, then there is something called Wake On LAN (WOL). But the condition is the computer’s hardware must support WOL and it should be¬†provided continuous power supply and connected to the network.

For WOL to work, the target computer must have the functionality of WOL in it. On most computers, one may find it in the BIOS > Power Management >Wake on LAN or Wake-up by PCI card. If you find it there, then Enable it.

Once WOL enabled on first computer, note down the MAC address of the computer. You may do it on Linux by typing command:

ifconfig

MAC address will be the 12 digit code separated by colons (usually) and is labelled as HWaddr as after running above command in terminal.

On a windows pc, run

ipconfig /all

to see the same.

For more about setting up WOL, see: https://www.mythtv.org/wiki/Wake-on-LAN

Now, you need to set up the other computer from which you want to wake up the first one. I am using Linux (Ubuntu 14.04) currently, so on that I’ve compiled WOL. Here is the link to the tool: ¬†http://sourceforge.net/projects/wake-on-lan/

After downloading, extract the tar.gz file and go to that directory in terminal. First run

./configure

and after that run

make

and finally run

make install

and you are done. Read the INSTALL file in the same directory for further help.

After it installs, run command:

wol <mac:address:here>