Vim Tips & Tricks

1. Save  file in Vim/Vi editor, which is opened without root permission

Consider this :    You are system admin, editing a file in the vim editor, which requires the ‘root’ level access. When you try to save it, you realize that you were editing the file in the normal mode instead of the ‘root’ mode.

Since you require the ‘root’ level access to save the file, you will have to save the  file, you will have to save it in some ‘tmp’ location and copy it back. To avoid doing this extra task, you can use the following command in the ‘Vi’ editor:

:w !sudo tee %

Where …

*   ‘:w’ stands for ‘write a file’.

*   ‘!sudo’ for ‘Call shell sudo command’.

*   ‘tee’ is the output of the write (‘Vim :w’) command redirected, using ‘tee’.

‘%’ : The ‘%’ is nothing but the current file name represented by ‘%’. However, this will prompt it to reload the file again (Hit ‘L’ load changes in the Vim editor itself).

2. Simple VIM feature

If you are looking for a particular pattern in a text file, then you can mention the same pattern at the command line while opening the file.

VIM will open the file and place the cursor on the first ‘match found’ line. It places the cursor on the first printing character of the line and not on the pattern being searched.

For example : If file “test,txt” has the string “main” then the following command will start the VIM editor and find the “main” pattern.

$vim +main test.txt

3. Starting VI with the search pattern

There is an option available in VI editor to open a file with a search pattern. If, for instance, a file named ‘xyz.c’ has a ‘printf’ string and you want to search the name, you can open the file with printf string as search pattern , so that it will not be required to open the file to search for the string.

At prompt, type the following:

$ vi +/printf xyz.c

On hitting Enter, the cursor will be placed at the start of the line where the pattern is found. Also if you are searching for any special characters, such as “[, *”, it needs to be escaped with “\”.

4. VIM copy file to file

Here is how to copy the required number of lines from one file to another in the vim editor. First use the following key combinations in the source file:

1. Press ESCAPE
2. Press SHIFT ” (Shift + Double quotes)
3. Press a
4. Press the number of lines you want to copy
5. Press y followed by another y
6. Now pres: (COLON) to get vim prompt
7. Hit e “Destination file name”
8. Once you enter the Destination file, go to the line where you want the copied lines to be inserted
9. Press ESCAPE
10. Press SHIFT “(Double quotes)
11. Press a
12. Press p

And the lines will be pasted there.

5. Editing multiple files in a single VIM editor

Open the file using the VIM Editor. For Example: vim file.c Now if you want to open another file and want the file.c to stay opened, just press Esc and then

:sp “your file” (without quotes).

This will split the window and open ‘your file’ along with ‘file.c’. You now have two files open in the  same vim editor. Using sp(split) you can open multiple files. For example:

:sp file2.c :sp file3.c

This will split the screen whenever you open a new file using sp. You can select the file pressing CTRL+w+w. You have to do this till you reach the specified file you want. The window will be split in to small parts. For more readability, you can enlarge the specified file using the on command. Press Exc and type:

: on

The file you are in will be maximized, and the rest will not be visible. After making all the files visible, apply the command ba. Press Esc and type:

: ba

6. Parenthesis matching, using ‘%’ in the VI or VIM editor

Here is a tip that can be very useful for programmers writing or debugging code in C language, using the  Vi or Vim editor. This will help you to find the opening and corresponding closing parentheses/bracket.

When working in ‘Command’ mode in Vi editor, ‘%’ can be used to find the opening/closing parentheses/bracket corresponding to its opening/closing parentheses/bracket , which is currently under the cursor. That is, if a simple C  program is opened, and you press ‘%’ in command mode when the cursor is under the last closing parentheses/bracket ‘}’, the cursor will move to the opening parentheses for the main function. Similarly, you can find if there
exists a closing parentheses/bracket for all opening parentheses in your code.

7. Remove blank lines from a file using Vi editor

Open the file from which you want to remove the blank lines in a Vi editor. Now run the following command in Vi editor:

:g/^$/d

The example given below will make the things more clear;

Create a test file with a blank space.

$cat > test
1

2

3

^d

This will create a test file with one blank line between two lines.

Now open the test file in Vi editor and run the following command:

$vi test

:g/^$/d

Save and exit the editor.

The final file that you get after saving will be the file without any blank lines in it.

8. Removing the Special characters from text files created in Windows using Vi/Vim editor

Sometimes, when you open a text file in Linux that was created in windows, special characters like ‘^M’ appear here and there. Here is the solution to remove these characters from the text file.

Just open the ‘vim’ editor and type the command:

$vim ‘file.txt’ (file name without quotes).

Hit Esc and type

:set ff=unix

Now save the file:

That is it! You will notice those annoying ‘^M’ characters are gone. Alternatively, you can also install and use the command ‘dos2unix’.

