• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

vi : remove space at beginning and end of each line

November 3, 2015 by Sharad Chhetri

This post is helpful to all vi / vim user. By this vi command user can remove space at beginning and end of each line . Today, while working I copied huge server list and pasted to new file. Now the file has lots of spaces at beginning and end of most of the lines.

By the given below two vi commands, delete space at the beginning and end of lines.

Remove space at the beginning of each line in a file

1. Open the file with vi editor

vi 

2. Press ESC key, write down below given command and hit enter

:%s/^s+

3. Save and Exit from file by pressing ESC key and typing

:wq!

Remove space at the end of each line in a file

1. Open the file with vi editor

vi 

2. Press ESC key, write down below given command and hit enter

:%s/s+$//

3. Save and Exit from file by pressing ESC key and typing

:wq!

NOTE: s finds space or tab), and + finds one or more occurrences inside the file.

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Convert new line to space by using sed command
  2. convert space into new line using sed command
  3. df command not showing correct free space in linux
  4. Linux enable or disable multiple swap space
  5. How to know how many swap space exist in linux system
  6. fatal error: error writing to /tmp/ccwAjc9Z.s: No space left on device
  7. Create and Remove AWS EC2 key pair by using command line
  8. tr command to convert lines to space , tab and vertical tab
  9. Completely remove package on Ubuntu / Debian by apt-get
  10. How to remove date from WordPress Post URL

Filed Under: Linux Tagged With: vi

Reader Interactions

Comments

  1. Haldun says

    October 20, 2020 at 10:07 am

    This does not seem to work.
    Correct replace is
    :%s/^\s+

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

How to convert rpm file into deb file

Nagios HTTP WARNING: HTTP/1.1 403 Forbidden

How to encode and decode the strings with base64

SSH WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

master admin password openerp 7.x

fatal error: error writing to /tmp/ccwAjc9Z.s: No space left on device

set and unset line number in file with vi editor

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy