Auto-mount disks on Startup


sudo fdisk -l

See which disk you want to auto-mount.

Suppose you attached a USB-drive and it’s shown as “/dev/sdb1” in above command.

Its file-system will also be shown beside that. Match your type from System name and notice the corresponding Linux type.

 

System name

English name

Linux type

 

W95 FAT32

Microsoft FAT32

vfat

 

W95 FAT32 (LBA)

Microsoft FAT32

vfat

 

W95 FAT16 (LBA)

Microsoft FAT16

vfat

 

W95 Ext’d (LBA)

Microsoft extended partition

Not used

 

NTFS volume set

Microsoft NTFS

ntfs

 

NTFS volume set

Microsoft NTFS with read-write access

ntfs-3g

 

Apple_HFS

Apple HFS

hfsplus

Suppose it’s W95 FAT32 (LBA) so your Linux type is vfat. If you are unsure about this type, you may consider mounting the device using: sudo mount /dev/sdb1 and then run mount command to see mounted file-systems and it’s file-system type.

After gathering all the necessary information, open /etc/fstab with root permissions with your text editor e.g. sudo vim /etc/fstab

Then add the new entry,

/dev/sdb1       /mnt/usb_drive       vfat       errors=remount-ro       0      0

to fill in line with the following.

<file system>     <mount point>   <type>       <options>         <dump>  <pass>

Then save the file and exit. You may check on the next reboot if it is mounted.

errors=remount-ro option is for the case when some error occurs, the device will get remounted as read-only. The <dump> option specifies if the file-system needs to be dumped (back-up) for safe keeping. 0 here means it doesn’t need to be dumped. And the <pass> means that the order which filesystem checks are to be performed at system reboot. The root filesystem should be labeled as 1, while others as 20 if we don’t want it to be checked. See man fstab command for more.

Advertisements

Vim: Add some text at the start/end of each line


Suppose, you have a file with 1000 lines. You want to add some text at the beginning/end of each line. You won’t think of doing it manually (actually, I can’t stop you). Here, I am telling you how to save you a lot of time.

For example, we have the following lines in the file:

Name

Email

Phone Number

Address

And you want your output to be:

“Name”,

“Email”,

“Phone Number”,

“Address”,

and so on.

In Vim, you can simply type:

:%s/$/",/

It will place “, at the end of each line.

Similarly, for beginning:

:%s/^/"/

It will place ” at the beginning of each line.

Hence, we used $ for appending to end and ^ for prepending.

You can also do this for a few lines by visually selecting them with and then pressing : and type the command

s/^/"/

Skip the “%” because we do it while doing things globally across the file. So it will look like:

:'<,'>s/^/"/

Another method, if you want to type some text interactively on a couple of lines:

Ctrl+V

Select a few rows (downside, pressing j). After selecting the position in couple in all rows, press:

Shift + i

They type what you want. Then press the Esc key:

Esc

Now, wait 1 second. You’ll see same text to be replicated over all rows you selected.

Please share in comments, if you even easier method to do the similar.

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