9. Eliminate the ‘^M’ character using ‘vi’ editor

Sometimes certain characters (e.g., ‘^M’ characters) appear when we convert a file from DOS to UNIX. To remove these ‘^M’ characters, which appear at the end of every line, use the following command in the Vi editor.

:%s/^M//g

(To type ‘^M’, hold the ‘Control’ key, press ‘V’ and then the ‘M’ key (both while holding the control key), and the character ‘^M’ will appear)

This command will ‘find’ all occurrence of the character ‘^M’ and replace them with ‘nothing’ . Here, the ‘:%s’ denotes the basic search and replace command in the vi editor. It tells the ‘Vi’ to replace the regular expression between the second and third slashes (nothing, in this case). The ‘g’ at the end directs the Vi editor to search and replace, globally all occurrence of the character.

10. Setting abbreviations in VI editor

There is feature in the VI editor that allows you to define expansion values for abbreviations. This means that the moment a user types an abbreviation in the edit mode (for which expansion has already been set), the editor generates expansion.

An abbreviation can be defined in the command mode  by giving the following ‘EX’ command:

:ab abbr  full_text

where ‘abbr’ is an abbreviation for the specified phrase. The expansion is done only when the abbreviation is typed as a word. Avoid repeating the abbreviation in the defined phrase.

The abbreviation can be disabled by typing the following command:

:unab abbr

11. Remembering the deleted text in Vi

In case you want to perform the cut-and-paste function at several places in your file, the Vi text editor provides an easy method to name a buffer to perform this task. We’ll look at an example to understand this. Let’s suppose you have a file ‘MyFile.Txt’ with the following content:

— MyFile.txt —
Sun                      
Moon                  
Stars                   
Planets               
Clouds               
— MyFile.txt —

Now Suppose I want to cut Line2 and paste it later in Line3. In Vi, I can Use the double quote (“) Operator to name the ‘cut’ command buffers. For example, in the above case, go to Line2, and then type “cdd to save Line 2 with the name ‘c’. (dd is used to delete the current line).

Now, we can paste the deleted line by typing “cp. So, simply go to Line3 and type “cp. Line2 will be pasted after Line 3 as follows.

— MyFile.txt —
Sun                     
Stars                  
Moon                 
Planets              
Clouds               
— MyFile.txt —

Advertisements

Installation of flash player in Slackware

N.B : Support for Firefox in Linux is only up to v. 11.2 of flash player

[1] Flash Player  Installation : Manual Way

  1. Change to the folder in which the flash-player tar ball is downloaded.
    .

    $cd path_to_the_folder_downloaded_folder
  2. Extract the file.
    .

    $ tar -xf install_flash_player_11_linux.tar.gz
  3. Create a directory named ‘plugins’ in /usr/lib/firefox-* (32 bit slackware), or /usr/lib64/firefox-* (64 bit slackware). ( For firefox 21 and above it is /usr/lib/firefox-*/browser or /usr/lib64/firefox-*/browser ).
  4. # mkdir /usr/lib/firefox-*/plugins or # mkdir /usr/lib64/firefox-*/plugins
  5. Copy the libflashplayer.so to the plugins folder.
    .

    # cp libflashplayer.so /usr/lib/firefox-*/plugins/ (for 32 bit slackware)
    
    or
    
    # cp libflashplayer.so /usr/lib64/firefox-*/plugins/ (for 64 bit slackware)
  6. Restart the Firefox browser.
    .
  7. Open Firefox web browser. Type about:plugins to verify flash player.

Enjoy the YouTube videos.

[2] Flash Player  Installation : Using Slack builds

To install the Adobe flash player 11 plugin in Slackware 13.37, we can get the slackbuild scripts from Slackware ftp server or HTTP mirrors

Here is one URL : http://slackbuilds.org/repository/13.37/multimedia/flash-player-plugin/

Download the file “flash-player-plugin.tar.gz” which contains

  • doinst.sh
  • flsh-player-plugin.info
  • flash-player-plugin.SlackBuild
  • README
  • slack-desc

Uncompress the tar ball and change directory to the uncompressed folder. Then run the slack build to install the flash player

Step by step instructions are given below :

  1. Download “flash-player-plugin.tar.gz” from Slackware mirror. The above link is one of them.
  2. Download the “install_flash_player_11_linux.x86_64.tar.gz” from the Adobe website.
  3. Uncompress the tar ball
  4. Copy the “install_flash_player_11_linux.x86_64.tar.gz ” file to the uncompressed folder
  5. Change directory to where you uncompressed slackbuild scripts.
  6. Run slackbuild script to create Slackware package.
  7. $ cd  /tmp
  8. $ installpkg flash-player-plugin-11.1.102.55-x86_64-1_SBo.tgz