1

Note: For those of you who do not know VHDL do not worry, I will explain the problem with an example.

Motivation

I have a VHDL file which is a big mess of buses. I have declared many signals and I think I have some unused signals. I want to delete unnecessary declarations.

Example

Imagine I have the following signal declarations

signal exmem_wb_we_in : std_logic;
signal exmem_const_in : std_logic_vector(15 downto 0);
signal exmem_data_in : std_logic_vector(15 downto 0);
signal exmem_addr_in : std_logic_vector(15 downto 0);
signal exmem_foo_in : std_logic;
signal exmem_mem_en_in : std_logic;
signal exmem_bar_in : std_logic_vector(15 downto 0);
signal exmem_jump_cond_in : std_logic_vector(3 downto 0);

Say the signals in bold (exmem_foo_in and exmem_bar_in) are never used. Then those words occur only once in the file. I want the lines where they appear to be deleted.

The question

Is it possible to get Vim to delete all lines which hold a single occurrence of a word in a file? The solution should use Vim commands that are 'always available', rather than OS dependent commands. A single command would be preferred. But multiple commands are also welcome.

Some assumptions

  • assume only one signal is declared per line;
  • there are no occurrences of numbers who appear only once in the file and could get us a line deleted when we in fact want that line;
  • there are no comments which introduce a single word occurrence.

Bonus points if you can make it still work without some of those assumptions.

Clarifications:

  • I don't know which words I want to delete beforehand.
  • A VHDL file might look like this.
  • Have you tried anything so far? By showing your attempts and describing what parts you are having difficulty with, we get a better understanding of your proficiency and can give a more precise answer. As it stands, it just looks like you've posted a requirement and want someone to write your code for you. – Ingo Karkat Mar 16 '15 at 13:40
  • Can you please clarify, so, the goal is to delete all lines containing unique words? Without knowing beforehandedly what are these unique words? – shabunc Mar 16 '15 at 13:47
  • @shabunc, yes, that is the situation. – Gonçalo Ribeiro Mar 16 '15 at 13:51
  • @IngoKarkat I haven't. I normally would (I know how SE works ;) ) but in this case I have no feeling at all on how I could achieve this. The only thing that occurs to me is searching and counting. But since I don't know the words I want to search beforehand I really don't know where to start... – Gonçalo Ribeiro Mar 16 '15 at 13:59
  • 1
    @SnoringFrog, I have two reasons: 1) achieving OS independence for the task (on Linux a script for this would be easy, but not on Windows, I think); 2) exploring the limits of Vim. – Gonçalo Ribeiro Mar 16 '15 at 15:34
1

Just to delete all lines containing some specific word you can by just typing:

:g/exmem_bar_in/d
:g/exmem_foo_in/d

The signature is

:[range]g[lobal]/{pattern}/[cmd]

(can be found by :h :g)

If you need to know the list of unique words, it actually heavily depends on what you indeed want to treat as a unique word. I doubt that you actually mean any unique word but rather something unique in the signal section. If it is so, then I'd just create a separate buffer, copied eveything there, then remove all besides "signal :" prefix:

:%s/\(.*\):.*/\1/ "deleting all after last semicolon

then leave only unique value:

:sort u.

Now I have a list of unique words I can work with.

  • That doesn't address what the OP is asking, though. – jamessan Mar 16 '15 at 13:37
  • @jamessan re-read the question and still don't get - so, the goal is to delete all lines containing unique words? – shabunc Mar 16 '15 at 13:38
  • 1
    The goal is to delete a line if that is the only line in the file containing a certain word. – jamessan Mar 16 '15 at 13:39
  • @jamessan I'm not arguing, I'm just trying to understand the question better to asnwer it neverthelees. "The only line in file containing a certain word" - but it looks like :g/<pattern>/d falls under this definition – shabunc Mar 16 '15 at 13:41
  • 1
    The OP doesn't know what words need to be deleted. So the goal is to have a way to run a command which will determine which of a set of words only appear in one line and then delete those lines. :g/{pattern}/d only solves the second part of the problem, not the first. – jamessan Mar 16 '15 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.