Shortcut to remove first character of multiple lines in gedit

5,883

Solution 1

This can be done using gedit Snippets. They are available as part of gedit-plugins from your distro's repositories.

Once they're installed, open gedit, go to Edit -> Preferences -> Plugins, and enable Snippets.

Now you have to create the new snippet which is pretty straightforward and an amazing time saver for code you write frequently.

1. Go to Tools -> Manage Snippets.

2. Find the language/type of file you want to use the snippet for (or Global for all) and click the '+' icon to create new snippet.

3. Name the snippet, then click in the "shortcut key" text box on the right and press your shortcut key combination you want to use: e.g. Shift_ctrl_%

4. Then in the box on the right, enter:

$<
import re
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
  output += re.sub('^%((.)*)', r"\1\n", line)
return output
>

This uses a python regular expression to only remove the first character in a line if it is '%'.

note: you can use any python code in a snippet, for example if you wanted to remove multiple instances of '%' or '#' at the beginning of a line you could use lstrip.

$<
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
    output += line.lstrip('%#') + "\n"
return output
>

5. After you create the snippet, click close, highlight your text and hit your shortcut key.

note: If you want a snippet that will return % back t the beginning of all highlighted lines:

$<
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = "";
for line in lines:
  output += "%" + line + "\n";
return output
>

note: for some reason snippets with shortcut keys only work (for me) when they're defined for a specific language. Global shortcut keys don't work but the tab triggers for them do, ymmv.

More info on snippets at http://live.gnome.org/Gedit/Plugins/Snippets

Solution 2

That really depends on the software you're using.

For instance, MS-Word would left you alt-select the first character of the lines (if the lines doesn't wrap around).

If you're using an editor that allows Regular Expression search and replace you could use something like ^. to search for the first character of each line.

Unfortunately you need to be more specific.

Share:
5,883

Related videos on Youtube

Christoph Rüegg
Author by

Christoph Rüegg

Updated on September 17, 2022

Comments

  • Christoph Rüegg
    Christoph Rüegg over 1 year

    I want to be able to remove the first character of a line when I highlight multiple lines in gedit.

    Example:

    %Example is
    %Commented Code
    %Uncomment using this shortcut
    

    I would then highlight/select these lines, and remove the first character to make it look like this:

    Example is
    Commented Code
    Uncomment using this shortcut
    

    I'm pretty sure there is an actual shortcut for this.

    If there is another text editor on Linux that it would work in, it would be nice to know how to do it in that editor as well.

    • whitequark
      whitequark over 14 years
      This probably will be specific for editors. Can you provide more detail?
    • hyperslug
      hyperslug over 14 years
      In some editors you can hold down alt to select a block of text, then delete.
    • Scott McClenning
      Scott McClenning over 14 years
      added linux tag
    • J Slick
      J Slick over 11 years
      Sublime Text has powerful multiselection features. If the cursor were at the beginning of the first line, you would just go ctrl-alt-down twice, then press delete.
  • Jared Harley
    Jared Harley over 14 years
    In Virtual Studio, it's Ctrl+Shift+E, C to add comment marks to a selection, and Ctrl+Shift+E, U to uncomment a selection.