39

How can I remove the first few characters like remove ; from the selected lines using commands? I switched into the insert mode, but can't figure out how to do.

;extension=php_bz2.dll
;extension=php_curl.dll
;extension=php_fileinfo.dll
;extension=php_ftp.dll
;extension=php_gd2.dll
;extension=php_gettext.dll
;extension=php_gmp.dll
;extension=php_intl.dll
;extension=php_imap.dll
;extension=php_interbase.dll
;extension=php_ldap.dll
;extension=php_mbstring.dll
;extension=php_exif.dll      ; Must be after mbstring as it depends on it
;extension=php_mysqli.dll
;extension=php_oci8_12c.dll  ; Use with Oracle Database 12c Instant Client
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll

3 Answers 3

62
  1. Place cursor on first or last ;
  2. Press Ctrl+v to enter Visual Block mode
  3. Use arrow keys or j, k to select the ; characters you want to delete (or the other "first few characters")
  4. Press x to delete them all at once
2
  • 7
    using j or k is better than arrow keys ;-)
    – vdegenne
    Commented May 8, 2020 at 23:28
  • 1
    VSCode's embedded terminal (on Windows using git bash), I had to use Ctrl + Q instead of Ctrl + V as it was bound to paste.
    – Greg K
    Commented May 5, 2023 at 12:42
12

Normally you'd do this in ex-mode, e.g., if that is the entire file you could do just

:%s/^;//

to remove all leading semicolons.

If there are other lines that you do not want to modify, of course, it becomes more complicated.

2
  • 1
    Can we give line numbers in the above %s command?
    – Arsal
    Commented Aug 25, 2020 at 22:08
  • You could use line numbers instead of %. That's a standard feature of ex/vi. Commented Aug 25, 2020 at 23:03
12

techraf's answer worked for me. Another option is to use the :norm-command of vim.

  1. Place cursor on first line
  2. Press Shift+V to enter Visual Line mode
  3. Press 16j to select 16 lines below the current line
  4. Type :norm ^x

: enters command mode, norm specifies a command that will be executed on all lines, ^ goes to the beginning of the line, and x deletes the first character.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .