Showing posts with label Line. Show all posts
Showing posts with label Line. Show all posts

17 September 2023

How to set up a command for toggling line wrapping in vim?

In .vimrc, add a line -

map <C-a> :set wrap!<CR>

Thus you press Ctrl-A and toggle between line wrapping and unwrapping

12 April 2022

How to debug or test python scripts using inspect module and print and input?

At the top of your script, 
from inspect import currentframe, getframeinfo
At the spot where you want to print the value of a variable,
print(f'Line #{getframeinfo(currentframe()).lineno}:\nDescription:\n{varName}\nEnd of Description\n')
input()

26 January 2022

How to break all lines into 78 character long all at once in vim?

(Author: Sherman Chen)


Method 1
========

:set textwidth=78

gggqG

"gg" takes you to the first line, "gq" is the format operator and "G" the motion that jumps to the last line.


Method 2
========

:set textwidth=78 

:1,$g/.*/normal jgwap



Method 3
========

1. :set linebreak
   to recover those broken words at the end of each line into whole words.

2. :set textwidth=78

3. Find a line which is longer than 78 characters, say line #3.  Move the cursor to that line minus one, i.e. 3 - 1 = 2.

4. qaj$aa Backspace Escape q

5. See how many lines the file contains totally, say, 100.

6. gg

7. 100@a Enter

All the lines will become 78 character long now.


Method 4 (which has improved Method
3)
========

1. :set linebreak
   to recover those broken words at the end of each line into whole words.

2. :set textwidth=78

3. Move the cursor to any line which is longer than 78 characters.

4. qa$aa Backspace Escape q

5. :%normal @a

All the lines will become 78 character long now.


(END)

13 January 2022

How to automatically break long lines if lines are too long in vim?

To break long lines,

:set textwidth=78

To prevent a line from being broken from within a word,

:set linebreak

05 September 2021

How to Delete Lines in Vim / Vi

How to Delete Lines in Vim / Vi 

 https://linuxize.com/post/vim-delete-line/

 

 Deleting a Line

The command to delete a line in Vim is
  dd .

Below are step-by-step instructions to delete a line:

    Press the Esc key to go to normal mode.
    Place the cursor on the line you want to delete.
    Type 
d and hit Enter to remove the line.

Pressing dd multiple times will delete multiple lines.
Deleting Multiple Lines

To delete multiple lines at once, prepend the dd command with the number of lines to be deleted. For example, to delete five lines you would do the following:

    Press the Esc key to go to normal mode.
    Place the cursor on the first line you want to delete.
    Type 
5dd  and hit Enter to delete the next five lines.

Delete a range of lines

The syntax for deleting a range of lines is as follows:

  :[start],[end]d 

For example, to delete lines starting from 3 to 5 you would do the following:

    Press the Esc key to go to normal mode.
    Type 
:3,5d  and hit Enter to delete the lines.

You can also use the following characters to specify the range:

    . (dot) - The current line.
    $ - The last line.
    % - All lines.

Here are a few examples:

    :.,$d - From the current line to the end of the file.
    :.,1d - From the current line to the beginning of the file.
    :10,$d - From the 10th line to the end of the file.

Delete All Lines

To delete all line you can use either the % symbol that represents all lines or the 1,$ range:

    Press the Esc key to go to normal mode.
    Type  
:%d  and hit Enter to delete all the lines.

Deleting Lines Containing a Pattern

The syntax for deleting multiple lines based on a specific pattern is as follows:


 :g/<pattern>/d 

The global command (g) tells the delete command (d) to delete all lines containing the <pattern>.

To match the lines not matching the pattern, add an exclamation mark (!) before the pattern:

 :g!/<pattern>/d

The pattern can be a literal match or regular expression . Below are some examples:

     :g/foo/d  - Delete all lines containing the string “foo”. It also removes line where “foo” is embedded in larger words, such as “football”.
    :g!/foo/d - Delete all lines not containing the string “foo”.
    :g/^#/d - Remove all comments from a Bash script. The pattern ^# means each line beginning with #.
    :g/^$/d - Remove all blank lines. The pattern ^$ matches all empty lines.
    :g/^\s*$/d - Remove all blank lines. Unlike the previous command, this also removes the blank lines that have zero or more whitespace characters (\s*).



27 February 2018

How to insert line numbers to a Vim file

How to insert line numbers to a Vim file?

Insert line numbers

The :s command can be used to insert line numbers before each line:
:%s/^/\=printf('%-4d', line('.'))
The pattern ^ matches the start of every line, and the replacement \= is the result of evaluating the following expression. That expression uses printf() to format the number of the current line: %-4d is a left-aligned decimal number, padded if necessary by adding spaces to a 4-column width (%4d is right-aligned, and %04d inserts leading zeroes).